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
# File 'lib/locraft/google_drive_wrapper.rb', line 16

def authenticate
  token_file = File.join(@config.from_folder, SESSION_TOKEN_FILE)
  @session = GoogleDrive.saved_session(token_file)
end

#doc_named(name) ⇒ Object



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

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

#export_worksheetObject

return worksheet



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

def export_worksheet
  worksheets = doc_named(@config.gdoc_file)&.worksheets
  if worksheets.count > @config.gdoc_sheet
    worksheets[@config.gdoc_sheet]
  else
    warn 'gdrive_wrapper worksheet export error:
You have no access to google_doc or sheet number in config out of bounds!'
  end
end

#export_worksheet_csvObject

return worksheet as string in csv format



46
47
48
49
# File 'lib/locraft/google_drive_wrapper.rb', line 46

def export_worksheet_csv
  worksheet = export_worksheet
  worksheet.export_as_string
end

#export_worksheet_fileObject

return exported worksheet file path



38
39
40
41
42
43
# File 'lib/locraft/google_drive_wrapper.rb', line 38

def export_worksheet_file
  sheet = export_worksheet
  file = File.join(@config.relative_strings_destination, EXPORTED_CSV_FILE)
  File.delete(file) if File.exist?(file)
  sheet.export_as_file(file)
end