Refactor metadata object and loading, parse HTML

This commit is contained in:
Basti 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

View File

@ -3,6 +3,7 @@
<head>
<meta charset='utf-8'>
<meta content='width=device-width, initial-scale=1' name='viewport'>
<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 color='#2b90d9' href='/mask-icon.svg' rel='mask-icon'>

File diff suppressed because one or more lines are too long

39
spec/fixtures/sharesome.html vendored Normal file
View File

@ -0,0 +1,39 @@
<!DOCTYPE html>
<html manifest="/cache.manifest">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Sharesome</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="/favicon.ico">
<link href='/apple-touch-icon.png' rel='apple-touch-icon' sizes='180x180'>
<meta name="sharesome/config/environment" content="%7B%22modulePrefix%22%3A%22sharesome%22%2C%22environment%22%3A%22production%22%2C%22rootURL%22%3A%22/%22%2C%22locationType%22%3A%22auto%22%2C%22EmberENV%22%3A%7B%22FEATURES%22%3A%7B%7D%2C%22EXTEND_PROTOTYPES%22%3A%7B%22Date%22%3Afalse%7D%7D%2C%22APP%22%3A%7B%22name%22%3A%22sharesome%22%2C%22version%22%3A%221.3.0+c1c87ebe%22%7D%2C%22exportApplicationGlobal%22%3Afalse%7D" />
<link integrity="" rel="stylesheet" href="/assets/vendor-44c509474e3bdca9b3b255c6bfc3c589.css">
<link integrity="" rel="stylesheet" href="/assets/sharesome-2e47c89428f64d9d5de9f18e97cb3964.css">
<meta property="og:type" content="website">
<meta property="og:site_name" content="Sharesome">
<meta property="og:title" content="Sharesome">
<meta property="og:image" content="https://sharesome.5apps.com/application_icon_x144.png">
<meta property="og:image:width" content="144">
<meta property="og:image:height" content="144">
<meta property="og:url" content="https://sharesome.5apps.com/">
<meta name="twitter:card" content="summary">
<meta name="mobile-web-app-capable" content="yes">
<link rel="icon" sizes="512x512" href="/application_icon_x512.png">
<link rel="icon" sizes="256x256" href="/application_icon_x256.png">
<link rel="icon" sizes="228x228" href="/application_icon_x228.png">
<link rel="icon" sizes="196x196" href="/application_icon_x196.png">
<link rel="icon" sizes="192x192" href="/application_icon_x192.png">
<link rel="icon" sizes="128x128" href="/application_icon_x128.png">
<meta name="application-name" content="Sharesome">
<meta name="msapplication-starturl" content="/">
<meta name="msapplication-TileImage" content="/application_icon_x144.png">
<meta name="msapplication-TileColor" content="#B22323">
<meta name="theme-color" content="#222222">
</head>
<body>
<script src="/assets/vendor-057958b38d910985153b341c38e7a4d7.js" integrity="sha256-mbJDOHfNu2UK6EuCzTZFsT6NmXKeU2EqjKzIPuWcUUI= sha512-wvFQpzwqiQVwEUjShUOc6FWErWEyyBOKWpU7uXPD2iCFExD7UT8pzIP1FnWXF5UgJiwHRm8Qd4/K8khm+vNvew==" ></script>
<script src="/assets/sharesome-678876be4a774f0d8b5a16f0431e60d0.js" integrity="sha256-HBRVkGfCC/CDqvw7kkO7S7h6gLUzuLrZ18NR9MVgNqI= sha512-Un4RxKDDEHH5BG2vt9oojWHVlNFWwm3qw+04zFE4yVHoh2U6hgkvcFwSC73YEWV4fK/okZR3cETkt+niwp6vOg==" ></script>
</body>
</html>

View File

@ -15,7 +15,7 @@ RSpec.describe Manifique::Metadata do
let(:manifest) { JSON.parse(File.read(File.join(__dir__, "..", "fixtures", "mastodon-web-app-manifest.json"))) }
before do
metadata.web_manifest = manifest
metadata.load_from_web_manifest(manifest)
end
it "stores the manifest properties as metadata object properties" do

View File

@ -93,7 +93,7 @@ RSpec.describe Manifique::WebClient do
web_client.fetch_web_manifest
end
it "fetches and returns the manifest" do
it "returns the fetched manifest as a hash" do
expect(subject).to be_kind_of(Hash)
expect(subject["name"]).to eq("kosmos.social")
end
@ -139,13 +139,13 @@ RSpec.describe Manifique::WebClient do
subject { web_client.fetch_metadata }
it "returns a metadata object" do
it "returns a metadata object with the manifest properties loaded" do
expect(subject).to be_kind_of(Manifique::Metadata)
expect(subject.name).to eq("kosmos.social")
end
it "stores the web app manifest data" do
expect(subject.web_manifest).to be_kind_of(Hash)
expect(subject.web_manifest["name"]).to eq("kosmos.social")
it "knows which properties were loaded from the web app manifest" do
expect(subject.from_web_manifest.length).to eq(10)
end
end
@ -160,13 +160,15 @@ RSpec.describe Manifique::WebClient do
subject { web_client.fetch_metadata }
it "returns a metadata object" do
it "returns a metadata object with the HTML properties loaded" do
expect(subject).to be_kind_of(Manifique::Metadata)
expect(subject.name).to eq("kosmos.social")
expect(subject.description).to eq("A friendly place for tooting")
end
it "parses and stores metadata from HTML" do
pending
# expect(subject.html).to be_kind_of(Hash)
it "knows which properties were loaded from HTML" do
expect(subject.from_html).to include("name")
expect(subject.from_html).to include("description")
end
end
end