class Lt::Lcms::Lesson::Downloader::Base

Constants

MAX_RETRY_COUNT
MIME_TYPE_EXPORT
RETRY_DELAYES

Attributes

content[R]
options[R]

Public Class Methods

file_id_for(url) click to toggle source
# File lib/lt/lcms/lesson/downloader/base.rb, line 14
def self.file_id_for(url)
  url.scan(%r{/d/([^/]+)/?}).first.try(:first) ||
    url.scan(%r{/open\?id=([^/]+)/?}).first.try(:first)
end
new(credentials, file_url, opts = {}) click to toggle source
# File lib/lt/lcms/lesson/downloader/base.rb, line 21
def initialize(credentials, file_url, opts = {})
  @credentials = credentials
  @file_url = file_url
  @options = opts
end

Public Instance Methods

download(mime_type: self.class::MIME_TYPE_EXPORT) { |raw_content| ... } click to toggle source
# File lib/lt/lcms/lesson/downloader/base.rb, line 27
def download(mime_type: self.class::MIME_TYPE_EXPORT)
  retry_attempt ||= 0
  raw_content = service.export_file(file_id, mime_type)&.force_encoding('UTF-8')
  @content =
    if block_given?
      yield raw_content
    else
      raw_content
    end
  self
rescue ::Google::Apis::RateLimitError
  raise unless options[:import_retry]
  raise if retry_attempt >= MAX_RETRY_COUNT

  sleep RETRY_DELAYES[retry_attempt] * rand(1.0..5.0)
  retry_attempt += 1
  retry
end
file() click to toggle source
# File lib/lt/lcms/lesson/downloader/base.rb, line 46
def file
  @file ||= service.get_file(
    file_id,
    fields: 'lastModifyingUser,modifiedTime,name,version',
    supports_all_drives: true
  )
end
file_id() click to toggle source
# File lib/lt/lcms/lesson/downloader/base.rb, line 54
def file_id
  @file_id ||= self.class.file_id_for @file_url
end

Private Instance Methods

service() click to toggle source
# File lib/lt/lcms/lesson/downloader/base.rb, line 62
def service
  @service ||= Lt::Google::Api::Drive.build(@credentials)
end