bigquery-client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /.ruby-version
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+ /vendor/
12
+ *.bundle
13
+ *.so
14
+ *.o
15
+ *.a
16
+ mkmf.log
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1.0
7
+ - 2.2.0
8
+
9
+ script: bundle exec rake test
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Tsukuru Tanimichi
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,53 @@
1
+ # BigQuery Client [![Build Status](https://travis-ci.org/ttanimichi/bigquery-client.png)](https://travis-ci.org/ttanimichi/bigquery-client)
2
+
3
+ A Ruby interface to the BigQuery API.
4
+
5
+ ## Installation
6
+
7
+ ```
8
+ % gem install bigquery-client
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ruby
14
+ require "bigquery-client"
15
+
16
+ client = BigQuery::Client.new(
17
+ project: "your-project-42",
18
+ dataset: "your_dataset",
19
+ email: "1234567890@developer.gserviceaccount.com",
20
+ private_key_path: "/path/to/keyfile.p12",
21
+ private_key_passphrase: "notasecret",
22
+ auth_method: "private_key"
23
+ )
24
+
25
+ # insert
26
+ client.insert("your_table", uid: "john", age: 42)
27
+
28
+ # insert multiple rows
29
+ rows = [
30
+ { uid: "foo", age: 43 },
31
+ { uid: "bar", age: 44 }
32
+ ]
33
+ client.insert("your_table", rows)
34
+
35
+ # create table
36
+ schema = [
37
+ { name: "foo", type: "timestamp" },
38
+ { name: "bar", type: "string" }
39
+ ]
40
+ client.create_table("new_table", schema)
41
+
42
+ # fetch schema
43
+ client.fetch_schema("your_table")
44
+ #=> [{"name"=>"uid", "type"=>"STRING"}, {"name"=>"age", "type"=>"INTEGER"}]
45
+ ```
46
+
47
+ ## Contributing
48
+
49
+ 1. Fork it ( https://github.com/ttanimichi/bigquery-client/fork )
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am "Add some feature"`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create a new Pull Request
@@ -0,0 +1,5 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) { |task| task.libs << 'test' }
5
+ task default: :test
@@ -0,0 +1,28 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'bigquery-client/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'bigquery-client'
7
+ spec.version = BigQuery::Client::VERSION
8
+ spec.authors = ['Tsukuru Tanimichi']
9
+ spec.email = ['ttanimichi@hotmail.com']
10
+ spec.summary = 'A Ruby interface to the BigQuery API.'
11
+ spec.homepage = 'https://github.com/ttanimichi/bigquery-client'
12
+ spec.license = 'MIT'
13
+
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ['lib']
18
+
19
+ spec.required_ruby_version = '>= 1.9.3'
20
+
21
+ spec.add_runtime_dependency 'google-api-client', '~> 0.8.0'
22
+
23
+ spec.add_development_dependency 'rake'
24
+ spec.add_development_dependency 'bundler'
25
+ spec.add_development_dependency 'rr'
26
+ spec.add_development_dependency 'test-unit', '~> 3.0.2'
27
+ spec.add_development_dependency 'test-unit-rr', '~> 1.0.3'
28
+ end
@@ -0,0 +1,3 @@
1
+ require 'bigquery-client/client'
2
+ require 'bigquery-client/errors'
3
+ require 'bigquery-client/version'
@@ -0,0 +1,112 @@
1
+ require 'json'
2
+ require 'google/api_client'
3
+ require 'google/api_client/client_secrets'
4
+ require 'google/api_client/auth/installed_app'
5
+ require 'google/api_client/auth/compute_service_account'
6
+
7
+ module BigQuery
8
+ class Client
9
+ def initialize(attributes = {})
10
+ attributes.each { |name, value| instance_variable_set("@#{name}", value) }
11
+ end
12
+
13
+ def insert(table, args)
14
+ rows = args.is_a?(Array) ? args : [args]
15
+ result = access_api(
16
+ api_method: bigquery.tabledata.insert_all,
17
+ parameters: {
18
+ tableId: table
19
+ },
20
+ body_object: {
21
+ rows: rows.map { |row| { json: row } }
22
+ }
23
+ )
24
+ handle_error(result) if result.error?
25
+ end
26
+
27
+ def create_table(name, schema)
28
+ result = access_api(
29
+ api_method: bigquery.tables.insert,
30
+ body_object: {
31
+ tableReference: {
32
+ tableId: name
33
+ },
34
+ schema: {
35
+ fields: schema
36
+ }
37
+ }
38
+ )
39
+ handle_error(result) if result.error?
40
+ end
41
+
42
+ def fetch_schema(table)
43
+ result = access_api(
44
+ api_method: bigquery.tables.get,
45
+ parameters: {
46
+ tableId: table
47
+ }
48
+ )
49
+ handle_error(result) if result.error?
50
+ JSON.parse(result.body)['schema']['fields']
51
+ end
52
+
53
+ private
54
+
55
+ def access_api(params = {})
56
+ params[:parameters] ||= {}
57
+ params[:parameters][:projectId] ||= @project
58
+ params[:parameters][:datasetId] ||= @dataset
59
+ client.execute(params)
60
+ end
61
+
62
+ def bigquery
63
+ @bigquery ||= client.discovered_api('bigquery', 'v2')
64
+ end
65
+
66
+ def handle_error(result)
67
+ @client = nil
68
+ error =
69
+ case result.status
70
+ when 404 then NotFound
71
+ when 409 then Conflict
72
+ when 400..499 then ClientError
73
+ when 500..599 then ServerError
74
+ else UnexpectedError
75
+ end
76
+ fail error, result.error_message
77
+ end
78
+
79
+ def client
80
+ @client = nil if expired?
81
+ unless @client
82
+ @client = Google::APIClient.new(
83
+ application_name: 'bigquery-client',
84
+ application_version: BigQuery::Client::VERSION
85
+ )
86
+ authorize_client
87
+ @expiration = Time.now + 1800
88
+ end
89
+ @client
90
+ end
91
+
92
+ def expired?
93
+ @expiration && @expiration < Time.now
94
+ end
95
+
96
+ def authorize_client
97
+ case @auth_method
98
+ when 'private_key'
99
+ asserter = Google::APIClient::JWTAsserter.new(
100
+ @email,
101
+ 'https://www.googleapis.com/auth/bigquery',
102
+ Google::APIClient::PKCS12.load_key(@private_key_path, @private_key_passphrase)
103
+ )
104
+ @client.authorization = asserter.authorize
105
+ when 'compute_engine'
106
+ auth = Google::APIClient::ComputeServiceAccount.new
107
+ auth.fetch_access_token!
108
+ @client.authorization = auth
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,23 @@
1
+ module BigQuery
2
+ class APIError < StandardError
3
+ end
4
+
5
+ # HTTP 4xx Client Error
6
+ class ClientError < APIError
7
+ end
8
+
9
+ # HTTP 404 Not Found
10
+ class NotFound < ClientError
11
+ end
12
+
13
+ # HTTP 409 Conflict
14
+ class Conflict < ClientError
15
+ end
16
+
17
+ # HTTP 5xx Server Error
18
+ class ServerError < APIError
19
+ end
20
+
21
+ class UnexpectedError < APIError
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ module BigQuery
2
+ class Client
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+
3
+ require 'bigquery-client'
4
+ require 'test/unit'
5
+ require 'test/unit/rr'
6
+ require 'rr'
@@ -0,0 +1,171 @@
1
+ require 'helper'
2
+
3
+ class ClientTest < Test::Unit::TestCase
4
+ def setup
5
+ @client = BigQuery::Client.new(
6
+ project: '1234567890',
7
+ dataset: 'my_dataset',
8
+ email: '1234567890@developer.gserviceaccount.com',
9
+ private_key_path: '/path/to/keyfile.p12',
10
+ private_key_passphrase: 'itsasecret',
11
+ auth_method: 'private_key'
12
+ )
13
+ end
14
+
15
+ def test_initialize
16
+ actual = @client.instance_variables
17
+ expected = [:@project, :@dataset, :@email, :@private_key_path, :@private_key_passphrase, :@auth_method]
18
+ assert { actual.sort == expected.sort }
19
+ end
20
+
21
+ def test_create_table
22
+ params = {
23
+ api_method: 'create_table',
24
+ body_object: {
25
+ tableReference: {
26
+ tableId: 'my_table'
27
+ },
28
+ schema: {
29
+ fields: [
30
+ { name: 'foo', type: 'timestamp' },
31
+ { name: 'bar', type: 'string' }
32
+ ]
33
+ }
34
+ },
35
+ parameters: {
36
+ projectId: '1234567890',
37
+ datasetId: 'my_dataset'
38
+ }
39
+ }
40
+ auth = Object.new
41
+ mock(client = Object.new) do |client|
42
+ client.discovered_api('bigquery', 'v2') { mock!.tables.mock!.insert { 'create_table' } }
43
+ client.execute(params) { mock(Object.new).error? { false } }
44
+ client.authorization = auth
45
+ end
46
+ mock(Google::APIClient).new.with_any_args { client }
47
+ mock(Google::APIClient::JWTAsserter).new.with_any_args { mock(Object.new).authorize { auth } }
48
+ mock(Google::APIClient::PKCS12).load_key('/path/to/keyfile.p12', 'itsasecret')
49
+ schema = [{name: 'foo', type: 'timestamp'}, { name: 'bar', type: 'string'}]
50
+ assert { @client.create_table('my_table', schema).nil? }
51
+ end
52
+
53
+ def test_insert
54
+ params = {
55
+ api_method: 'insert_all',
56
+ parameters: {
57
+ projectId: '1234567890',
58
+ datasetId: 'my_dataset',
59
+ tableId: 'my_table'
60
+ },
61
+ body_object: {
62
+ rows: [ { json: { a: 'b', c: 'd' } } ]
63
+ }
64
+ }
65
+ auth = Object.new
66
+ mock(client = Object.new) do |client|
67
+ client.discovered_api('bigquery', 'v2') { mock!.tabledata.mock!.insert_all { 'insert_all' } }
68
+ client.execute(params) { mock(Object.new).error? { false } }
69
+ client.authorization = auth
70
+ end
71
+ mock(Google::APIClient).new.with_any_args { client }
72
+ mock(Google::APIClient::JWTAsserter).new.with_any_args { mock(Object.new).authorize { auth } }
73
+ mock(Google::APIClient::PKCS12).load_key('/path/to/keyfile.p12', 'itsasecret')
74
+ assert { @client.insert('my_table', a: 'b', c: 'd').nil? }
75
+ end
76
+
77
+ def test_insert_rows
78
+ params = {
79
+ api_method: 'insert_all',
80
+ parameters: {
81
+ projectId: '1234567890',
82
+ datasetId: 'my_dataset',
83
+ tableId: 'my_table'
84
+ },
85
+ body_object: {
86
+ rows: [
87
+ { json: { 'a' => 'b' } },
88
+ { json: { 'b' => 'c' } }
89
+ ]
90
+ }
91
+ }
92
+ auth = Object.new
93
+ mock(client = Object.new) do |client|
94
+ client.discovered_api('bigquery', 'v2') { mock!.tabledata.mock!.insert_all { 'insert_all' } }
95
+ client.execute(params) { mock(Object.new).error? { false } }
96
+ client.authorization = auth
97
+ end
98
+ mock(Google::APIClient).new.with_any_args { client }
99
+ mock(Google::APIClient::JWTAsserter).new.with_any_args { mock(Object.new).authorize { auth } }
100
+ mock(Google::APIClient::PKCS12).load_key('/path/to/keyfile.p12', 'itsasecret')
101
+ rows = [{'a' => 'b'}, {'b' => 'c'}]
102
+ assert { @client.insert('my_table', rows).nil? }
103
+ end
104
+
105
+ def test_fetch_schema
106
+ params = {
107
+ api_method: 'tables_get',
108
+ parameters: {
109
+ tableId: 'my_table',
110
+ projectId: '1234567890',
111
+ datasetId: 'my_dataset'
112
+ }
113
+ }
114
+ result_body = JSON.generate(
115
+ {
116
+ schema: {
117
+ fields: [
118
+ { name: 'time', type: 'TIMESTAMP' },
119
+ { name: 'tty', type: 'STRING' }
120
+ ]
121
+ }
122
+ }
123
+ )
124
+ auth = Object.new
125
+ mock(result = Object.new) do |result|
126
+ result.error? { false }
127
+ result.body { result_body }
128
+ end
129
+ mock(client = Object.new) do |client|
130
+ client.discovered_api('bigquery', 'v2') { mock!.tables.mock!.get { 'tables_get' } }
131
+ client.execute(params) { result }
132
+ client.authorization = auth
133
+ end
134
+ mock(Google::APIClient).new.with_any_args { client }
135
+ mock(Google::APIClient::JWTAsserter).new.with_any_args { mock(Object.new).authorize { auth } }
136
+ mock(Google::APIClient::PKCS12).load_key('/path/to/keyfile.p12', 'itsasecret')
137
+ expected = [
138
+ { 'name' => 'time', 'type' => 'TIMESTAMP' },
139
+ { 'name' => 'tty', 'type' => 'STRING' }
140
+ ]
141
+ assert { @client.fetch_schema('my_table') == expected }
142
+ end
143
+
144
+ def test_errors
145
+ errors = [
146
+ { code: 404, klass: BigQuery::NotFound },
147
+ { code: 409, klass: BigQuery::Conflict },
148
+ { code: 403, klass: BigQuery::ClientError },
149
+ { code: 503, klass: BigQuery::ServerError },
150
+ { code: 301, klass: BigQuery::UnexpectedError }
151
+ ]
152
+ errors.each do |error|
153
+ auth = Object.new
154
+ mock(result = Object.new) do |result|
155
+ result.error? { true }
156
+ result.status { error[:code] }
157
+ result.error_message { 'this is an error message' }
158
+ end
159
+ mock(client = Object.new) do |client|
160
+ client.execute.with_any_args { result }
161
+ client.authorization = auth
162
+ end
163
+ mock(@client).bigquery { mock!.tabledata.mock!.insert_all }
164
+ mock(Google::APIClient).new.with_any_args { client }
165
+ mock(Google::APIClient::JWTAsserter).new.with_any_args { mock(Object.new).authorize { auth } }
166
+ mock(Google::APIClient::PKCS12).load_key('/path/to/keyfile.p12', 'itsasecret')
167
+ rows = [{'a' => 'b'}, {'b' => 'c'}]
168
+ assert_raise(error[:klass]) { @client.insert('my_table', rows) }
169
+ end
170
+ end
171
+ end
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bigquery-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tsukuru Tanimichi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-01-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: google-api-client
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.8.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 0.8.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rr
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: test-unit
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 3.0.2
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 3.0.2
94
+ - !ruby/object:Gem::Dependency
95
+ name: test-unit-rr
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: 1.0.3
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 1.0.3
110
+ description:
111
+ email:
112
+ - ttanimichi@hotmail.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - .gitignore
118
+ - .travis.yml
119
+ - Gemfile
120
+ - LICENSE.txt
121
+ - README.md
122
+ - Rakefile
123
+ - bigquery-client.gemspec
124
+ - lib/bigquery-client.rb
125
+ - lib/bigquery-client/client.rb
126
+ - lib/bigquery-client/errors.rb
127
+ - lib/bigquery-client/version.rb
128
+ - test/helper.rb
129
+ - test/test_client.rb
130
+ homepage: https://github.com/ttanimichi/bigquery-client
131
+ licenses:
132
+ - MIT
133
+ post_install_message:
134
+ rdoc_options: []
135
+ require_paths:
136
+ - lib
137
+ required_ruby_version: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ! '>='
141
+ - !ruby/object:Gem::Version
142
+ version: 1.9.3
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ none: false
145
+ requirements:
146
+ - - ! '>='
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ segments:
150
+ - 0
151
+ hash: -1636212372983959579
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 1.8.23.2
155
+ signing_key:
156
+ specification_version: 3
157
+ summary: A Ruby interface to the BigQuery API.
158
+ test_files:
159
+ - test/helper.rb
160
+ - test/test_client.rb