class FaviconParty::Fetcher

Actually go and grab the favicon from the given url

Constants

ICON_SELECTORS

Attributes

candidate_urls[RW]
data[RW]
favicon_url[RW]
final_url[RW]
html[RW]
query_url[RW]

Public Class Methods

new(url, options = {}) click to toggle source
# File lib/favicon_party/fetcher.rb, line 21
def initialize(url, options = {})
  @options         = options
  @query_url       = prefix_url(url)
  @final_url       = nil
  @favicon_url     = nil
  @html            = nil
  @data            = nil
  @candidate_urls  = []
end

Public Instance Methods

fetch() click to toggle source
# File lib/favicon_party/fetcher.rb, line 37
def fetch
  @html = http_get final_url
  set_candidate_favicon_urls
  get_favicon_data
end
final_location(response_headers) click to toggle source
# File lib/favicon_party/fetcher.rb, line 101
def final_location(response_headers)
  location = response_headers.scan(/^Location: (.*)$/)[-1]
  location && location[0].strip
end
find_favicon_urls_in_html(html) click to toggle source

Tries to find favicon urls from the html content of query_url

# File lib/favicon_party/fetcher.rb, line 70
def find_favicon_urls_in_html(html)
  doc = Nokogiri.parse html
  candidate_urls = doc.css(ICON_SELECTORS.join(",")).map {|e| e.attr('href') }.compact
  candidate_urls.sort_by! {|href|
    if href =~ /\.ico/
      0
    elsif href =~ /\.png/
      1
    else
      2
    end
  }
  uri = URI final_url
  candidate_urls.map! do |href|
    href = URI.encode(href.strip)
    if href =~ /\A\/\//
      href = "#{uri.scheme}:#{href}"
    elsif href !~ /\Ahttp/
      # Ignore invalid URLS - ex. {http://i50.tinypic.com/wbuzcn.png}
      href = URI.join(url_root, href).to_s rescue nil
    end
    href
  end.compact.uniq
end
get_favicon_data() click to toggle source
# File lib/favicon_party/fetcher.rb, line 43
def get_favicon_data
  return @data if !@data.nil?
  @data = get_favicon_data_from_candidate_urls
  raise FaviconParty::FaviconNotFound.new(@query_url) unless @data
  @data
end
get_favicon_data_from_candidate_urls() click to toggle source
# File lib/favicon_party/fetcher.rb, line 50
def get_favicon_data_from_candidate_urls
  @candidate_urls.each do |url|
    data = FaviconParty::Image.new(get_favicon_data_from_url(url))
    begin
      if data.valid?(@options)
        @favicon_url = url
        return data
      end
    rescue FaviconParty::ImageMagickError => error
      error.meta = get_urls
      error.meta[:favicon_url] ||= url
      error.meta[:base64_favicon_data] = data.base64_source_data
      raise error
    end
  end
  nil
end
get_favicon_data_from_url(url) click to toggle source
# File lib/favicon_party/fetcher.rb, line 135
def get_favicon_data_from_url(url)
  if url =~ /^data:/
    data = url.split(',')[1]
    return data && Base64.decode64(data)
  end
  FaviconParty::HTTPClient.bin_get url
end
get_urls() click to toggle source
# File lib/favicon_party/fetcher.rb, line 143
def get_urls
  {
    :query_url    => @query_url,
    :final_url    => final_url,
    :favicon_url  => @favicon_url
  }
end
has_data?() click to toggle source
# File lib/favicon_party/fetcher.rb, line 151
def has_data?
  !@data.nil? && !@data.empty?
end
http_get(url) click to toggle source

Encodes output as utf8 - Not for binary http responses

# File lib/favicon_party/fetcher.rb, line 33
def http_get(url)
  FaviconParty::HTTPClient.get(url)
end
set_candidate_favicon_urls() click to toggle source
# File lib/favicon_party/fetcher.rb, line 95
def set_candidate_favicon_urls
  @candidate_urls = find_favicon_urls_in_html(@html)
  @candidate_urls << URI.join(url_root, "favicon.ico").to_s
  @candidate_urls << URI.join(url_root, "favicon.png").to_s
end
url_root() click to toggle source
# File lib/favicon_party/fetcher.rb, line 130
def url_root
  uri = URI final_url
  "#{uri.scheme}://#{uri.host}"
end