Class: FaviconParty::Fetcher

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/favicon_party/fetcher.rb

Overview

Actually go and grab the favicon from the given url

Constant Summary collapse

ICON_SELECTORS =
[ 'link[rel="shortcut icon"]',
  'link[rel="icon"]',
  'link[type="image/x-icon"]',
  'link[rel="fluid-icon"]',
  'link[rel="apple-touch-icon"]'
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#encode_utf8, #get_mime_type, #prefix_url, #with_temp_data_file

Constructor Details

#initialize(url, options = {}) ⇒ Fetcher



21
22
23
24
25
26
27
28
# File 'lib/favicon_party/fetcher.rb', line 21

def initialize(url, options = {})
  @query_url       = prefix_url(url)
  @final_url       = nil
  @favicon_url     = nil
  @html            = nil
  @data            = nil
  @candidate_urls  = []
end

Instance Attribute Details

#candidate_urlsObject

Returns the value of attribute candidate_urls.



19
20
21
# File 'lib/favicon_party/fetcher.rb', line 19

def candidate_urls
  @candidate_urls
end

#dataObject

Returns the value of attribute data.



19
20
21
# File 'lib/favicon_party/fetcher.rb', line 19

def data
  @data
end

#favicon_urlObject

Returns the value of attribute favicon_url.



19
20
21
# File 'lib/favicon_party/fetcher.rb', line 19

def favicon_url
  @favicon_url
end

#final_urlObject

Follow redirects from the query url to get to the last url



102
103
104
# File 'lib/favicon_party/fetcher.rb', line 102

def final_url
  @final_url
end

#htmlObject

Returns the value of attribute html.



19
20
21
# File 'lib/favicon_party/fetcher.rb', line 19

def html
  @html
end

#query_urlObject

Returns the value of attribute query_url.



19
20
21
# File 'lib/favicon_party/fetcher.rb', line 19

def query_url
  @query_url
end

Instance Method Details

#fetchObject



36
37
38
39
40
# File 'lib/favicon_party/fetcher.rb', line 36

def fetch
  @html = http_get final_url
  set_candidate_favicon_urls
  get_favicon_data
end

#find_favicon_urls_in_html(html) ⇒ Object

Tries to find favicon urls from the html content of query_url



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/favicon_party/fetcher.rb', line 69

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
end

#get_favicon_dataObject



42
43
44
45
46
47
# File 'lib/favicon_party/fetcher.rb', line 42

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_urlsObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/favicon_party/fetcher.rb', line 49

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?
        @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) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/favicon_party/fetcher.rb', line 131

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_urlsObject



139
140
141
142
143
144
145
# File 'lib/favicon_party/fetcher.rb', line 139

def get_urls
  {
    :query_url    => @query_url,
    :final_url    => final_url,
    :favicon_url  => @favicon_url
  }
end

#has_data?Boolean



147
148
149
# File 'lib/favicon_party/fetcher.rb', line 147

def has_data?
  !@data.nil? && !@data.empty?
end

#http_get(url) ⇒ Object

Encodes output as utf8 - Not for binary http responses



32
33
34
# File 'lib/favicon_party/fetcher.rb', line 32

def http_get(url)
  FaviconParty::HTTPClient.get(url)
end

#set_candidate_favicon_urlsObject



94
95
96
97
98
# File 'lib/favicon_party/fetcher.rb', line 94

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_rootObject



126
127
128
129
# File 'lib/favicon_party/fetcher.rb', line 126

def url_root
  uri = URI final_url
  "#{uri.scheme}://#{uri.host}"
end