DEV: Replace custom Onebox symbolize_keys implementation with ActiveSupport (#23828)

We have a custom implementation of #symbolize_keys in our Onebox helpers. This is likely a legacy from when Onebox was a standalone gem. This change replaces all usages with either #deep_symbolize_keys from ActiveSupport, or appropriate option to the JSON parser gem used.
This commit is contained in:
Ted Johansson 2023-10-09 09:32:09 +02:00 committed by GitHub
parent c970cbeac4
commit b2a5f5802a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 7 additions and 18 deletions

View File

@ -76,7 +76,7 @@ module Onebox
script = page.at_css('script[type="application/ld+json"]') script = page.at_css('script[type="application/ld+json"]')
if json_string = script&.text if json_string = script&.text
@og_data = Onebox::Helpers.symbolize_keys(::MultiJson.load(json_string)) @og_data = ::MultiJson.load(json_string, symbolize_keys: true)
else else
@og_data = {} @og_data = {}
end end

View File

@ -53,7 +53,7 @@ module Onebox
rescue StandardError rescue StandardError
"{}" "{}"
end end
oembed_data = Onebox::Helpers.symbolize_keys(::MultiJson.load(response)) oembed_data = ::MultiJson.load(response, symbolize_keys: true)
imgur_data_id = Nokogiri.HTML(oembed_data[:html]).xpath("//blockquote").attr("data-id") imgur_data_id = Nokogiri.HTML(oembed_data[:html]).xpath("//blockquote").attr("data-id")
imgur_data_id.to_s[%r{a/}] imgur_data_id.to_s[%r{a/}]
end end

View File

@ -34,7 +34,7 @@ module Onebox
def oembed_data def oembed_data
response = Onebox::Helpers.fetch_response("https://vimeo.com/api/oembed.json?url=#{url}") response = Onebox::Helpers.fetch_response("https://vimeo.com/api/oembed.json?url=#{url}")
@oembed_data = Onebox::Helpers.symbolize_keys(::MultiJson.load(response)) @oembed_data = ::MultiJson.load(response, symbolize_keys: true)
rescue StandardError rescue StandardError
"{}" "{}"
end end

View File

@ -9,17 +9,6 @@ module Onebox
IGNORE_CANONICAL_DOMAINS ||= %w[www.instagram.com medium.com youtube.com] IGNORE_CANONICAL_DOMAINS ||= %w[www.instagram.com medium.com youtube.com]
def self.symbolize_keys(hash)
return {} if hash.nil?
hash.inject({}) do |result, (key, value)|
new_key = key.is_a?(String) ? key.to_sym : key
new_value = value.is_a?(Hash) ? symbolize_keys(value) : value
result[new_key] = new_value
result
end
end
def self.clean(html) def self.clean(html)
html.gsub(/<[^>]+>/, " ").gsub(/\n/, "") html.gsub(/<[^>]+>/, " ").gsub(/\n/, "")
end end

View File

@ -12,7 +12,7 @@ module Onebox
attr_reader :view attr_reader :view
def initialize(name, record) def initialize(name, record)
@record = Onebox::Helpers.symbolize_keys(record) @record = record.deep_symbolize_keys
# Fix any relative paths # Fix any relative paths
if @record[:image] && @record[:image] =~ %r{\A/[^/]} if @record[:image] && @record[:image] =~ %r{\A/[^/]}

View File

@ -3,7 +3,7 @@
module Onebox module Onebox
class Oembed < OpenGraph class Oembed < OpenGraph
def initialize(response) def initialize(response)
@data = Onebox::Helpers.symbolize_keys(::MultiJson.load(response)) @data = ::MultiJson.load(response, symbolize_keys: true)
# never use oembed from WordPress 4.4 (it's broken) # never use oembed from WordPress 4.4 (it's broken)
@data.delete(:html) if @data[:html] && @data[:html]["wp-embedded-content"] @data.delete(:html) if @data[:html] && @data[:html]["wp-embedded-content"]

View File

@ -72,7 +72,7 @@ RSpec.describe Onebox::Engine::GoogleMapsOnebox do
end end
end end
let(:data) { Onebox::Helpers.symbolize_keys(onebox.send(:data)) } let(:data) { onebox.send(:data).deep_symbolize_keys }
let(:link) { |example| URLS[example.metadata[:urltype] || :short][:test] } let(:link) { |example| URLS[example.metadata[:urltype] || :short][:test] }
include_context "an engine", urltype: :short include_context "an engine", urltype: :short

View File

@ -22,7 +22,7 @@ module OneboxHelpers
let(:onebox) { described_class.new(link) } let(:onebox) { described_class.new(link) }
let(:html) { onebox.to_html } let(:html) { onebox.to_html }
let(:data) { Onebox::Helpers.symbolize_keys(onebox.send(:data)) } let(:data) { onebox.send(:data).deep_symbolize_keys }
let(:link) { @link } let(:link) { @link }
end end