From 08bdd0a18c21fb1c1da32e62dee4dca44c0f9e58 Mon Sep 17 00:00:00 2001 From: Sebastian Kippe Date: Mon, 2 Jul 2018 11:54:38 -0500 Subject: [PATCH] Extract more metadata from HTML And don't overwrite existing metadata properties --- lib/manifique/web_client.rb | 39 +++++++++++++++++++++++++++---- spec/manifique/web_client_spec.rb | 7 ++++-- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/lib/manifique/web_client.rb b/lib/manifique/web_client.rb index 7b33f46..f8b9156 100644 --- a/lib/manifique/web_client.rb +++ b/lib/manifique/web_client.rb @@ -48,15 +48,44 @@ module Manifique private def parse_metadata_from_html + parse_title_from_html + parse_meta_elements_from_html + parse_display_mode_from_html + end + + def parse_title_from_html + return if @metadata.name + 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 + + def parse_meta_elements_from_html + { + description: "description", + theme_color: "theme-color" + }.each do |prop, name| + next if @metadata.send("#{prop}") + if value = get_meta_element_value(name) + @metadata.send "#{prop}=", value + @metadata.from_html.push prop.to_s + end + end + end + + def parse_display_mode_from_html + return if @metadata.display + if get_meta_element_value("apple-mobile-web-app-capable") == "yes" + @metadata.display = "standalone" + @metadata.from_html.push "display" + end + end + + def get_meta_element_value(name) + if el = @html.at_css("meta[name=#{name}]") and !el.attributes["content"].value.empty? + el.attributes["content"].value end end diff --git a/spec/manifique/web_client_spec.rb b/spec/manifique/web_client_spec.rb index d1e2419..665c7fb 100644 --- a/spec/manifique/web_client_spec.rb +++ b/spec/manifique/web_client_spec.rb @@ -164,11 +164,14 @@ RSpec.describe Manifique::WebClient 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") + expect(subject.theme_color).to eq("#282c37") + expect(subject.display).to eq("standalone") end it "knows which properties were loaded from HTML" do - expect(subject.from_html).to include("name") - expect(subject.from_html).to include("description") + %w{ name description theme_color display }.each do |property| + expect(subject.from_html).to include(property) + end end end end