Sha256: d778db1757873cf7ab2cf48eeee39fbc614f1c39d86cf34886510f0938e42220

Contents?: true

Size: 1.62 KB

Versions: 24

Compression:

Stored size: 1.62 KB

Contents

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: og_data[:description] || "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

24 entries across 24 versions & 1 rubygems

Version Path
onebox-1.8.19 lib/onebox/engine/google_docs_onebox.rb
onebox-1.8.18 lib/onebox/engine/google_docs_onebox.rb
onebox-1.8.17 lib/onebox/engine/google_docs_onebox.rb
onebox-1.8.16 lib/onebox/engine/google_docs_onebox.rb
onebox-1.8.15 lib/onebox/engine/google_docs_onebox.rb
onebox-1.8.14 lib/onebox/engine/google_docs_onebox.rb
onebox-1.8.13 lib/onebox/engine/google_docs_onebox.rb
onebox-1.8.12 lib/onebox/engine/google_docs_onebox.rb
onebox-1.8.11 lib/onebox/engine/google_docs_onebox.rb
onebox-1.8.10 lib/onebox/engine/google_docs_onebox.rb
onebox-1.8.9 lib/onebox/engine/google_docs_onebox.rb
onebox-1.8.8 lib/onebox/engine/google_docs_onebox.rb
onebox-1.8.7 lib/onebox/engine/google_docs_onebox.rb
onebox-1.8.6 lib/onebox/engine/google_docs_onebox.rb
onebox-1.8.5 lib/onebox/engine/google_docs_onebox.rb
onebox-1.8.4 lib/onebox/engine/google_docs_onebox.rb
onebox-1.8.3 lib/onebox/engine/google_docs_onebox.rb
onebox-1.8.2 lib/onebox/engine/google_docs_onebox.rb
onebox-1.8.1 lib/onebox/engine/google_docs_onebox.rb
onebox-1.8.0 lib/onebox/engine/google_docs_onebox.rb