Class: DocumentExporter::Gdoc::Base

Inherits:
Base
  • Object
show all
Defined in:
lib/document_exporter/gdoc/base.rb

Direct Known Subclasses

Document, Material, StudentMaterial, TeacherMaterial

Constant Summary collapse

GOOGLE_API_CLIENT_UPLOAD_RETRIES =
ENV.fetch('GOOGLE_API_CLIENT_UPLOAD_RETRIES', 5).to_i
GOOGLE_API_CLIENT_UPLOAD_TIMEOUT =
ENV.fetch('GOOGLE_API_CLIENT_UPLOAD_TIMEOUT', 60).to_i
GOOGLE_API_UPLOAD_OPTIONS =
{
  options: {
    open_timeout_sec: GOOGLE_API_CLIENT_UPLOAD_TIMEOUT,
    read_timeout_sec: GOOGLE_API_CLIENT_UPLOAD_TIMEOUT,
    retries: GOOGLE_API_CLIENT_UPLOAD_RETRIES,
    send_timeout_sec: GOOGLE_API_CLIENT_UPLOAD_TIMEOUT
  }
}.freeze
VERSION_RE =
/_v\d+$/i.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#included_materials, #initialize, #ordered_materials, pdf_key

Constructor Details

This class inherits a constructor from DocumentExporter::Base

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



21
22
23
# File 'lib/document_exporter/gdoc/base.rb', line 21

def document
  @document
end

#optionsObject (readonly)

Returns the value of attribute options.



21
22
23
# File 'lib/document_exporter/gdoc/base.rb', line 21

def options
  @options
end

Class Method Details

.gdoc_key(type) ⇒ Object



24
25
26
# File 'lib/document_exporter/gdoc/base.rb', line 24

def gdoc_key(type)
  "gdoc_#{type}"
end

.url_for(file_id) ⇒ Object



28
29
30
# File 'lib/document_exporter/gdoc/base.rb', line 28

def url_for(file_id)
  "https://drive.google.com/open?id=#{file_id}"
end

Instance Method Details

#create_gdoc_folders(folder) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/document_exporter/gdoc/base.rb', line 33

def create_gdoc_folders(folder)
  id = drive_service.create_folder(folder)
  folders = [id]
  folders << drive_service.create_folder(DocumentExporter::Gdoc::TeacherMaterial::FOLDER_NAME, id)
  folders << drive_service.create_folder(DocumentExporter::Gdoc::StudentMaterial::FOLDER_NAME, id)
  folders.each(&method(:delete_previous_versions_from))
end

#exportObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/document_exporter/gdoc/base.rb', line 41

def export
  file_id = @options[:file_id] || drive_service.file_id
  parent_folder = file_id.blank? ? @options[:folder_id] || drive_service.parent : nil

  file_name = "#{@options[:prefix]}#{document.base_filename}"
  file_params = { name: file_name, mime_type: 'application/vnd.google-apps.document' }
  file_params[:parents] = [parent_folder] if parent_folder.present?
   = Google::Apis::DriveV3::File.new(file_params)

  params = {
    content_type: 'text/html',
    upload_source: StringIO.new(content)
  }.merge(GOOGLE_API_UPLOAD_OPTIONS)

  @id = if file_id.blank?
          drive_service.service.create_file(, params)
        else
          drive_service.service.update_file(file_id, , params)
        end.id

  post_processing

  self
end

#export_to(folder_id, file_id: nil) ⇒ Object

Parameters:

  • folder_id

    String ID of folder export file to

  • file_id (defaults to: nil)

    String ID of existing file



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/document_exporter/gdoc/base.rb', line 70

def export_to(folder_id, file_id: nil)
   = Google::Apis::DriveV3::File.new(
    name: document.base_filename(with_version: false),
    mime_type: 'application/vnd.google-apps.document',
    parents: [folder_id]
  )

  params = {
    content_type: 'text/html',
    upload_source: StringIO.new(content)
  }.merge(GOOGLE_API_UPLOAD_OPTIONS)

  @id = if file_id.present?
          drive_service.service.update_file(file_id, , params)
        else
          drive_service.service.create_file(, params)
        end.id

  post_processing

  self
end

#urlObject



93
94
95
# File 'lib/document_exporter/gdoc/base.rb', line 93

def url
  self.class.url_for @id
end