Class: AdLocalize::Repositories::GSheetsRepository

Inherits:
Object
  • Object
show all
Defined in:
lib/ad_localize/repositories/g_sheets_repository.rb

Constant Summary collapse

SCOPES =
[Google::Apis::SheetsV4::AUTH_SPREADSHEETS_READONLY]
ROWS_MAJOR_DIMENSION =
'ROWS'.freeze
SPREADSHEET_APPLICATION_NAME =
'ad_localize'.freeze

Instance Method Summary collapse

Constructor Details

#initializeGSheetsRepository

Returns a new instance of GSheetsRepository.



8
9
10
11
# File 'lib/ad_localize/repositories/g_sheets_repository.rb', line 8

def initialize
  @service = Google::Apis::SheetsV4::SheetsService.new
  @service.client_options.application_name = SPREADSHEET_APPLICATION_NAME
end

Instance Method Details

#get_sheets_values(g_spreadsheet_options:) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ad_localize/repositories/g_sheets_repository.rb', line 13

def get_sheets_values(g_spreadsheet_options:)
  configure(json_configuration: g_spreadsheet_options.) if @service.authorization.nil?
  spreadsheet = @service.get_spreadsheet(g_spreadsheet_options.spreadsheet_id)
  if g_spreadsheet_options.export_all
    spreadsheet.sheets.map { |sheet| get_sheet_values(spreadsheet_id: g_spreadsheet_options.spreadsheet_id, sheet: sheet) }
  elsif g_spreadsheet_options.sheet_ids.nil? || g_spreadsheet_options.sheet_ids.size.zero?
    spreadsheet.sheets[0..0].map { |sheet| get_sheet_values(spreadsheet_id: g_spreadsheet_options.spreadsheet_id, sheet: sheet) }
  else
    spreadsheet.sheets.select do |sheet|
      g_spreadsheet_options.sheet_ids.include?(sheet.properties.sheet_id.to_s)
    end.map do |sheet|
      get_sheet_values(spreadsheet_id: g_spreadsheet_options.spreadsheet_id, sheet: sheet)
    end
  end
end