class Lt::Lcms::Lesson::Downloader::Gdoc

Constants

BASE_DPI
GOOGLE_DRAWING_RE
GOOGLE_URL_RE
MIME_TYPE
MIME_TYPE_EXPORT

Attributes

options[R]

Public Class Methods

gdoc_file_url(id) click to toggle source
# File lib/lt/lcms/lesson/downloader/gdoc.rb, line 15
def self.gdoc_file_url(id)
  "https://docs.google.com/document/d/#{id}"
end

Public Instance Methods

download() click to toggle source
# File lib/lt/lcms/lesson/downloader/gdoc.rb, line 19
def download
  super do |html|
    fix_links handle_google_drawings(html)
  end
end

Private Instance Methods

handle_google_drawings(html) click to toggle source
# File lib/lt/lcms/lesson/downloader/gdoc.rb, line 42
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|
    upd_url = updated_drawing_url_for(url)
    response = HTTParty.get CGI.unescapeHTML(upd_url), headers: headers

    next unless response.code == 200

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

  html
end
updated_drawing_url_for(url) click to toggle source

Update drawing url w/h parameters to download in better quality than default 72 dpi

# File lib/lt/lcms/lesson/downloader/gdoc.rb, line 62
def updated_drawing_url_for(url)
  return url unless options[:dpi].present?

  dpi_ratio = options[:dpi].to_f / BASE_DPI
  uri = URI.parse(url)
  query = CGI.parse(uri.query).transform_values(&:first)
  query['w'] = (query['w'].to_i * dpi_ratio).round.to_s
  query['h'] = (query['h'].to_i * dpi_ratio).round.to_s
  URI::HTTPS.build(host: uri.host, path: uri.path, query: query.to_query).to_s
end