class ActiveWorksheet::Adapters::GoogleSheetsAdapter

Constants

ADAPTER_NAME

Public Class Methods

new(source: nil, authorization: {}) click to toggle source
# File lib/active_worksheet/adapters/google_sheets_adapter.rb, line 10
def initialize(source: nil, authorization: {})
  super(source: source, authorization: authorization)
end

Public Instance Methods

all() click to toggle source
# File lib/active_worksheet/adapters/google_sheets_adapter.rb, line 14
def all
  session = build_session(authorization)
  @workbook = session.spreadsheet_by_url(source)
  table = @workbook.worksheets.first

  headers = table.rows[0].map do |header|
    ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.parameterize(header))
  end

  table.rows(1).map do |row|
    row.each_with_index.reduce({}) do |result, (value, index)|
      result[headers[index]] = value
      result
    end
  end
end
count() click to toggle source
# File lib/active_worksheet/adapters/google_sheets_adapter.rb, line 43
def count
  all.count
end
find(index) click to toggle source
# File lib/active_worksheet/adapters/google_sheets_adapter.rb, line 31
def find(index)
  all[index]
end
first() click to toggle source
# File lib/active_worksheet/adapters/google_sheets_adapter.rb, line 35
def first
  find(0)
end
last() click to toggle source
# File lib/active_worksheet/adapters/google_sheets_adapter.rb, line 39
def last
  find(-1)
end

Private Instance Methods

build_session(authorization) click to toggle source
# File lib/active_worksheet/adapters/google_sheets_adapter.rb, line 49
def build_session(authorization)
  return GoogleDrive::Session.new_dummy if authorization.nil?

  if authorization.has_key?(:access_token)
    GoogleDrive::Session.login_with_oauth(authorization[:access_token])
  elsif authorization.has_key?(:config)
    GoogleDrive::Session.from_config(authorization[:config])
  elsif authorization.has_key?(:credentials)
    GoogleDrive::Session.from_credentials(authorization[:credentials])
  elsif authorization.has_key?(:service_account_key)
    GoogleDrive::Session.from_service_account_key(authorization[:service_account_key])
  end
end