diff --git a/app/services/metadata_json_ld_parser.rb b/app/services/metadata_json_ld_parser.rb index 363c31a..42f3966 100644 --- a/app/services/metadata_json_ld_parser.rb +++ b/app/services/metadata_json_ld_parser.rb @@ -70,7 +70,13 @@ def content_key_value(rating_metadata_content) def type_values(values) values = val_or_first_item(values) - [val_or_first_item(values["@type"]), values] + [type_key(values["@type"]), values] + end + + # @type is sometimes an array of types (e.g. ["Person", "Organization"]) + def type_key(type) + return type unless type.is_a?(Array) + ((KEY_PRIORITY + PUBLISHER_KEY_PRIORITY) & type).first || type.first end # IDK why they wrap some values in arrays! Just deal with it diff --git a/spec/services/metadata_json_ld_parser_spec.rb b/spec/services/metadata_json_ld_parser_spec.rb index e11d861..7e87007 100644 --- a/spec/services/metadata_json_ld_parser_spec.rb +++ b/spec/services/metadata_json_ld_parser_spec.rb @@ -150,6 +150,21 @@ end end end + context "array of @types" do + let(:values) do + [ + {"url" => "https://www.example.com", "@type" => ["WebSite"]}, + {"name" => "Jane Doe", "@type" => ["Person", "Organization"]}, + {"name" => "John Doe", "@type" => ["Person", "Unknownthing"]} + ] + end + let(:target) { %w[WebSite Organization Person].zip(values).to_h } + it "uses the prioritized type" do + expect(subject.content_hash(rating_metadata)).to eq target + + expect(subject.parse(rating_metadata)).to eq(values.first.merge("@type" => "WebSite", "publisher" => "Jane Doe")) + end + end context "more dataexample" do let(:values) { [{"url" => "https://example.com", "@type" => "NewsArticle", "image" => {"url" => "https://example.com/image.png", "@type" => "ImageObject", "width" => 2057, "height" => 1200}, "author" => ["John Doe"], "creator" => ["John Doe"], "hasPart" => [], "@context" => "http://schema.org", "headline" => "example title", "keywords" => ["topic: Cool Matters"]}, {"@type" => "BreadcrumbList", "@context" => "https://schema.org/"}] } let(:target) do