Sha256: 81e77fe021634d3cbf6c0495c0aac15b2093a26a6b6ab55ea626af63204fda88

Contents?: true

Size: 1.68 KB

Versions: 66

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

module Onebox
  module Engine
    class GoogleDocsOnebox
      include Engine
      include LayoutSupport

      def self.supported_endpoints
        %w(spreadsheets document forms presentation)
      end

      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-]*)).+$/
      always_https

      protected

      def data
        og_data = get_og_data
        result = { 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
                 }
        result
      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, 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

Version data entries

66 entries across 66 versions & 1 rubygems

Version Path
onebox-1.9.30 lib/onebox/engine/google_docs_onebox.rb
onebox-1.9.29 lib/onebox/engine/google_docs_onebox.rb
onebox-1.9.28.4 lib/onebox/engine/google_docs_onebox.rb
onebox-1.9.28.3 lib/onebox/engine/google_docs_onebox.rb
onebox-1.9.28.2 lib/onebox/engine/google_docs_onebox.rb
onebox-1.9.28.1 lib/onebox/engine/google_docs_onebox.rb
onebox-1.9.28 lib/onebox/engine/google_docs_onebox.rb
onebox-1.9.27.2 lib/onebox/engine/google_docs_onebox.rb
onebox-1.9.27.1 lib/onebox/engine/google_docs_onebox.rb
onebox-1.9.26 lib/onebox/engine/google_docs_onebox.rb
onebox-1.9.25 lib/onebox/engine/google_docs_onebox.rb
onebox-1.9.24 lib/onebox/engine/google_docs_onebox.rb
onebox-1.9.23 lib/onebox/engine/google_docs_onebox.rb
onebox-1.9.22 lib/onebox/engine/google_docs_onebox.rb
onebox-1.9.21 lib/onebox/engine/google_docs_onebox.rb
onebox-1.9.20 lib/onebox/engine/google_docs_onebox.rb
onebox-1.9.19 lib/onebox/engine/google_docs_onebox.rb
onebox-1.9.18 lib/onebox/engine/google_docs_onebox.rb
onebox-1.9.17 lib/onebox/engine/google_docs_onebox.rb
onebox-1.9.16 lib/onebox/engine/google_docs_onebox.rb