lib/onebox/engine/google_docs_onebox.rb in onebox-2.2.14 vs lib/onebox/engine/google_docs_onebox.rb in onebox-2.2.15

- old
+ new

@@ -2,63 +2,45 @@ module Onebox module Engine class GoogleDocsOnebox include Engine + include StandardEmbed include LayoutSupport - def self.supported_endpoints - %w(spreadsheets document forms presentation) - end + SUPPORTED_ENDPOINTS = %w(spreadsheets document forms presentation) + SHORT_TYPES = { + spreadsheets: :sheets, + document: :docs, + presentation: :slides, + forms: :forms, + } - def self.short_types - @shorttypes ||= { - spreadsheets: :sheets, - document: :docs, - presentation: :slides, - forms: :forms, - } - end - - matches_regexp(/^(https?:)?\/\/(docs\.google\.com)\/(?<endpoint>(#{supported_endpoints.join('|')}))\/d\/((?<key>[\w-]*)).+$/) + matches_regexp(/^(https?:)?\/\/(docs\.google\.com)\/(?<endpoint>(#{SUPPORTED_ENDPOINTS.join('|')}))\/d\/((?<key>[\w-]*)).+$/) always_https - protected + private def data - og_data = get_og_data + og_data = get_opengraph + short_type = SHORT_TYPES[match[:endpoint].to_sym] + + description = if Onebox::Helpers.blank?(og_data.description) + "This #{short_type.to_s.chop.capitalize} is private" + else + Onebox::Helpers.truncate(og_data.description, 250) + end + { link: link, - title: og_data[:title] || "Google #{shorttype.to_s.capitalize}", - description: Onebox::Helpers.truncate(og_data[:description], 250) || "This #{shorttype.to_s.chop.capitalize} is private", - type: shorttype + title: og_data.title || "Google #{short_type.to_s.capitalize}", + description: description, + type: short_type } end - def doc_type - @doc_type ||= match[:endpoint].to_sym - end - - def shorttype - GoogleDocsOnebox.short_types[doc_type] - end - def match @match ||= @url.match(@@matcher) - end - - def get_og_data - response = Onebox::Helpers.fetch_response(url, redirect_limit: 10) rescue nil - html = Nokogiri::HTML(response) - og_data = {} - html.css('meta').each do |m| - if m.attribute('property') && m.attribute('property').to_s.match(/^og:/i) - m_content = m.attribute('content').to_s.strip - m_property = m.attribute('property').to_s.gsub('og:', '') - og_data[m_property.to_sym] = m_content - end - end - og_data end end end end