Class: Rdb2spreadsheet::SpreadsheetClient
- Inherits:
-
Object
- Object
- Rdb2spreadsheet::SpreadsheetClient
- Defined in:
- lib/rdb2spreadsheet/spreadsheet_client.rb
Constant Summary collapse
- OAUTH =
{ site: 'https://accounts.google.com', token_url: '/o/oauth2/token', autorize_url: '/o/oauth2/auth' }
Instance Method Summary collapse
-
#initialize(c) ⇒ SpreadsheetClient
constructor
A new instance of SpreadsheetClient.
- #open_book_by_key(key) ⇒ Object
- #read_worksheet(worksheet_title) ⇒ Object
- #update_worksheet(worksheet_title, headers, records) ⇒ Object
Constructor Details
#initialize(c) ⇒ SpreadsheetClient
Returns a new instance of SpreadsheetClient.
12 13 14 15 16 17 |
# File 'lib/rdb2spreadsheet/spreadsheet_client.rb', line 12 def initialize(c) oauth2 = OAuth2::Client.new(c['client_id'], c['client_secret'], OAUTH) auth_token = OAuth2::AccessToken.from_hash(oauth2, refresh_token: c['refresh_token'], expires_at: 3600) auth_token = auth_token.refresh! @session = GoogleDrive.login_with_oauth(auth_token.token) end |
Instance Method Details
#open_book_by_key(key) ⇒ Object
19 20 21 |
# File 'lib/rdb2spreadsheet/spreadsheet_client.rb', line 19 def open_book_by_key(key) @book = @session.spreadsheet_by_key(key) end |
#read_worksheet(worksheet_title) ⇒ Object
23 24 25 26 |
# File 'lib/rdb2spreadsheet/spreadsheet_client.rb', line 23 def read_worksheet(worksheet_title) return nil if @book.nil? || worksheet_title.nil? @book.worksheet_by_title(worksheet_title) end |
#update_worksheet(worksheet_title, headers, records) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rdb2spreadsheet/spreadsheet_client.rb', line 28 def update_worksheet(worksheet_title, headers, records) return if @book.nil? || worksheet_title.nil? || records.nil? begin target = find_or_create_worksheet_by_title(worksheet_title) headers.each.with_index(1) do |header, index| target[1, index] = header end records.each.with_index(2) do |row, row_index| row.each.with_index(1) do |column, col_index| target[row_index, col_index] = column end target.save if row_index % 100 == 0 end target.save rescue => e puts e. end end |