Class: FaviconParty::Fetcher
- Inherits:
-
Object
- Object
- FaviconParty::Fetcher
- 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
-
#candidate_urls ⇒ Object
Returns the value of attribute candidate_urls.
-
#data ⇒ Object
Returns the value of attribute data.
-
#favicon_url ⇒ Object
Returns the value of attribute favicon_url.
-
#final_url ⇒ Object
Follow redirects from the query url to get to the last url.
-
#html ⇒ Object
Returns the value of attribute html.
-
#query_url ⇒ Object
Returns the value of attribute query_url.
Instance Method Summary collapse
- #fetch ⇒ Object
- #final_location(response_headers) ⇒ Object
-
#find_favicon_urls_in_html(html) ⇒ Object
Tries to find favicon urls from the html content of query_url.
- #get_favicon_data ⇒ Object
- #get_favicon_data_from_candidate_urls ⇒ Object
- #get_favicon_data_from_url(url) ⇒ Object
- #get_urls ⇒ Object
- #has_data? ⇒ Boolean
-
#http_get(url) ⇒ Object
Encodes output as utf8 - Not for binary http responses.
-
#initialize(url, options = {}) ⇒ Fetcher
constructor
A new instance of Fetcher.
- #set_candidate_favicon_urls ⇒ Object
- #url_root ⇒ Object
Methods included from Utils
#encode_utf8, #get_mime_type, #prefix_url, #with_temp_data_file
Constructor Details
#initialize(url, options = {}) ⇒ Fetcher
Returns a new instance of Fetcher.
21 22 23 24 25 26 27 28 29 |
# 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_urls ⇒ Object
Returns the value of attribute candidate_urls.
19 20 21 |
# File 'lib/favicon_party/fetcher.rb', line 19 def candidate_urls @candidate_urls end |
#data ⇒ Object
Returns the value of attribute data.
19 20 21 |
# File 'lib/favicon_party/fetcher.rb', line 19 def data @data end |
#favicon_url ⇒ Object
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_url ⇒ Object
Follow redirects from the query url to get to the last url
108 109 110 |
# File 'lib/favicon_party/fetcher.rb', line 108 def final_url @final_url end |
#html ⇒ Object
Returns the value of attribute html.
19 20 21 |
# File 'lib/favicon_party/fetcher.rb', line 19 def html @html end |
#query_url ⇒ Object
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
#fetch ⇒ Object
37 38 39 40 41 |
# 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) ⇒ Object
101 102 103 104 |
# 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) ⇒ Object
Tries to find favicon urls from the html content of query_url
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# 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 ⇒ Object
43 44 45 46 47 48 |
# 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 ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# 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. = get_urls error.[:favicon_url] ||= url error.[:base64_favicon_data] = data.base64_source_data raise error end end nil end |
#get_favicon_data_from_url(url) ⇒ Object
135 136 137 138 139 140 141 |
# 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 ⇒ Object
143 144 145 146 147 148 149 |
# 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? ⇒ Boolean
151 152 153 |
# File 'lib/favicon_party/fetcher.rb', line 151 def has_data? !@data.nil? && !@data.empty? end |
#http_get(url) ⇒ Object
Encodes output as utf8 - Not for binary http responses
33 34 35 |
# File 'lib/favicon_party/fetcher.rb', line 33 def http_get(url) FaviconParty::HTTPClient.get(url) end |
#set_candidate_favicon_urls ⇒ Object
95 96 97 98 99 |
# 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 ⇒ Object
130 131 132 133 |
# File 'lib/favicon_party/fetcher.rb', line 130 def url_root uri = URI final_url "#{uri.scheme}://#{uri.host}" end |