Refactor icon parsing, add apple touch icons

This commit is contained in:
Basti 2018-07-04 09:27:03 +02:00
parent 87baa5960c
commit 44bf2404f6
3 changed files with 42 additions and 2 deletions

View File

@ -92,6 +92,12 @@ module Manifique
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 = {}
@ -100,9 +106,26 @@ module Manifique
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 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
@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
@ -110,6 +133,7 @@ module Manifique
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

View File

@ -6,6 +6,7 @@
<meta name="description" content="A friendly place for tooting">
<link href='/favicon.ico' rel='icon' type='image/x-icon'>
<link href='/apple-touch-icon.png' rel='apple-touch-icon' sizes='180x180'>
<link href='/apple-touch-icon-57px.png' rel='apple-touch-icon' sizes='57x57'>
<link color='#2b90d9' href='/mask-icon.svg' rel='mask-icon'>
<meta content='/browserconfig.xml' name='msapplication-config'>
<meta content='#282c37' name='theme-color'>

View File

@ -160,16 +160,31 @@ RSpec.describe Manifique::WebClient do
subject { web_client.fetch_metadata }
it "returns a metadata object with the HTML properties loaded" do
it "returns a metadata object" do
expect(subject).to be_kind_of(Manifique::Metadata)
end
it "loads properties from parsed HTML" do
expect(subject.name).to eq("kosmos.social")
expect(subject.description).to eq("A friendly place for tooting")
expect(subject.theme_color).to eq("#282c37")
expect(subject.display).to eq("standalone")
end
it "loads icons from link[rel=icon] elements" do
png_icons = subject.icons.select{|i| i["type"] == "image/png"}
expect(png_icons.length).to eq(5)
expect(png_icons.length).to eq(7)
expect(subject.icons.find{|i| i["sizes"] == "512x512"}["src"]).to eq( "/application_icon_x512.png")
end
it "loads icons from link[rel=apple-touch-icon] elements" do
apple_touch_icons = subject.icons.select{|i| i["purpose"] == "apple-touch-icon"}
expect(apple_touch_icons.length).to eq(2)
expect(apple_touch_icons.first["type"]).to eq("image/png")
expect(apple_touch_icons.first["sizes"]).to eq("180x180")
end
it "loads mask icons from link[rel=mask-icon] elements" do
mask_icon = subject.icons.find{|i| i["purpose"] == "mask-icon"}
expect(mask_icon["color"]).to eq("#2b90d9")
expect(mask_icon["type"]).to eq("image/svg")