mirror of
https://github.com/discourse/discourse.git
synced 2024-11-24 08:56:36 +08:00
f723b4c322
A followup to #17007
39 lines
823 B
Ruby
39 lines
823 B
Ruby
# frozen_string_literal: true
|
|
|
|
module Onebox
|
|
class JsonLd < Normalizer
|
|
# Full schema.org hierarchy can be found here: https://schema.org/docs/full.html
|
|
MOVIE_JSON_LD_TYPE = "Movie"
|
|
|
|
def initialize(doc)
|
|
@data = extract(doc)
|
|
end
|
|
|
|
private
|
|
|
|
def extract(doc)
|
|
return {} if Onebox::Helpers::blank?(doc)
|
|
|
|
doc.css('script[type="application/ld+json"]').each do |element|
|
|
parsed_json = parse_json(element.text)
|
|
|
|
case parsed_json["@type"]
|
|
when MOVIE_JSON_LD_TYPE
|
|
return Onebox::Movie.new(parsed_json).to_h
|
|
end
|
|
end
|
|
|
|
{}
|
|
end
|
|
|
|
def parse_json(json)
|
|
begin
|
|
JSON[json]
|
|
rescue JSON::ParserError => e
|
|
Discourse.warn_exception(e, message: "Error parsing JSON-LD: #{json}")
|
|
{}
|
|
end
|
|
end
|
|
end
|
|
end
|