Store manifest props as metadata props

This commit is contained in:
Basti 2018-06-29 15:21:28 -07:00
parent 4c0d40bd34
commit b05cad0f3f
4 changed files with 78 additions and 11 deletions

View File

@ -1,10 +1,22 @@
require 'ostruct'
module Manifique
class Metadata
attr_accessor :manifest
def initialize
attr_accessor :url, :src_web_manifest, :src_html_data,
:name, :short_name, :description, :icons,
:theme_color, :background_color, :display,
:start_url, :scope, :share_target
def initialize(data={})
self.url = data[:url]
end
def 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]
end
self.src_web_manifest = manifest
end
def to_json

View File

@ -11,14 +11,14 @@ module Manifique
def initialize(options={})
@options = options
@url = options[:url]
@metadata = Metadata.new
@metadata = Metadata.new(url: @url)
end
def fetch_metadata
fetch_website
manifest = fetch_web_manifest
fetch_web_manifest
if @metadata.manifest = manifest
if @metadata.web_manifest
return @metadata
else
#TODO assemble from HTML elements
@ -44,7 +44,7 @@ module Manifique
res = do_get_request manifest_url
JSON.parse(res.body) rescue false
@metadata.web_manifest = JSON.parse(res.body) rescue false
end
private

View File

@ -2,6 +2,40 @@ require "spec_helper"
require "manifique/metadata"
RSpec.describe Manifique::Metadata do
# RSpec.describe "Manifique::MetaData" do
describe "#initialize" do
it "sets the URL when given" do
metadata = Manifique::Metadata.new(url: "https://5apps.com")
expect(metadata.url).to eq("https://5apps.com")
end
end
describe "#manifest=" do
let(:metadata) { Manifique::Metadata.new }
let(:manifest) { JSON.parse(File.read(File.join(__dir__, "..", "fixtures", "mastodon-web-app-manifest.json"))) }
before do
metadata.web_manifest = manifest
end
it "stores the manifest properties as metadata object properties" do
expect(metadata.name).to eq("kosmos.social")
expect(metadata.short_name).to eq("kosmos.social")
expect(metadata.description).to eq("A friendly place for tooting. Run by the Kosmos peeps.")
expect(metadata.theme_color).to eq("#282c37")
expect(metadata.background_color).to eq("#191b22")
expect(metadata.display).to eq("standalone")
expect(metadata.start_url).to eq("/web/timelines/home")
expect(metadata.scope).to eq("https://kosmos.social/")
expect(metadata.share_target).to eq({ "url_template" => "share?title={title}&text={text}&url={url}"})
expect(metadata.icons).to eq([
{
"src"=>"/android-chrome-192x192.png",
"sizes"=>"192x192",
"type"=>"image/png"
}
])
end
end
end

View File

@ -144,8 +144,29 @@ RSpec.describe Manifique::WebClient do
end
it "stores the web app manifest data" do
expect(subject.manifest).to be_kind_of(Hash)
expect(subject.manifest["name"]).to eq("kosmos.social")
expect(subject.web_manifest).to be_kind_of(Hash)
expect(subject.web_manifest["name"]).to eq("kosmos.social")
end
end
context "no web app manifest present" do
before do
index_html = File.read(File.join(__dir__, "..", "fixtures", "mastodon-no-manifest.html"));
stub_request(:get, "https://kosmos.social/").
to_return(body: index_html, status: 200, headers: {
"Content-Type": "text/html; charset=utf-8"
})
end
subject { web_client.fetch_metadata }
it "returns a metadata object" do
expect(subject).to be_kind_of(Manifique::Metadata)
end
it "parses and stores metadata from HTML" do
pending
# expect(subject.html).to be_kind_of(Hash)
end
end
end