lib/flavicon/finder.rb in flavicon-0.2.0 vs lib/flavicon/finder.rb in flavicon-0.3.0
- old
+ new
@@ -14,34 +14,34 @@
@url = url
end
def find
response, resolved = request(url)
- favicon_url = extract_from_html(response.body, resolved) || default_path(resolved)
- verify_favicon_url(favicon_url)
+
+ extract_from_html(response.body, resolved)
+ .push(default_path(resolved))
+ .find { |url| verify_favicon_url(url) }
end
def verify_favicon_url(url)
response, resolved = request(url)
return unless response.is_a?(Net::HTTPSuccess) && response.body.to_s != '' && response.content_type =~ /image/i
resolved
end
def extract_from_html(html, url)
- link = Nokogiri::HTML(html).css('head link').find do |node|
- node[:rel] =~ /\A(shortcut )?icon\z/i
+ Nokogiri::HTML(html).css('head link').filter_map do |node|
+ URI.join(url, node[:href]).to_s if node[:rel] =~ /\A(shortcut )?icon\z/i
end
-
- URI.join(url, link[:href]).to_s if link
end
def default_path(url)
URI.join(url, '/favicon.ico').to_s
end
- # rubocop:disable Metrics/AbcSize,MethodLength
+ # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
def request(url, limit = 10)
raise TooManyRedirects if limit.negative?
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
@@ -58,10 +58,10 @@
request(extract_location(response, url), limit - 1)
else
[response, url]
end
end
- # rubocop:enable Metrics/AbcSize,MethodLength
+ # rubocop:enable Metrics/AbcSize,Metrics/MethodLength
# While the soon-to-be obsolete IETF standard RFC 2616 (HTTP 1.1) requires a complete absolute URI for redirection,
# the most popular web browsers tolerate the passing of a relative URL as the value for a Location header field.
# Consequently, the current revision of HTTP/1.1 makes relative URLs conforming.
def extract_location(response, url)