Refactor metadata object and loading, parse HTML

This commit is contained in:
2018-06-29 16:58:26 -07:00
parent b05cad0f3f
commit 39ae311acb
7 changed files with 81 additions and 26 deletions

View File

@@ -1,22 +1,24 @@
module Manifique
class Metadata
attr_accessor :url, :src_web_manifest, :src_html_data,
attr_accessor :url, :from_web_manifest, :from_html,
:name, :short_name, :description, :icons,
:theme_color, :background_color, :display,
:start_url, :scope, :share_target
def initialize(data={})
self.url = data[:url]
self.from_web_manifest = []
self.from_html = []
end
def web_manifest=(manifest)
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|
self.send("#{prop}=", manifest[prop]) if manifest[prop]
self.from_web_manifest.push(prop)
end
self.src_web_manifest = manifest
end
def to_json

View File

@@ -16,12 +16,10 @@ module Manifique
def fetch_metadata
fetch_website
fetch_web_manifest
if @metadata.web_manifest
return @metadata
if manifest = fetch_web_manifest
@metadata.load_from_web_manifest(manifest)
else
#TODO assemble from HTML elements
parse_metadata_from_html
end
@metadata
@@ -44,11 +42,24 @@ module Manifique
res = do_get_request manifest_url
@metadata.web_manifest = JSON.parse(res.body) rescue false
JSON.parse(res.body) rescue false
end
private
def parse_metadata_from_html
if title = @html.at_css("title") and !title.text.empty?
@metadata.name = title.text
@metadata.from_html.push "name"
end
# TODO extract meta element parsing to seperate method
if desc = @html.at_css("meta[name=description]") and
!desc.attributes["content"].value.empty?
@metadata.description = desc.attributes["content"].value
@metadata.from_html.push "description"
end
end
def do_get_request(url)
conn = Faraday.new do |b|
b.use FaradayMiddleware::FollowRedirects