Sha256: 6f73aef2fe62ba62b1bc9da5965504799784636cfa4af7d7dae4d4d8c23972b2

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

require 'lt/lcms/lesson/downloader/base'

module Lt
  module Lcms
    module Lesson
      module Downloader
        class Gdoc < Base
          GOOGLE_DRAWING_RE = %r{https?://docs\.google\.com/?[^"]*/drawings/[^"]*}i
          MIME_TYPE = 'application/vnd.google-apps.document'
          MIME_TYPE_EXPORT = 'text/html'

          def self.gdoc_file_url(id)
            "https://docs.google.com/document/d/#{id}"
          end

          def download
            super(&method(:handle_google_drawings))
          end

          private

          attr_reader :options

          def handle_google_drawings(html)
            return html unless (match = html.scan(GOOGLE_DRAWING_RE))

            bearer = @credentials.fetch_access_token!['access_token']
            headers = { 'Authorization' => "Bearer #{bearer}" }

            match.to_a.uniq.each do |url|
              response = HTTParty.get CGI.unescapeHTML(url), headers: headers

              next unless response.code == 200

              new_src = "data:#{response.content_type};base64, #{Base64.encode64(response)}\" drawing_url=\"#{url}"
              html = html.gsub(url, new_src)
            end

            html
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lt-lcms-0.2.0 lib/lt/lcms/lesson/downloader/gdoc.rb
lt-lcms-0.1.1 lib/lt/lcms/lesson/downloader/gdoc.rb