Class: ActiveWorksheet::Adapters::GoogleSheetsAdapter

Inherits:
BaseAdapter
  • Object
show all
Defined in:
lib/active_worksheet/adapters/google_sheets_adapter.rb

Constant Summary collapse

ADAPTER_NAME =
"GoogleSheets"

Instance Attribute Summary

Attributes inherited from BaseAdapter

#authorization, #source

Instance Method Summary collapse

Methods inherited from BaseAdapter

#new

Constructor Details

#initialize(source: nil, authorization: {}) ⇒ GoogleSheetsAdapter

Returns a new instance of GoogleSheetsAdapter.



10
11
12
# File 'lib/active_worksheet/adapters/google_sheets_adapter.rb', line 10

def initialize(source: nil, authorization: {})
  super(source: source, authorization: authorization)
end

Instance Method Details

#allObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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

#countObject



43
44
45
# File 'lib/active_worksheet/adapters/google_sheets_adapter.rb', line 43

def count
  all.count
end

#find(index) ⇒ Object



31
32
33
# File 'lib/active_worksheet/adapters/google_sheets_adapter.rb', line 31

def find(index)
  all[index]
end

#firstObject



35
36
37
# File 'lib/active_worksheet/adapters/google_sheets_adapter.rb', line 35

def first
  find(0)
end

#lastObject



39
40
41
# File 'lib/active_worksheet/adapters/google_sheets_adapter.rb', line 39

def last
  find(-1)
end