Class: C11n::External::GoogleDriveDriver

Inherits:
Object
  • Object
show all
Defined in:
lib/c11n/external/google_drive_driver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ GoogleDriveDriver

Returns a new instance of GoogleDriveDriver.



8
9
10
11
# File 'lib/c11n/external/google_drive_driver.rb', line 8

def initialize(options = {})
  load_from_options(options)
  load_configuration
end

Instance Attribute Details

#emailObject

Returns the value of attribute email.



6
7
8
# File 'lib/c11n/external/google_drive_driver.rb', line 6

def email
  @email
end

#passwordObject

Returns the value of attribute password.



6
7
8
# File 'lib/c11n/external/google_drive_driver.rb', line 6

def password
  @password
end

#spreadsheet_keyObject

Returns the value of attribute spreadsheet_key.



6
7
8
# File 'lib/c11n/external/google_drive_driver.rb', line 6

def spreadsheet_key
  @spreadsheet_key
end

#worksheet_numberObject

Returns the value of attribute worksheet_number.



6
7
8
# File 'lib/c11n/external/google_drive_driver.rb', line 6

def worksheet_number
  @worksheet_number
end

Instance Method Details

#connectionObject



27
28
29
# File 'lib/c11n/external/google_drive_driver.rb', line 27

def connection
  @connection ||= ::GoogleDrive.(@email, @password)
end

#load_configurationObject



20
21
22
23
24
25
# File 'lib/c11n/external/google_drive_driver.rb', line 20

def load_configuration
  @email ||= C11n::Configuration.instance.external(:google_drive)[:email]
  @password ||= C11n::Configuration.instance.external(:google_drive)[:password]
  @spreadsheet_key ||= C11n::Configuration.instance.external(:google_drive)[:spreadsheet_key]
  @worksheet_number ||= C11n::Configuration.instance.external(:google_drive)[:worksheet_number]
end

#load_from_options(options) ⇒ Object



13
14
15
16
17
18
# File 'lib/c11n/external/google_drive_driver.rb', line 13

def load_from_options(options)
  @email = options[:email]
  @password = options[:password]
  @spreadsheet_key = options[:spreadsheet_key]
  @worksheet_number = options[:worksheet_number]
end

#spreadsheetObject



35
36
37
# File 'lib/c11n/external/google_drive_driver.rb', line 35

def spreadsheet
  @spreadsheet ||= connection.spreadsheet_by_key(@spreadsheet_key)
end

#tableObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/c11n/external/google_drive_driver.rb', line 39

def table
  raw_table = []

  for row in 1..worksheet.num_rows
    raw_table[row - 1] ||= []

    for col in 1..worksheet.num_cols
      raw_table[row - 1][col - 1] ||= worksheet[row, col]
    end
  end

  raw_table
end

#worksheetObject



31
32
33
# File 'lib/c11n/external/google_drive_driver.rb', line 31

def worksheet
  @worksheet ||= spreadsheet.worksheets[@worksheet_number]
end

#write_table(table) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/c11n/external/google_drive_driver.rb', line 53

def write_table(table)
  table.each_with_index do |row, row_index|
    row.each_with_index do |column, column_index|
      worksheet[row_index + 1, column_index + 1] = column if column
    end
  end

  worksheet.save
end