Class: Locraft::GoogleDriveWrapper

Inherits:
Object
  • Object
show all
Includes:
FileUtils
Defined in:
lib/locraft/google_drive_wrapper.rb

Constant Summary collapse

EXPORTED_CSV_FILE =
'google_doc_sheet.csv'
SESSION_TOKEN_FILE =
'.locraft_gdrive_token'

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ GoogleDriveWrapper

Returns a new instance of GoogleDriveWrapper.



12
13
14
# File 'lib/locraft/google_drive_wrapper.rb', line 12

def initialize(config)
  @config = config
end

Instance Method Details

#authenticateObject



16
17
18
19
20
# File 'lib/locraft/google_drive_wrapper.rb', line 16

def authenticate
  mkdir_p @config.relative_destination_dir unless Dir.exist?(@config.relative_destination_dir)
  token_file = File.join(@config.relative_destination_dir, SESSION_TOKEN_FILE)
  @session = GoogleDrive.saved_session(token_file)
end

#doc_named(name) ⇒ Object



22
23
24
25
# File 'lib/locraft/google_drive_wrapper.rb', line 22

def doc_named(name)
  authenticate unless @session
  @session.spreadsheet_by_title(name)
end

#export_worksheetObject

return exported file path



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/locraft/google_drive_wrapper.rb', line 28

def export_worksheet
  worksheets = doc_named(@config.gdoc_file).worksheets
  if worksheets.count > @config.gdoc_sheet
    sheet = worksheets[@config.gdoc_sheet]
    file = File.join(@config.relative_destination_dir, EXPORTED_CSV_FILE)
    sheet.export_as_file(file)
    file
  else
    warn 'gdrive_wrapper worksheet export error: sheet number in config out of bounds!'
  end
end