Refactor metadata object and loading, parse HTML

This commit is contained in:
basti committed 2018-06-29 16:58:26 -07:00
1 parent b05cad0f3f
commit 39ae311acb
7 files changed
+81 -26

No files matched your search

+5 -3
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
+17 -6
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
+1
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'>
+7 -7
View File
@@ -1,11 +1,11 @@
<!DOCTYPE html>
<!doctype html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<meta content='width=device-width, initial-scale=1' name='viewport'>
<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'>
<link color='#2b90d9' href='/mask-icon.svg' rel='mask-icon'>
<link href='/mastodon-web-app-manifest.json' rel='manifest'>
<meta content='/browserconfig.xml' name='msapplication-config'>
<meta content='#282c37' name='theme-color'>
@@ -16,21 +16,21 @@
<script src="/packs/common-1b7201293887a2c2d284.js" crossorigin="anonymous"></script>
<script src="/packs/locale_en-c8065c7bb76863454ac6.js" crossorigin="anonymous"></script>
<meta name="csrf-param" content="authenticity_token" />
<meta name="csrf-token" content="iCGf+8Jx0lYahbQk2Peua3fJirHNds/gauxKcUniH+xw/KRyrdnJc8rQcR2qQKd4BPfCgJJ7W3yYAMA3Lt5q9g==" />
<meta name="csrf-token" content="icgf+8jx0lyahbqk2peua3fjirhnds/gauxkcunih+xw/kryrdnjc8rqcr2qqkd4bpfcgjj7w3yyama3lt5q9g==" />
<link rel="preload" href="/packs/features/getting_started-1dd5d55a2eb78554be5f.js" as="script" type="text/javascript" crossorigin="anonymous">
<link rel="preload" href="/packs/features/compose-87d8eb2c08790e37389f.js" as="script" type="text/javascript" crossorigin="anonymous">
<link rel="preload" href="/packs/features/home_timeline-09ae9683dfa755543815.js" as="script" type="text/javascript" crossorigin="anonymous">
<link rel="preload" href="/packs/features/notifications-070ca1d5be465b079719.js" as="script" type="text/javascript" crossorigin="anonymous">
<meta content='BLWQ_9tiCpcja4_uJs5avHCm6SUlJMbwcybgIPkJjYzfkdiZ88HpJ1mLNJ26j7Aj0XZd87qWrn0exxplpenSTSc=' name='applicationServerKey'>
<script id='initial-state' type='application/json'>{"meta":{"streaming_api_base_url":"wss://kosmos.social","access_token":"34e249632c89759445fc6a1dc7153a22a0d693af3c3c7ab51fa53b1cfe1fd3a7","locale":"en","domain":"kosmos.social","admin":"8","search_enabled":false,"invites_enabled":false,"me":"2","unfollow_modal":false,"boost_modal":false,"delete_modal":true,"auto_play_gif":false,"display_sensitive_media":false,"reduce_motion":false},"compose":{"me":"2","default_privacy":"public","default_sensitive":false},"accounts":{"2":{"id":"2","username":"raucao","acct":"raucao","display_name":"Râu Cao","locked":false,"bot":false,"created_at":"2017-04-06T18:16:03.212Z","note":"\u003cp\u003eMaking web apps awesome with 5apps. Redecentralizing the Web with \u003cspan class=\"h-card\"\u003e\u003ca href=\"https://kosmos.social/@remotestorage\" class=\"u-url mention\"\u003e@\u003cspan\u003eremotestorage\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e and \u003cspan class=\"h-card\"\u003e\u003ca href=\"https://kosmos.social/@kosmos\" class=\"u-url mention\"\u003e@\u003cspan\u003ekosmos\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e. Evading winters with \u003cspan class=\"h-card\"\u003e\u003ca href=\"https://kosmos.social/@hackerbeach\" class=\"u-url mention\"\u003e@\u003cspan\u003ehackerbeach\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e.\u003c/p\u003e","url":"https://kosmos.social/@raucao","avatar":"https://s3-eu-west-1.amazonaws.com/kosmos-social/accounts/avatars/000/000/002/original/571dbe497dc5399a.png","avatar_static":"https://s3-eu-west-1.amazonaws.com/kosmos-social/accounts/avatars/000/000/002/original/571dbe497dc5399a.png","header":"https://s3-eu-west-1.amazonaws.com/kosmos-social/accounts/headers/000/000/002/original/545dc9b61e7c288b.jpg","header_static":"https://s3-eu-west-1.amazonaws.com/kosmos-social/accounts/headers/000/000/002/original/545dc9b61e7c288b.jpg","followers_count":327,"following_count":306,"statuses_count":2090,"emojis":[],"fields":[{"name":"Home page","value":"\u003ca href=\"https://sebastian.kip.pe\" rel=\"me nofollow noopener\" target=\"_blank\"\u003e\u003cspan class=\"invisible\"\u003ehttps://\u003c/span\u003e\u003cspan class=\"\"\u003esebastian.kip.pe\u003c/span\u003e\u003cspan class=\"invisible\"\u003e\u003c/span\u003e\u003c/a\u003e"},{"name":"Toot origin","value":"\u003ca href=\"https://updates.kip.pe\" rel=\"me nofollow noopener\" target=\"_blank\"\u003e\u003cspan class=\"invisible\"\u003ehttps://\u003c/span\u003e\u003cspan class=\"\"\u003eupdates.kip.pe\u003c/span\u003e\u003cspan class=\"invisible\"\u003e\u003c/span\u003e\u003c/a\u003e"}]},"8":{"id":"8","username":"kosmos","acct":"kosmos","display_name":"Kosmos Team","locked":false,"bot":false,"created_at":"2017-04-06T19:45:13.620Z","note":"\u003cp\u003eThe people behind \u003ca href=\"https://kosmos.org\" rel=\"nofollow noopener\" target=\"_blank\"\u003e\u003cspan class=\"invisible\"\u003ehttps://\u003c/span\u003e\u003cspan class=\"\"\u003ekosmos.org\u003c/span\u003e\u003cspan class=\"invisible\"\u003e\u003c/span\u003e\u003c/a\u003e and \u003ca href=\"https://kosmos.social\" rel=\"nofollow noopener\" target=\"_blank\"\u003e\u003cspan class=\"invisible\"\u003ehttps://\u003c/span\u003e\u003cspan class=\"\"\u003ekosmos.social\u003c/span\u003e\u003cspan class=\"invisible\"\u003e\u003c/span\u003e\u003c/a\u003e\u003c/p\u003e","url":"https://kosmos.social/@kosmos","avatar":"https://s3-eu-west-1.amazonaws.com/kosmos-social/accounts/avatars/000/000/008/original/1ba9744718783391.png","avatar_static":"https://s3-eu-west-1.amazonaws.com/kosmos-social/accounts/avatars/000/000/008/original/1ba9744718783391.png","header":"https://s3-eu-west-1.amazonaws.com/kosmos-social/accounts/headers/000/000/008/original/cca12d52b4c66532.jpg","header_static":"https://s3-eu-west-1.amazonaws.com/kosmos-social/accounts/headers/000/000/008/original/cca12d52b4c66532.jpg","followers_count":59,"following_count":9,"statuses_count":54,"emojis":[],"fields":[]}},"media_attachments":{"accept_content_types":[".jpg",".jpeg",".png",".gif",".webm",".mp4",".m4v","image/jpeg","image/png","image/gif","video/webm","video/mp4"]},"settings":{"frequentlyUsedEmojis":{"blush":1,"confused":1,"the_horns":1,"beers":1,"grinning":1,"heart":2,"zap":1,"point_up":1,"thinking_face":8,"upside_down_face":1,"fearful":1,"point_right":1,"wink":4,"yum":1,"grimacing":1,"ok_hand":2,"smiling_imp":1,"airplane":1,"face_with_rolling_eyes":9,"rocket":3,"money_mouth_face":1,"joy":2,"clap":1,"wave":1,"pray":2,"computer":1,"sweat_smile":2,"sunglasses":2,"clown_face":2},"onboarded":true,"notifications":{"alerts":{"follow":true,"favourite":true,"reblog":true,"mention":true},"shows":{"follow":true,"favourite":true,"reblog":true,"mention":true},"sounds":{"follow":true,"favourite":true,"reblog":true,"mention":true}},"public":{"regex":{"body":""}},"direct":{"regex":{"body":""}},"community":{"regex":{"body":"@twitter.com"}},"skinTone":1,"trends":{"show":true},"columns":[{"id":"COMPOSE","uuid":"9092b61a-015e-49b8-a511Line truncated
<meta content='blwq_9ticpcja4_ujs5avhcm6suljmbwcybgipkjjyzfkdiz88hpj1mlnj26j7aj0xzd87qwrn0exxplpenstsc=' name='applicationserverkey'>
<script id='initial-state' type='application/json'>{"meta":{"streaming_api_base_url":"wss://kosmos.social","access_token":"34e249632c89759445fc6a1dc7153a22a0d693af3c3c7ab51fa53b1cfe1fd3a7","locale":"en","domain":"kosmos.social","admin":"8","search_enabled":false,"invites_enabled":false,"me":"2","unfollow_modal":false,"boost_modal":false,"delete_modal":true,"auto_play_gif":false,"display_sensitive_media":false,"reduce_motion":false},"compose":{"me":"2","default_privacy":"public","default_sensitive":false},"accounts":{"2":{"id":"2","username":"raucao","acct":"raucao","display_name":"râu cao","locked":false,"bot":false,"created_at":"2017-04-06t18:16:03.212z","note":"\u003cp\u003emaking web apps awesome with 5apps. redecentralizing the web with \u003cspan class=\"h-card\"\u003e\u003ca href=\"https://kosmos.social/@remotestorage\" class=\"u-url mention\"\u003e@\u003cspan\u003eremotestorage\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e and \u003cspan class=\"h-card\"\u003e\u003ca href=\"https://kosmos.social/@kosmos\" class=\"u-url mention\"\u003e@\u003cspan\u003ekosmos\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e. evading winters with \u003cspan class=\"h-card\"\u003e\u003ca href=\"https://kosmos.social/@hackerbeach\" class=\"u-url mention\"\u003e@\u003cspan\u003ehackerbeach\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e.\u003c/p\u003e","url":"https://kosmos.social/@raucao","avatar":"https://s3-eu-west-1.amazonaws.com/kosmos-social/accounts/avatars/000/000/002/original/571dbe497dc5399a.png","avatar_static":"https://s3-eu-west-1.amazonaws.com/kosmos-social/accounts/avatars/000/000/002/original/571dbe497dc5399a.png","header":"https://s3-eu-west-1.amazonaws.com/kosmos-social/accounts/headers/000/000/002/original/545dc9b61e7c288b.jpg","header_static":"https://s3-eu-west-1.amazonaws.com/kosmos-social/accounts/headers/000/000/002/original/545dc9b61e7c288b.jpg","followers_count":327,"following_count":306,"statuses_count":2090,"emojis":[],"fields":[{"name":"home page","value":"\u003ca href=\"https://sebastian.kip.pe\" rel=\"me nofollow noopener\" target=\"_blank\"\u003e\u003cspan class=\"invisible\"\u003ehttps://\u003c/span\u003e\u003cspan class=\"\"\u003esebastian.kip.pe\u003c/span\u003e\u003cspan class=\"invisible\"\u003e\u003c/span\u003e\u003c/a\u003e"},{"name":"toot origin","value":"\u003ca href=\"https://updates.kip.pe\" rel=\"me nofollow noopener\" target=\"_blank\"\u003e\u003cspan class=\"invisible\"\u003ehttps://\u003c/span\u003e\u003cspan class=\"\"\u003eupdates.kip.pe\u003c/span\u003e\u003cspan class=\"invisible\"\u003e\u003c/span\u003e\u003c/a\u003e"}]},"8":{"id":"8","username":"kosmos","acct":"kosmos","display_name":"kosmos team","locked":false,"bot":false,"created_at":"2017-04-06t19:45:13.620z","note":"\u003cp\u003ethe people behind \u003ca href=\"https://kosmos.org\" rel=\"nofollow noopener\" target=\"_blank\"\u003e\u003cspan class=\"invisible\"\u003ehttps://\u003c/span\u003e\u003cspan class=\"\"\u003ekosmos.org\u003c/span\u003e\u003cspan class=\"invisible\"\u003e\u003c/span\u003e\u003c/a\u003e and \u003ca href=\"https://kosmos.social\" rel=\"nofollow noopener\" target=\"_blank\"\u003e\u003cspan class=\"invisible\"\u003ehttps://\u003c/span\u003e\u003cspan class=\"\"\u003ekosmos.social\u003c/span\u003e\u003cspan class=\"invisible\"\u003e\u003c/span\u003e\u003c/a\u003e\u003c/p\u003e","url":"https://kosmos.social/@kosmos","avatar":"https://s3-eu-west-1.amazonaws.com/kosmos-social/accounts/avatars/000/000/008/original/1ba9744718783391.png","avatar_static":"https://s3-eu-west-1.amazonaws.com/kosmos-social/accounts/avatars/000/000/008/original/1ba9744718783391.png","header":"https://s3-eu-west-1.amazonaws.com/kosmos-social/accounts/headers/000/000/008/original/cca12d52b4c66532.jpg","header_static":"https://s3-eu-west-1.amazonaws.com/kosmos-social/accounts/headers/000/000/008/original/cca12d52b4c66532.jpg","followers_count":59,"following_count":9,"statuses_count":54,"emojis":[],"fields":[]}},"media_attachments":{"accept_content_types":[".jpg",".jpeg",".png",".gif",".webm",".mp4",".m4v","image/jpeg","image/png","image/gif","video/webm","video/mp4"]},"settings":{"frequentlyusedemojis":{"blush":1,"confused":1,"the_horns":1,"beers":1,"grinning":1,"heart":2,"zap":1,"point_up":1,"thinking_face":8,"upside_down_face":1,"fearful":1,"point_right":1,"wink":4,"yum":1,"grimacing":1,"ok_hand":2,"smiling_imp":1,"airplane":1,"face_with_rolling_eyes":9,"rocket":3,"money_mouth_face":1,"joy":2,"clap":1,"wave":1,"pray":2,"computer":1,"sweat_smile":2,"sunglasses":2,"clown_face":2},"onboarded":true,"notifications":{"alerts":{"follow":true,"favourite":true,"reblog":true,"mention":true},"shows":{"follow":true,"favourite":true,"reblog":true,"mention":true},"sounds":{"follow":true,"favourite":true,"reblog":true,"mention":true}},"public":{"regex":{"body":""}},"direct":{"regex":{"body":""}},"community":{"regex":{"body":"@twitter.com"}},"skintone":1,"trends":{"show":true},"columns":[{"id":"compose","uuid":"9092b61a-015e-49b8-a511Line truncated
<script src="/packs/application-485a7a7977f02013f2fa.js" crossorigin="anonymous"></script>
</head>
<body class='app-body no-reduce-motion'>
<div class='app-holder' data-props='{&quot;locale&quot;:&quot;en&quot;}' id='mastodon'>
<noscript>
<img alt="Mastodon" src="/packs/logo-fe5141d38a25f50068b4c69b77ca1ec8.svg" />
<img alt="mastodon" src="/packs/logo-fe5141d38a25f50068b4c69b77ca1ec8.svg" />
<div>
To use the Mastodon web application, please enable JavaScript. Alternatively, try one of the <a href="https://github.com/tootsuite/documentation/blob/master/Using-Mastodon/Apps.md">native apps</a> for Mastodon for your platform.
to use the mastodon web application, please enable javascript. alternatively, try one of the <a href="https://github.com/tootsuite/documentation/blob/master/using-mastodon/apps.md">native apps</a> for mastodon for your platform.
</div>
</noscript>
</div>
+39
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>
+1 -1
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
+11 -9
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