class BigQuery::Client

Constants

VERSION

Public Class Methods

new(attributes = {}) click to toggle source
# File lib/bigquery-client/client.rb, line 17
def initialize(attributes = {})
  attributes.each { |name, value| instance_variable_set("@#{name}", value) }
end

Private Instance Methods

access_api(params = {}) click to toggle source
# File lib/bigquery-client/client.rb, line 21
def access_api(params = {})
  params[:parameters] ||= {}
  params[:parameters][:projectId] ||= @project
  params[:parameters][:datasetId] ||= @dataset
  result = client.execute(params)
  handle_error(result) if result.error?
  JSON.parse(result.body) unless result.body.empty?
end
authorize_client() click to toggle source
# File lib/bigquery-client/client.rb, line 64
def authorize_client
  case @auth_method
  when 'private_key'
    asserter = Google::APIClient::JWTAsserter.new(
      @email,
      'https://www.googleapis.com/auth/bigquery',
      Google::APIClient::PKCS12.load_key(@private_key_path, @private_key_passphrase)
    )
    @client.authorization = asserter.authorize
  when 'compute_engine'
    auth = Google::APIClient::ComputeServiceAccount.new
    auth.fetch_access_token!
    @client.authorization = auth
  end
end
bigquery() click to toggle source
# File lib/bigquery-client/client.rb, line 30
def bigquery
  @bigquery ||= client.discovered_api('bigquery', 'v2')
end
client() click to toggle source
# File lib/bigquery-client/client.rb, line 47
def client
  @client = nil if expired?
  unless @client
    @client = Google::APIClient.new(
      application_name:    'bigquery-client',
      application_version: BigQuery::Client::VERSION
    )
    authorize_client
    @expiration = Time.now + 1800
  end
  @client
end
expired?() click to toggle source
# File lib/bigquery-client/client.rb, line 60
def expired?
  @expiration && @expiration < Time.now
end
handle_error(result) click to toggle source
# File lib/bigquery-client/client.rb, line 34
def handle_error(result)
  @client = nil
  error =
    case result.status
    when 404      then NotFound
    when 409      then Conflict
    when 400..499 then ClientError
    when 500..599 then ServerError
    else UnexpectedError
    end
  fail error, result.error_message
end