Move all metadata parsing to Metadata class
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
02fb119957
commit
8d79113438
@ -23,6 +23,14 @@ module Manifique
|
||||
end
|
||||
end
|
||||
|
||||
def load_from_html(html)
|
||||
@html = html
|
||||
parse_title_from_html
|
||||
parse_meta_elements_from_html
|
||||
parse_display_mode_from_html
|
||||
parse_icons_from_html
|
||||
end
|
||||
|
||||
def select_icon(options={})
|
||||
if options[:type].nil? && options[:sizes].nil? && options[:purpose].nil?
|
||||
raise ArgumentError, "Tell me what to do!"
|
||||
@ -58,6 +66,104 @@ module Manifique
|
||||
|
||||
private
|
||||
|
||||
def parse_title_from_html
|
||||
return if self.name
|
||||
|
||||
if title = @html.at_css("title") and !title.text.empty?
|
||||
self.name = title.text
|
||||
self.from_html.add "name"
|
||||
end
|
||||
end
|
||||
|
||||
def parse_meta_elements_from_html
|
||||
{
|
||||
description: "description",
|
||||
theme_color: "theme-color"
|
||||
}.each do |prop, name|
|
||||
next if self.send("#{prop}")
|
||||
if value = get_meta_element_value(name)
|
||||
self.send "#{prop}=", value
|
||||
self.from_html.add prop.to_s
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def parse_display_mode_from_html
|
||||
return if self.display
|
||||
|
||||
if get_meta_element_value("apple-mobile-web-app-capable") == "yes"
|
||||
self.display = "standalone"
|
||||
self.from_html.add "display"
|
||||
end
|
||||
end
|
||||
|
||||
def get_meta_element_value(name)
|
||||
if el = @html.at_css("meta[name=#{name}]") and !el.attributes["content"].value.empty?
|
||||
el.attributes["content"].value
|
||||
end
|
||||
end
|
||||
|
||||
def parse_icons_from_html
|
||||
parse_link_icons_from_html
|
||||
parse_apple_touch_icons_from_html
|
||||
parse_mask_icon_from_html
|
||||
end
|
||||
|
||||
def parse_link_icons_from_html
|
||||
return if self.icons.any?
|
||||
|
||||
if icon_links = @html.css("link[rel=icon]")
|
||||
icon_links.each do |link|
|
||||
icon = {}
|
||||
icon["src"] = link.attributes["href"].value rescue nil
|
||||
next unless is_adequate_src(icon["src"])
|
||||
icon["sizes"] = link.attributes["sizes"].value rescue nil
|
||||
icon["type"] = link.attributes["type"].value rescue get_icon_type(icon["src"])
|
||||
self.icons.push icon
|
||||
self.from_html.add "icons"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def parse_apple_touch_icons_from_html
|
||||
if icon_links = @html.css("link[rel=apple-touch-icon]")
|
||||
icon_links.each do |link|
|
||||
icon = { "purpose" => "apple-touch-icon" }
|
||||
icon["src"] = link.attributes["href"].value rescue nil
|
||||
next unless is_adequate_src(icon["src"])
|
||||
icon["sizes"] = link.attributes["sizes"].value rescue nil
|
||||
icon["type"] = link.attributes["type"].value rescue get_icon_type(icon["src"])
|
||||
self.icons.push icon
|
||||
self.from_html.add "icons"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def parse_mask_icon_from_html
|
||||
if mask_icon_link = @html.at_css("link[rel=mask-icon]")
|
||||
icon = { "purpose" => "mask-icon" }
|
||||
icon["src"] = mask_icon_link.attributes["href"].value rescue nil
|
||||
return unless is_adequate_src(icon["src"])
|
||||
icon["type"] = link.attributes["type"].value rescue get_icon_type(icon["src"])
|
||||
icon["color"] = mask_icon_link.attributes["color"].value rescue nil
|
||||
self.icons.push icon
|
||||
self.from_html.add "icons"
|
||||
end
|
||||
end
|
||||
|
||||
def is_data_url?(src)
|
||||
!!src.match(/^data:/)
|
||||
end
|
||||
|
||||
def is_adequate_src(src)
|
||||
!src.to_s.empty? && !is_data_url?(src)
|
||||
end
|
||||
|
||||
def get_icon_type(src)
|
||||
extension = src.match(/\.([a-zA-Z]+)$/)[1]
|
||||
"image/#{extension}"
|
||||
end
|
||||
|
||||
def sizes_to_i(str)
|
||||
str.match(/(\d+)x/)[1].to_i
|
||||
end
|
||||
|
@ -19,11 +19,8 @@ module Manifique
|
||||
|
||||
if manifest = fetch_web_manifest
|
||||
@metadata.load_from_web_manifest(manifest)
|
||||
parse_apple_touch_icons_from_html
|
||||
parse_mask_icon_from_html
|
||||
else
|
||||
parse_metadata_from_html
|
||||
end
|
||||
@metadata.load_from_html(@html)
|
||||
|
||||
@metadata
|
||||
end
|
||||
@ -50,108 +47,6 @@ module Manifique
|
||||
JSON.parse(res.body)
|
||||
end
|
||||
|
||||
def parse_metadata_from_html
|
||||
parse_title_from_html
|
||||
parse_meta_elements_from_html
|
||||
parse_display_mode_from_html
|
||||
parse_icons_from_html
|
||||
end
|
||||
|
||||
def parse_title_from_html
|
||||
return if @metadata.name
|
||||
|
||||
if title = @html.at_css("title") and !title.text.empty?
|
||||
@metadata.name = title.text
|
||||
@metadata.from_html.add "name"
|
||||
end
|
||||
end
|
||||
|
||||
def parse_meta_elements_from_html
|
||||
{
|
||||
description: "description",
|
||||
theme_color: "theme-color"
|
||||
}.each do |prop, name|
|
||||
next if @metadata.send("#{prop}")
|
||||
if value = get_meta_element_value(name)
|
||||
@metadata.send "#{prop}=", value
|
||||
@metadata.from_html.add prop.to_s
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def parse_display_mode_from_html
|
||||
return if @metadata.display
|
||||
if get_meta_element_value("apple-mobile-web-app-capable") == "yes"
|
||||
@metadata.display = "standalone"
|
||||
@metadata.from_html.add "display"
|
||||
end
|
||||
end
|
||||
|
||||
def get_meta_element_value(name)
|
||||
if el = @html.at_css("meta[name=#{name}]") and !el.attributes["content"].value.empty?
|
||||
el.attributes["content"].value
|
||||
end
|
||||
end
|
||||
|
||||
def parse_icons_from_html
|
||||
parse_link_icons_from_html
|
||||
parse_apple_touch_icons_from_html
|
||||
parse_mask_icon_from_html
|
||||
end
|
||||
|
||||
def parse_link_icons_from_html
|
||||
if icon_links = @html.css("link[rel=icon]")
|
||||
icon_links.each do |link|
|
||||
icon = {}
|
||||
icon["src"] = link.attributes["href"].value rescue nil
|
||||
next unless is_adequate_src(icon["src"])
|
||||
icon["sizes"] = link.attributes["sizes"].value rescue nil
|
||||
icon["type"] = link.attributes["type"].value rescue get_icon_type(icon["src"])
|
||||
@metadata.icons.push icon
|
||||
@metadata.from_html.add "icons"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def parse_apple_touch_icons_from_html
|
||||
if icon_links = @html.css("link[rel=apple-touch-icon]")
|
||||
icon_links.each do |link|
|
||||
icon = { "purpose" => "apple-touch-icon" }
|
||||
icon["src"] = link.attributes["href"].value rescue nil
|
||||
next unless is_adequate_src(icon["src"])
|
||||
icon["sizes"] = link.attributes["sizes"].value rescue nil
|
||||
icon["type"] = link.attributes["type"].value rescue get_icon_type(icon["src"])
|
||||
@metadata.icons.push icon
|
||||
@metadata.from_html.add "icons"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def parse_mask_icon_from_html
|
||||
if mask_icon_link = @html.at_css("link[rel=mask-icon]")
|
||||
icon = { "purpose" => "mask-icon" }
|
||||
icon["src"] = mask_icon_link.attributes["href"].value rescue nil
|
||||
return unless is_adequate_src(icon["src"])
|
||||
icon["type"] = link.attributes["type"].value rescue get_icon_type(icon["src"])
|
||||
icon["color"] = mask_icon_link.attributes["color"].value rescue nil
|
||||
@metadata.icons.push icon
|
||||
@metadata.from_html.add "icons"
|
||||
end
|
||||
end
|
||||
|
||||
def is_data_url?(src)
|
||||
!!src.match(/^data:/)
|
||||
end
|
||||
|
||||
def is_adequate_src(src)
|
||||
!src.to_s.empty? && !is_data_url?(src)
|
||||
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 |faraday|
|
||||
faraday.response :follow_redirects
|
||||
@ -163,7 +58,7 @@ module Manifique
|
||||
else
|
||||
raise Manifique::Error.new "Failed with HTTP status #{res.status}", "http_#{res.status}", url
|
||||
end
|
||||
rescue Faraday::ConnectionFailed, Faraday::TimeoutError, Faraday::SSLError => e
|
||||
rescue Faraday::ConnectionFailed, Faraday::TimeoutError, Faraday::SSLError
|
||||
raise Manifique::Error.new "Failed to connect", "connection_failed", url
|
||||
end
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user