Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion app/services/metadata_json_ld_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions spec/services/metadata_json_ld_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down