lib/onebox/engine/standard_embed.rb in onebox-1.8.79 vs lib/onebox/engine/standard_embed.rb in onebox-1.8.80

- old
+ new

@@ -1,6 +1,7 @@ require "cgi" +require "onebox/open_graph" module Onebox module Engine module StandardEmbed @@ -39,14 +40,23 @@ twitter = get_twitter oembed = get_oembed @raw = {} - og.each { |k, v| @raw[k] ||= v unless Onebox::Helpers::blank?(v) } + og.data.each do |k, v| + next if k == "title_attr" + v = og.send(k) + @raw[k] ||= v unless v.nil? + end + twitter.each { |k, v| @raw[k] ||= v unless Onebox::Helpers::blank?(v) } - oembed.each { |k, v| @raw[k] ||= v unless Onebox::Helpers::blank?(v) } + oembed.data.each do |k, v| + v = oembed.send(k) + @raw[k] ||= v unless v.nil? + end + favicon = get_favicon @raw["favicon".to_sym] = favicon unless Onebox::Helpers::blank?(favicon) @raw end @@ -61,46 +71,15 @@ @html_doc = Onebox::Helpers.fetch_html_doc(url, headers) end def get_oembed - oembed_url = nil - - StandardEmbed.oembed_providers.each do |regexp, endpoint| - if url =~ regexp - oembed_url = "#{endpoint}?url=#{url}" - break - end - end - - if html_doc - if Onebox::Helpers.blank?(oembed_url) - application_json = html_doc.at("//link[@type='application/json+oembed']/@href") - oembed_url = application_json.value if application_json - end - - if Onebox::Helpers.blank?(oembed_url) - text_json = html_doc.at("//link[@type='text/json+oembed']/@href") - oembed_url ||= text_json.value if text_json - end - end - - return {} if Onebox::Helpers.blank?(oembed_url) - - json_response = Onebox::Helpers.fetch_response(oembed_url) rescue "{}" - oe = Onebox::Helpers.symbolize_keys(::MultiJson.load(json_response)) - - # never use oembed from WordPress 4.4 (it's broken) - oe.delete(:html) if oe[:html] && oe[:html]["wp-embedded-content"] - - oe - rescue Errno::ECONNREFUSED, Net::HTTPError, Net::HTTPFatalError, MultiJson::LoadError - {} + @oembed ||= Onebox::Oembed.new(get_json_response) end def get_opengraph - ::Onebox::Helpers.extract_opengraph(html_doc) + @opengraph ||= ::Onebox::OpenGraph.new(html_doc) end def get_twitter return {} unless html_doc @@ -121,9 +100,46 @@ favicon = html_doc.css('link[rel="shortcut icon"], link[rel="icon shortcut"], link[rel="shortcut"], link[rel="icon"]').first favicon = favicon.nil? ? nil : (favicon['href'].nil? ? nil : favicon['href'].strip) Onebox::Helpers::get_absolute_image_url(favicon, url) + end + + def get_json_response + oembed_url = get_oembed_url + + return "{}" if Onebox::Helpers.blank?(oembed_url) + + Onebox::Helpers.fetch_response(oembed_url) rescue "{}" + rescue Errno::ECONNREFUSED, Net::HTTPError, Net::HTTPFatalError, MultiJson::LoadError + "{}" + end + + protected + + def get_oembed_url + oembed_url = nil + + StandardEmbed.oembed_providers.each do |regexp, endpoint| + if url =~ regexp + oembed_url = "#{endpoint}?url=#{url}" + break + end + end + + if html_doc + if Onebox::Helpers.blank?(oembed_url) + application_json = html_doc.at("//link[@type='application/json+oembed']/@href") + oembed_url = application_json.value if application_json + end + + if Onebox::Helpers.blank?(oembed_url) + text_json = html_doc.at("//link[@type='text/json+oembed']/@href") + oembed_url ||= text_json.value if text_json + end + end + + oembed_url end end end end