Refactor metadata object and loading, parse HTML
This commit is contained in:
parent
b05cad0f3f
commit
39ae311acb
@ -1,22 +1,24 @@
|
|||||||
module Manifique
|
module Manifique
|
||||||
class Metadata
|
class Metadata
|
||||||
|
|
||||||
attr_accessor :url, :src_web_manifest, :src_html_data,
|
attr_accessor :url, :from_web_manifest, :from_html,
|
||||||
:name, :short_name, :description, :icons,
|
:name, :short_name, :description, :icons,
|
||||||
:theme_color, :background_color, :display,
|
:theme_color, :background_color, :display,
|
||||||
:start_url, :scope, :share_target
|
:start_url, :scope, :share_target
|
||||||
|
|
||||||
def initialize(data={})
|
def initialize(data={})
|
||||||
self.url = data[:url]
|
self.url = data[:url]
|
||||||
|
self.from_web_manifest = []
|
||||||
|
self.from_html = []
|
||||||
end
|
end
|
||||||
|
|
||||||
def web_manifest=(manifest)
|
def load_from_web_manifest(manifest)
|
||||||
[:name, :short_name, :description, :icons,
|
[:name, :short_name, :description, :icons,
|
||||||
:theme_color, :background_color, :display,
|
:theme_color, :background_color, :display,
|
||||||
:start_url, :scope, :share_target].map(&:to_s).each do |prop|
|
:start_url, :scope, :share_target].map(&:to_s).each do |prop|
|
||||||
self.send("#{prop}=", manifest[prop]) if manifest[prop]
|
self.send("#{prop}=", manifest[prop]) if manifest[prop]
|
||||||
|
self.from_web_manifest.push(prop)
|
||||||
end
|
end
|
||||||
self.src_web_manifest = manifest
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_json
|
def to_json
|
||||||
|
@ -16,12 +16,10 @@ module Manifique
|
|||||||
|
|
||||||
def fetch_metadata
|
def fetch_metadata
|
||||||
fetch_website
|
fetch_website
|
||||||
fetch_web_manifest
|
if manifest = fetch_web_manifest
|
||||||
|
@metadata.load_from_web_manifest(manifest)
|
||||||
if @metadata.web_manifest
|
|
||||||
return @metadata
|
|
||||||
else
|
else
|
||||||
#TODO assemble from HTML elements
|
parse_metadata_from_html
|
||||||
end
|
end
|
||||||
|
|
||||||
@metadata
|
@metadata
|
||||||
@ -44,11 +42,24 @@ module Manifique
|
|||||||
|
|
||||||
res = do_get_request manifest_url
|
res = do_get_request manifest_url
|
||||||
|
|
||||||
@metadata.web_manifest = JSON.parse(res.body) rescue false
|
JSON.parse(res.body) rescue false
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
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)
|
def do_get_request(url)
|
||||||
conn = Faraday.new do |b|
|
conn = Faraday.new do |b|
|
||||||
b.use FaradayMiddleware::FollowRedirects
|
b.use FaradayMiddleware::FollowRedirects
|
||||||
|
1
spec/fixtures/mastodon-no-manifest.html
vendored
1
spec/fixtures/mastodon-no-manifest.html
vendored
@ -3,6 +3,7 @@
|
|||||||
<head>
|
<head>
|
||||||
<meta charset='utf-8'>
|
<meta charset='utf-8'>
|
||||||
<meta content='width=device-width, initial-scale=1' name='viewport'>
|
<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='/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.png' rel='apple-touch-icon' sizes='180x180'>
|
||||||
<link color='#2b90d9' href='/mask-icon.svg' rel='mask-icon'>
|
<link color='#2b90d9' href='/mask-icon.svg' rel='mask-icon'>
|
||||||
|
14
spec/fixtures/mastodon.html
vendored
14
spec/fixtures/mastodon.html
vendored
File diff suppressed because one or more lines are too long
39
spec/fixtures/sharesome.html
vendored
Normal file
39
spec/fixtures/sharesome.html
vendored
Normal 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>
|
@ -15,7 +15,7 @@ RSpec.describe Manifique::Metadata do
|
|||||||
let(:manifest) { JSON.parse(File.read(File.join(__dir__, "..", "fixtures", "mastodon-web-app-manifest.json"))) }
|
let(:manifest) { JSON.parse(File.read(File.join(__dir__, "..", "fixtures", "mastodon-web-app-manifest.json"))) }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
metadata.web_manifest = manifest
|
metadata.load_from_web_manifest(manifest)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "stores the manifest properties as metadata object properties" do
|
it "stores the manifest properties as metadata object properties" do
|
||||||
|
@ -93,7 +93,7 @@ RSpec.describe Manifique::WebClient do
|
|||||||
web_client.fetch_web_manifest
|
web_client.fetch_web_manifest
|
||||||
end
|
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).to be_kind_of(Hash)
|
||||||
expect(subject["name"]).to eq("kosmos.social")
|
expect(subject["name"]).to eq("kosmos.social")
|
||||||
end
|
end
|
||||||
@ -139,13 +139,13 @@ RSpec.describe Manifique::WebClient do
|
|||||||
|
|
||||||
subject { web_client.fetch_metadata }
|
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).to be_kind_of(Manifique::Metadata)
|
||||||
|
expect(subject.name).to eq("kosmos.social")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "stores the web app manifest data" do
|
it "knows which properties were loaded from the web app manifest" do
|
||||||
expect(subject.web_manifest).to be_kind_of(Hash)
|
expect(subject.from_web_manifest.length).to eq(10)
|
||||||
expect(subject.web_manifest["name"]).to eq("kosmos.social")
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -160,13 +160,15 @@ RSpec.describe Manifique::WebClient do
|
|||||||
|
|
||||||
subject { web_client.fetch_metadata }
|
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).to be_kind_of(Manifique::Metadata)
|
||||||
|
expect(subject.name).to eq("kosmos.social")
|
||||||
|
expect(subject.description).to eq("A friendly place for tooting")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "parses and stores metadata from HTML" do
|
it "knows which properties were loaded from HTML" do
|
||||||
pending
|
expect(subject.from_html).to include("name")
|
||||||
# expect(subject.html).to be_kind_of(Hash)
|
expect(subject.from_html).to include("description")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user