 defmodule FourOhFourFinderApp.PageAnalyzer do
   use HTTPoison.Base
-  require Logger
 
   def analyze(url) do
     case get_with_redirects(url) do
-      {:ok, %HTTPoison.Response{status_code: code, body: body}} ->
-        html = Floki.parse(body)
+      {:ok, response = %HTTPoison.Response{status_code: code, body: body}} ->
         if good_resp?(code) do
-          %{
-            url: url,
-            http_status: code,
-            success: true,
-            outgoing_hrefs: tree_hrefs(html, url),
-            ids: tree_ids(html)
-          }
+          if is_html(response) do
+            html = Floki.parse(body)
+            %{
+              url: url,
+              http_status: code,
+              success: true,
+              outgoing_hrefs: tree_hrefs(html, url),
+              ids: tree_ids(html)
+            }
+          else
+            %{
+              url: url,
+              http_status: code,
+              success: true,
+              outgoing_hrefs: [],
+              ids: []
+            }
+          end
         else
           %{ url: url, http_status: code, success: false }
         end

    end
  end

  def redirect_location(headers) do
    elem(Enum.find(headers, fn({header, _}) -> "location" == String.downcase(header) end), 1)
  end

  def good_resp?(code) do

    case get(url) do
      {:ok, response} ->
        if redirect?(response.status_code) do
          get_with_redirects(resolve_url(get_header(response.headers, "Location"), url))
        else
          {:ok, response}
        end
