diff --git a/lib/manifique/metadata.rb b/lib/manifique/metadata.rb index 3e82443..5757628 100644 --- a/lib/manifique/metadata.rb +++ b/lib/manifique/metadata.rb @@ -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 diff --git a/lib/manifique/web_client.rb b/lib/manifique/web_client.rb index 78e6d8b..7b33f46 100644 --- a/lib/manifique/web_client.rb +++ b/lib/manifique/web_client.rb @@ -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 diff --git a/spec/fixtures/mastodon-no-manifest.html b/spec/fixtures/mastodon-no-manifest.html index 3c576d3..37be55d 100644 --- a/spec/fixtures/mastodon-no-manifest.html +++ b/spec/fixtures/mastodon-no-manifest.html @@ -3,6 +3,7 @@ + diff --git a/spec/fixtures/mastodon.html b/spec/fixtures/mastodon.html index a2e3631..0225c83 100644 --- a/spec/fixtures/mastodon.html +++ b/spec/fixtures/mastodon.html @@ -1,11 +1,11 @@ - + - + @@ -16,21 +16,21 @@ - + - - + +
diff --git a/spec/fixtures/sharesome.html b/spec/fixtures/sharesome.html new file mode 100644 index 0000000..5cfaefe --- /dev/null +++ b/spec/fixtures/sharesome.html @@ -0,0 +1,39 @@ + + + + + + Sharesome + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spec/manifique/metadata_spec.rb b/spec/manifique/metadata_spec.rb index cf189ed..1267ea8 100644 --- a/spec/manifique/metadata_spec.rb +++ b/spec/manifique/metadata_spec.rb @@ -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 diff --git a/spec/manifique/web_client_spec.rb b/spec/manifique/web_client_spec.rb index b73ad48..d1e2419 100644 --- a/spec/manifique/web_client_spec.rb +++ b/spec/manifique/web_client_spec.rb @@ -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