Class: Bq::InstalledApp

Inherits:
Base
  • Object
show all
Defined in:
lib/bq.rb

Instance Attribute Summary

Attributes inherited from Base

#project_id, #token

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Bq::Base

Instance Method Details

#authorize(client_secrets_json = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/bq.rb', line 34

def authorize(client_secrets_json=nil)
  client_secrets_json ||= "client_secrets.json"
  puts "read from #{client_secrets_json}"
  credential = Google::APIClient::ClientSecrets.load(client_secrets_json)

  flow = Google::APIClient::InstalledAppFlow.new(
    :client_id     => credential.client_id,
    :client_secret => credential.client_secret,
    :scope         => ['https://www.googleapis.com/auth/bigquery']
  )
  @client.authorization = flow.authorize
  # Here, will be opened authorization web page.
  # Click [Authorize] button, and I see "Error: redirect_uri_mismatch".
  # But it may be succeed, authorize arguments is available.

  unless @client.authorization
    puts "failed to authorize. Canceled?"
    return nil
  end

  @token = {
    "scope"         => @client.authorization.scope,
    "client_id"     => @client.authorization.client_id,
    "client_secret" => @client.authorization.client_secret,
    "access_token"  => @client.authorization.access_token,
    "refresh_token" => @client.authorization.refresh_token
  }.freeze

  return @token
end

#datasetsObject



65
66
67
68
69
70
71
# File 'lib/bq.rb', line 65

def datasets
  result = @client.execute(
    :api_method => @bq_client.datasets.list,
    :parameters => {'projectId' => @project_id}
  )
  return result.data
end

#query(q, timeout = 90) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/bq.rb', line 73

def query(q,timeout=90)
  result = @client.execute(
    :api_method  => @bq_client.jobs.query,
    :body_object => {
      "query"     => q,
      "timeoutMs" => timeout * 1000
    },
    :parameters => {'projectId' => @project_id}
  )
  return result.data
end