From b05cad0f3f30d6c3221881758c8ee96fffafe13f Mon Sep 17 00:00:00 2001 From: Sebastian Kippe Date: Fri, 29 Jun 2018 15:21:28 -0700 Subject: [PATCH] Store manifest props as metadata props --- lib/manifique/metadata.rb | 20 +++++++++++++---- lib/manifique/web_client.rb | 8 +++---- spec/manifique/metadata_spec.rb | 36 ++++++++++++++++++++++++++++++- spec/manifique/web_client_spec.rb | 25 +++++++++++++++++++-- 4 files changed, 78 insertions(+), 11 deletions(-) diff --git a/lib/manifique/metadata.rb b/lib/manifique/metadata.rb index 3b537f5..3e82443 100644 --- a/lib/manifique/metadata.rb +++ b/lib/manifique/metadata.rb @@ -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 diff --git a/lib/manifique/web_client.rb b/lib/manifique/web_client.rb index ec22f57..78e6d8b 100644 --- a/lib/manifique/web_client.rb +++ b/lib/manifique/web_client.rb @@ -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 diff --git a/spec/manifique/metadata_spec.rb b/spec/manifique/metadata_spec.rb index c20465e..cf189ed 100644 --- a/spec/manifique/metadata_spec.rb +++ b/spec/manifique/metadata_spec.rb @@ -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 diff --git a/spec/manifique/web_client_spec.rb b/spec/manifique/web_client_spec.rb index dcc896d..b73ad48 100644 --- a/spec/manifique/web_client_spec.rb +++ b/spec/manifique/web_client_spec.rb @@ -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