class Purtea::SheetApi

Interacts with the Google Sheets API.

Constants

APP_NAME
CREDENTIALS_PATH
OOB_URI
SCOPE
TOKEN_PATH
USER_ID

Attributes

service[R]
spreadsheet_id[R]

Public Class Methods

new(spreadsheet_id) click to toggle source
# File lib/purtea/sheet.rb, line 27
def initialize(spreadsheet_id)
  @service = Google::Apis::SheetsV4::SheetsService.new
  @service.client_options.application_name = APP_NAME
  @service.authorization = authorize!
  @spreadsheet_id = spreadsheet_id
end

Public Instance Methods

append(range, data) click to toggle source
# File lib/purtea/sheet.rb, line 34
def append(range, data)
  vr = Google::Apis::SheetsV4::ValueRange.new values: data
  @service.append_spreadsheet_value(
    @spreadsheet_id,
    range,
    vr,
    insert_data_option: 'INSERT_ROWS',
    value_input_option: 'RAW'
  )
end

Private Instance Methods

authorize!() click to toggle source
# File lib/purtea/sheet.rb, line 47
    def authorize!
      unless File.exist?(CREDENTIALS_PATH)
        config_dir = Purtea::Config.resolve_directory
        abort <<~MSG
          Could not find Google credentials file (google_credentials.json).
          Please download it from the Google console and place it in Purtea's
          config directory: #{config_dir}
        MSG
      end

      client_id = Google::Auth::ClientId.from_file CREDENTIALS_PATH
      token_store = Google::Auth::Stores::FileTokenStore.new file: TOKEN_PATH
      authorizer = Google::Auth::UserAuthorizer.new client_id, SCOPE,
                                                    token_store
      @credentials = get_credentials authorizer
    end
get_credentials(authorizer) click to toggle source
# File lib/purtea/sheet.rb, line 64
def get_credentials(authorizer)
  credentials = authorizer.get_credentials USER_ID

  if credentials.nil? || credentials.expired?
    Purtea.logger.info(
      "Google credentials don't exist or expired, acquiring new ones"
    )
    url = authorizer.get_authorization_url base_url: OOB_URI
    code = prompt_code url
    authorizer.get_and_store_credentials_from_code(
      user_id: USER_ID, code: code, base_url: OOB_URI
    )
  end

  credentials
end
prompt_code(url) click to toggle source
# File lib/purtea/sheet.rb, line 81
def prompt_code(url)
  puts 'Open the following URL in the browser and enter the ' \
    "resulting code after authorization:\n#{url}"
  $stdin.gets
end