Parse icons from HTML
This commit is contained in:
parent
08bdd0a18c
commit
7be7e45493
@ -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
|
||||
|
5
spec/fixtures/mastodon-no-manifest.html
vendored
5
spec/fixtures/mastodon-no-manifest.html
vendored
File diff suppressed because one or more lines are too long
@ -166,10 +166,14 @@ RSpec.describe Manifique::WebClient do
|
||||
expect(subject.description).to eq("A friendly place for tooting")
|
||||
expect(subject.theme_color).to eq("#282c37")
|
||||
expect(subject.display).to eq("standalone")
|
||||
|
||||
png_icons = subject.icons.select{|i| i["type"] == "image/png"}
|
||||
expect(png_icons.length).to eq(5)
|
||||
expect(subject.icons.find{|i| i["sizes"] == "512x512"}["src"]).to eq( "/application_icon_x512.png")
|
||||
end
|
||||
|
||||
it "knows which properties were loaded from HTML" do
|
||||
%w{ name description theme_color display }.each do |property|
|
||||
%w{ name description theme_color display icons }.each do |property|
|
||||
expect(subject.from_html).to include(property)
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user