discourse/lib/onebox/json_ld.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

45 lines
1.0 KiB
Ruby
Raw Normal View History

Add support for JSON LD in Onebox (#17007) * FIX: Fix a bug that is accessing the values in a hash wrongly and write tests I decided to write tests in order to be confident in my refactor that's in the next commit. Meanwhile I have discovered a potential bug. The `title_attr` key was accessed as a string, but all the keys are actually symbols so it was never evaluated to be true. irb(main):025:0> d = {key: 'value'} => {:key=>"value"} irb(main):026:0> d['key'] => nil irb(main):027:0> d[:key] => "value" * DEV: Extract methods for readability I will be adding a new method following the conventions in place for adding a new normalizer. And this will make the readability of the `raw` block even more difficult; so I am extracting self contained private methods beforehand. * FEATURE: Parse JSON-LD and introduce Movie object JSON LD data is very easily transferable to Ruby objects because they contain types. If these types are mapped to Ruby objects, it is also better to make all the parsed data very explicit and easily extendable. JSON-LD has many more standardized item types, with a full list here: https://schema.org/docs/full.html However in order to decrease the scope, I only adapted the movie type. * DEV: Change inheritance between normalizers Normalizers are not supposed to have an inheritance relationships amongst each other. They are all normalizers, but all normalizing separate protocols. This is why I chose to extract a parent class and relieve Open Graph off that responsibility. Removing the parent class altogether could also a possibility, but I am keeping the scope limited to having a more accurate representation of the normalizers while making it easier to add a new one. * Lint changes * Bring back the Oembed OpenGraph inheritance There is one test that caught that this inheritance was necessary. I still think modelling wise this inheritance shouldn't exist, but this can be tackled separately. * Return empty hash if the json received is invalid Before this change if there was a parsing error with JSON it would throw an exception. The goal of this commit is to rescue that exception and then log a warning. I chose to use Discourse's logger wrapper `warn_exception` to have the backtrace and not just used Rails logger. I considered raising an `InvalidParameters` error however if the JSON here is invalid it should not block showing of the Onebox, so logging is enough. * Prep to support more JSONLD schema types with case * Extract mustache template object created from JSONLD
2022-06-13 11:32:34 -04:00
# 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"
SUPPORTED_TYPES = [MOVIE_JSON_LD_TYPE]
Add support for JSON LD in Onebox (#17007) * FIX: Fix a bug that is accessing the values in a hash wrongly and write tests I decided to write tests in order to be confident in my refactor that's in the next commit. Meanwhile I have discovered a potential bug. The `title_attr` key was accessed as a string, but all the keys are actually symbols so it was never evaluated to be true. irb(main):025:0> d = {key: 'value'} => {:key=>"value"} irb(main):026:0> d['key'] => nil irb(main):027:0> d[:key] => "value" * DEV: Extract methods for readability I will be adding a new method following the conventions in place for adding a new normalizer. And this will make the readability of the `raw` block even more difficult; so I am extracting self contained private methods beforehand. * FEATURE: Parse JSON-LD and introduce Movie object JSON LD data is very easily transferable to Ruby objects because they contain types. If these types are mapped to Ruby objects, it is also better to make all the parsed data very explicit and easily extendable. JSON-LD has many more standardized item types, with a full list here: https://schema.org/docs/full.html However in order to decrease the scope, I only adapted the movie type. * DEV: Change inheritance between normalizers Normalizers are not supposed to have an inheritance relationships amongst each other. They are all normalizers, but all normalizing separate protocols. This is why I chose to extract a parent class and relieve Open Graph off that responsibility. Removing the parent class altogether could also a possibility, but I am keeping the scope limited to having a more accurate representation of the normalizers while making it easier to add a new one. * Lint changes * Bring back the Oembed OpenGraph inheritance There is one test that caught that this inheritance was necessary. I still think modelling wise this inheritance shouldn't exist, but this can be tackled separately. * Return empty hash if the json received is invalid Before this change if there was a parsing error with JSON it would throw an exception. The goal of this commit is to rescue that exception and then log a warning. I chose to use Discourse's logger wrapper `warn_exception` to have the backtrace and not just used Rails logger. I considered raising an `InvalidParameters` error however if the JSON here is invalid it should not block showing of the Onebox, so logging is enough. * Prep to support more JSONLD schema types with case * Extract mustache template object created from JSONLD
2022-06-13 11:32:34 -04:00
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)
Add support for JSON LD in Onebox (#17007) * FIX: Fix a bug that is accessing the values in a hash wrongly and write tests I decided to write tests in order to be confident in my refactor that's in the next commit. Meanwhile I have discovered a potential bug. The `title_attr` key was accessed as a string, but all the keys are actually symbols so it was never evaluated to be true. irb(main):025:0> d = {key: 'value'} => {:key=>"value"} irb(main):026:0> d['key'] => nil irb(main):027:0> d[:key] => "value" * DEV: Extract methods for readability I will be adding a new method following the conventions in place for adding a new normalizer. And this will make the readability of the `raw` block even more difficult; so I am extracting self contained private methods beforehand. * FEATURE: Parse JSON-LD and introduce Movie object JSON LD data is very easily transferable to Ruby objects because they contain types. If these types are mapped to Ruby objects, it is also better to make all the parsed data very explicit and easily extendable. JSON-LD has many more standardized item types, with a full list here: https://schema.org/docs/full.html However in order to decrease the scope, I only adapted the movie type. * DEV: Change inheritance between normalizers Normalizers are not supposed to have an inheritance relationships amongst each other. They are all normalizers, but all normalizing separate protocols. This is why I chose to extract a parent class and relieve Open Graph off that responsibility. Removing the parent class altogether could also a possibility, but I am keeping the scope limited to having a more accurate representation of the normalizers while making it easier to add a new one. * Lint changes * Bring back the Oembed OpenGraph inheritance There is one test that caught that this inheritance was necessary. I still think modelling wise this inheritance shouldn't exist, but this can be tackled separately. * Return empty hash if the json received is invalid Before this change if there was a parsing error with JSON it would throw an exception. The goal of this commit is to rescue that exception and then log a warning. I chose to use Discourse's logger wrapper `warn_exception` to have the backtrace and not just used Rails logger. I considered raising an `InvalidParameters` error however if the JSON here is invalid it should not block showing of the Onebox, so logging is enough. * Prep to support more JSONLD schema types with case * Extract mustache template object created from JSONLD
2022-06-13 11:32:34 -04:00
if parsed_json.kind_of?(Array)
parsed_json = parsed_json.detect { |x| SUPPORTED_TYPES.include?(x["@type"]) }
return {} if !parsed_json
end
Add support for JSON LD in Onebox (#17007) * FIX: Fix a bug that is accessing the values in a hash wrongly and write tests I decided to write tests in order to be confident in my refactor that's in the next commit. Meanwhile I have discovered a potential bug. The `title_attr` key was accessed as a string, but all the keys are actually symbols so it was never evaluated to be true. irb(main):025:0> d = {key: 'value'} => {:key=>"value"} irb(main):026:0> d['key'] => nil irb(main):027:0> d[:key] => "value" * DEV: Extract methods for readability I will be adding a new method following the conventions in place for adding a new normalizer. And this will make the readability of the `raw` block even more difficult; so I am extracting self contained private methods beforehand. * FEATURE: Parse JSON-LD and introduce Movie object JSON LD data is very easily transferable to Ruby objects because they contain types. If these types are mapped to Ruby objects, it is also better to make all the parsed data very explicit and easily extendable. JSON-LD has many more standardized item types, with a full list here: https://schema.org/docs/full.html However in order to decrease the scope, I only adapted the movie type. * DEV: Change inheritance between normalizers Normalizers are not supposed to have an inheritance relationships amongst each other. They are all normalizers, but all normalizing separate protocols. This is why I chose to extract a parent class and relieve Open Graph off that responsibility. Removing the parent class altogether could also a possibility, but I am keeping the scope limited to having a more accurate representation of the normalizers while making it easier to add a new one. * Lint changes * Bring back the Oembed OpenGraph inheritance There is one test that caught that this inheritance was necessary. I still think modelling wise this inheritance shouldn't exist, but this can be tackled separately. * Return empty hash if the json received is invalid Before this change if there was a parsing error with JSON it would throw an exception. The goal of this commit is to rescue that exception and then log a warning. I chose to use Discourse's logger wrapper `warn_exception` to have the backtrace and not just used Rails logger. I considered raising an `InvalidParameters` error however if the JSON here is invalid it should not block showing of the Onebox, so logging is enough. * Prep to support more JSONLD schema types with case * Extract mustache template object created from JSONLD
2022-06-13 11:32:34 -04:00
case parsed_json["@type"]
when MOVIE_JSON_LD_TYPE
return Onebox::Movie.new(parsed_json).to_h
Add support for JSON LD in Onebox (#17007) * FIX: Fix a bug that is accessing the values in a hash wrongly and write tests I decided to write tests in order to be confident in my refactor that's in the next commit. Meanwhile I have discovered a potential bug. The `title_attr` key was accessed as a string, but all the keys are actually symbols so it was never evaluated to be true. irb(main):025:0> d = {key: 'value'} => {:key=>"value"} irb(main):026:0> d['key'] => nil irb(main):027:0> d[:key] => "value" * DEV: Extract methods for readability I will be adding a new method following the conventions in place for adding a new normalizer. And this will make the readability of the `raw` block even more difficult; so I am extracting self contained private methods beforehand. * FEATURE: Parse JSON-LD and introduce Movie object JSON LD data is very easily transferable to Ruby objects because they contain types. If these types are mapped to Ruby objects, it is also better to make all the parsed data very explicit and easily extendable. JSON-LD has many more standardized item types, with a full list here: https://schema.org/docs/full.html However in order to decrease the scope, I only adapted the movie type. * DEV: Change inheritance between normalizers Normalizers are not supposed to have an inheritance relationships amongst each other. They are all normalizers, but all normalizing separate protocols. This is why I chose to extract a parent class and relieve Open Graph off that responsibility. Removing the parent class altogether could also a possibility, but I am keeping the scope limited to having a more accurate representation of the normalizers while making it easier to add a new one. * Lint changes * Bring back the Oembed OpenGraph inheritance There is one test that caught that this inheritance was necessary. I still think modelling wise this inheritance shouldn't exist, but this can be tackled separately. * Return empty hash if the json received is invalid Before this change if there was a parsing error with JSON it would throw an exception. The goal of this commit is to rescue that exception and then log a warning. I chose to use Discourse's logger wrapper `warn_exception` to have the backtrace and not just used Rails logger. I considered raising an `InvalidParameters` error however if the JSON here is invalid it should not block showing of the Onebox, so logging is enough. * Prep to support more JSONLD schema types with case * Extract mustache template object created from JSONLD
2022-06-13 11:32:34 -04:00
end
end
Add support for JSON LD in Onebox (#17007) * FIX: Fix a bug that is accessing the values in a hash wrongly and write tests I decided to write tests in order to be confident in my refactor that's in the next commit. Meanwhile I have discovered a potential bug. The `title_attr` key was accessed as a string, but all the keys are actually symbols so it was never evaluated to be true. irb(main):025:0> d = {key: 'value'} => {:key=>"value"} irb(main):026:0> d['key'] => nil irb(main):027:0> d[:key] => "value" * DEV: Extract methods for readability I will be adding a new method following the conventions in place for adding a new normalizer. And this will make the readability of the `raw` block even more difficult; so I am extracting self contained private methods beforehand. * FEATURE: Parse JSON-LD and introduce Movie object JSON LD data is very easily transferable to Ruby objects because they contain types. If these types are mapped to Ruby objects, it is also better to make all the parsed data very explicit and easily extendable. JSON-LD has many more standardized item types, with a full list here: https://schema.org/docs/full.html However in order to decrease the scope, I only adapted the movie type. * DEV: Change inheritance between normalizers Normalizers are not supposed to have an inheritance relationships amongst each other. They are all normalizers, but all normalizing separate protocols. This is why I chose to extract a parent class and relieve Open Graph off that responsibility. Removing the parent class altogether could also a possibility, but I am keeping the scope limited to having a more accurate representation of the normalizers while making it easier to add a new one. * Lint changes * Bring back the Oembed OpenGraph inheritance There is one test that caught that this inheritance was necessary. I still think modelling wise this inheritance shouldn't exist, but this can be tackled separately. * Return empty hash if the json received is invalid Before this change if there was a parsing error with JSON it would throw an exception. The goal of this commit is to rescue that exception and then log a warning. I chose to use Discourse's logger wrapper `warn_exception` to have the backtrace and not just used Rails logger. I considered raising an `InvalidParameters` error however if the JSON here is invalid it should not block showing of the Onebox, so logging is enough. * Prep to support more JSONLD schema types with case * Extract mustache template object created from JSONLD
2022-06-13 11:32:34 -04:00
{}
Add support for JSON LD in Onebox (#17007) * FIX: Fix a bug that is accessing the values in a hash wrongly and write tests I decided to write tests in order to be confident in my refactor that's in the next commit. Meanwhile I have discovered a potential bug. The `title_attr` key was accessed as a string, but all the keys are actually symbols so it was never evaluated to be true. irb(main):025:0> d = {key: 'value'} => {:key=>"value"} irb(main):026:0> d['key'] => nil irb(main):027:0> d[:key] => "value" * DEV: Extract methods for readability I will be adding a new method following the conventions in place for adding a new normalizer. And this will make the readability of the `raw` block even more difficult; so I am extracting self contained private methods beforehand. * FEATURE: Parse JSON-LD and introduce Movie object JSON LD data is very easily transferable to Ruby objects because they contain types. If these types are mapped to Ruby objects, it is also better to make all the parsed data very explicit and easily extendable. JSON-LD has many more standardized item types, with a full list here: https://schema.org/docs/full.html However in order to decrease the scope, I only adapted the movie type. * DEV: Change inheritance between normalizers Normalizers are not supposed to have an inheritance relationships amongst each other. They are all normalizers, but all normalizing separate protocols. This is why I chose to extract a parent class and relieve Open Graph off that responsibility. Removing the parent class altogether could also a possibility, but I am keeping the scope limited to having a more accurate representation of the normalizers while making it easier to add a new one. * Lint changes * Bring back the Oembed OpenGraph inheritance There is one test that caught that this inheritance was necessary. I still think modelling wise this inheritance shouldn't exist, but this can be tackled separately. * Return empty hash if the json received is invalid Before this change if there was a parsing error with JSON it would throw an exception. The goal of this commit is to rescue that exception and then log a warning. I chose to use Discourse's logger wrapper `warn_exception` to have the backtrace and not just used Rails logger. I considered raising an `InvalidParameters` error however if the JSON here is invalid it should not block showing of the Onebox, so logging is enough. * Prep to support more JSONLD schema types with case * Extract mustache template object created from JSONLD
2022-06-13 11:32:34 -04:00
end
def parse_json(json)
begin
JSON[json]
rescue JSON::ParserError => e
Discourse.warn_exception(e, message: "Error parsing JSON-LD: #{json}")
Add support for JSON LD in Onebox (#17007) * FIX: Fix a bug that is accessing the values in a hash wrongly and write tests I decided to write tests in order to be confident in my refactor that's in the next commit. Meanwhile I have discovered a potential bug. The `title_attr` key was accessed as a string, but all the keys are actually symbols so it was never evaluated to be true. irb(main):025:0> d = {key: 'value'} => {:key=>"value"} irb(main):026:0> d['key'] => nil irb(main):027:0> d[:key] => "value" * DEV: Extract methods for readability I will be adding a new method following the conventions in place for adding a new normalizer. And this will make the readability of the `raw` block even more difficult; so I am extracting self contained private methods beforehand. * FEATURE: Parse JSON-LD and introduce Movie object JSON LD data is very easily transferable to Ruby objects because they contain types. If these types are mapped to Ruby objects, it is also better to make all the parsed data very explicit and easily extendable. JSON-LD has many more standardized item types, with a full list here: https://schema.org/docs/full.html However in order to decrease the scope, I only adapted the movie type. * DEV: Change inheritance between normalizers Normalizers are not supposed to have an inheritance relationships amongst each other. They are all normalizers, but all normalizing separate protocols. This is why I chose to extract a parent class and relieve Open Graph off that responsibility. Removing the parent class altogether could also a possibility, but I am keeping the scope limited to having a more accurate representation of the normalizers while making it easier to add a new one. * Lint changes * Bring back the Oembed OpenGraph inheritance There is one test that caught that this inheritance was necessary. I still think modelling wise this inheritance shouldn't exist, but this can be tackled separately. * Return empty hash if the json received is invalid Before this change if there was a parsing error with JSON it would throw an exception. The goal of this commit is to rescue that exception and then log a warning. I chose to use Discourse's logger wrapper `warn_exception` to have the backtrace and not just used Rails logger. I considered raising an `InvalidParameters` error however if the JSON here is invalid it should not block showing of the Onebox, so logging is enough. * Prep to support more JSONLD schema types with case * Extract mustache template object created from JSONLD
2022-06-13 11:32:34 -04:00
{}
end
end
end
end