Parse icons from HTML
This commit is contained in:
@@ -10,15 +10,16 @@ module Manifique
|
||||
self.url = data[:url]
|
||||
self.from_web_manifest = []
|
||||
self.from_html = []
|
||||
self.icons = []
|
||||
end
|
||||
|
||||
def load_from_web_manifest(manifest)
|
||||
[:name, :short_name, :description, :icons,
|
||||
:theme_color, :background_color, :display,
|
||||
:start_url, :scope, :share_target].map(&:to_s).each do |prop|
|
||||
[ :name, :short_name, :description, :icons,
|
||||
:theme_color, :background_color, :display,
|
||||
:start_url, :scope, :share_target ].map(&:to_s).each do |prop|
|
||||
self.send("#{prop}=", manifest[prop]) if manifest[prop]
|
||||
self.from_web_manifest.push(prop)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def to_json
|
||||
|
||||
@@ -16,6 +16,7 @@ module Manifique
|
||||
|
||||
def fetch_metadata
|
||||
fetch_website
|
||||
|
||||
if manifest = fetch_web_manifest
|
||||
@metadata.load_from_web_manifest(manifest)
|
||||
else
|
||||
@@ -51,6 +52,7 @@ module Manifique
|
||||
parse_title_from_html
|
||||
parse_meta_elements_from_html
|
||||
parse_display_mode_from_html
|
||||
parse_icons_from_html
|
||||
end
|
||||
|
||||
def parse_title_from_html
|
||||
@@ -89,6 +91,27 @@ module Manifique
|
||||
end
|
||||
end
|
||||
|
||||
def parse_icons_from_html
|
||||
icon_links = @html.css("link[rel=icon]")
|
||||
if icon_links.any?
|
||||
icon_links.each do |link|
|
||||
icon = {}
|
||||
icon["src"] = link.attributes["href"].value rescue nil
|
||||
next if icon["src"].to_s.empty?
|
||||
icon["sizes"] = link.attributes["sizes"].value rescue nil
|
||||
icon["type"] = link.attributes["type"].value rescue get_icon_type(icon["src"])
|
||||
@metadata.icons.push icon
|
||||
end
|
||||
end
|
||||
|
||||
@metadata.from_html.push "icons" unless @metadata.icons.empty?
|
||||
end
|
||||
|
||||
def get_icon_type(src)
|
||||
extension = src.match(/\.([a-zA-Z]+)$/)[1]
|
||||
return "image/#{extension}"
|
||||
end
|
||||
|
||||
def do_get_request(url)
|
||||
conn = Faraday.new do |b|
|
||||
b.use FaradayMiddleware::FollowRedirects
|
||||
|
||||
Reference in New Issue
Block a user