FIX: Use first supported type item when JSON-LD returns array (#17217)

This commit is contained in:
Penar Musaraj
2022-06-23 17:02:01 +00:00
committed by GitHub
parent 20e34b5da6
commit 3baefa25b5
2 changed files with 27 additions and 0 deletions

View File

@ -4,6 +4,7 @@ module Onebox
class JsonLd < Normalizer
# Full schema.org hierarchy can be found here: https://schema.org/docs/full.html
MOVIE_JSON_LD_TYPE = "Movie"
SUPPORTED_TYPES = [MOVIE_JSON_LD_TYPE]
def initialize(doc)
@data = extract(doc)
@ -17,6 +18,11 @@ module Onebox
doc.css('script[type="application/ld+json"]').each do |element|
parsed_json = parse_json(element.text)
if parsed_json.kind_of?(Array)
parsed_json = parsed_json.detect { |x| SUPPORTED_TYPES.include?(x["@type"]) }
return {} if !parsed_json
end
case parsed_json["@type"]
when MOVIE_JSON_LD_TYPE
return Onebox::Movie.new(parsed_json).to_h