kura 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2ef864675bf919edba8fa7dde30ecb2d6f006743
4
- data.tar.gz: 03a47e86b36c2592b4da083544862b6372095dc5
3
+ metadata.gz: 5bd51f30481828394075cc17c42f3031f56a51c2
4
+ data.tar.gz: cc6746437f90c04859b1d0f7e7cf6288f754334a
5
5
  SHA512:
6
- metadata.gz: 11d14b0676eeb2e49c2fa0f08af74c5aee00f8a673778187bea546daf6a5db47cea053ff05b6edeb6215b3ad4658469bd0009a2160fbe6b07e1da02ecdab925a
7
- data.tar.gz: 5fcfe50b25c5e6da3a6167da831963143859b0fd03655d6d214c8dd4b3b4ded3e2ccf859d8f82eadde39d3406a32572ec798d7e5e6bb9d63225e5a2e392d6623
6
+ metadata.gz: 5bb5f60f7b05bc12116cbc68f369c47dbc7368acfd4b03ca42f484577fa3acd1bb28b30437b508561c73de858b5d83b97cf59f27e4fd89634780e64fbaac16d3
7
+ data.tar.gz: becb95614ed113f44f4ae733055d2483b61d6a41878d09368e0eb85b5b34ad08fb6a6c42ac0c26e2c4f6c304d34daad7e066bfc261b40772525c481b0a083d48
data/ChangeLog.md CHANGED
@@ -1,3 +1,12 @@
1
+ # 0.2.5
2
+
3
+ ## Enhancements
4
+
5
+ * Add Kura::Client#batch and support Baches API call for #projects, #datasets, #dataset,
6
+ #tables, #table, #list\_tabledata. The results of these api call are passwed to blocks.
7
+ See also https://github.com/google/google-api-ruby-client#batches
8
+ The job insertion methods (load, query, extract, copy) are not supported to call in batch's block.
9
+
1
10
  # 0.2.4
2
11
 
3
12
  ## Enhancements
data/lib/kura/client.rb CHANGED
@@ -49,61 +49,93 @@ module Kura
49
49
  end
50
50
  private :process_error
51
51
 
52
- def projects(limit: 1000)
53
- result = @api.list_projects(max_results: limit)
54
- result.projects
52
+ def batch
53
+ @api.batch do |api|
54
+ original_api, @api = @api, api
55
+ begin
56
+ yield
57
+ ensure
58
+ @api = original_api
59
+ end
60
+ end
61
+ end
62
+
63
+ def projects(limit: 1000, &blk)
64
+ if blk
65
+ @api.list_projects(max_results: limit) do |result, err|
66
+ result &&= result.projects
67
+ blk.call(result, err)
68
+ end
69
+ else
70
+ result = @api.list_projects(max_results: limit)
71
+ result.projects
72
+ end
55
73
  rescue
56
74
  process_error($!)
57
75
  end
58
76
 
59
- def datasets(project_id: @default_project_id, all: false, limit: 1000)
60
- result = @api.list_datasets(project_id, all: all, max_results: limit)
61
- result.datasets
77
+ def datasets(project_id: @default_project_id, all: false, limit: 1000, &blk)
78
+ if blk
79
+ @api.list_datasets(project_id, all: all, max_results: limit) do |result, err|
80
+ result &&= result.datasets
81
+ blk.call(result, err)
82
+ end
83
+ else
84
+ result = @api.list_datasets(project_id, all: all, max_results: limit)
85
+ result.datasets
86
+ end
62
87
  rescue
63
88
  process_error($!)
64
89
  end
65
90
 
66
- def dataset(dataset_id, project_id: @default_project_id)
67
- @api.get_dataset(project_id, dataset_id)
91
+ def dataset(dataset_id, project_id: @default_project_id, &blk)
92
+ @api.get_dataset(project_id, dataset_id, &blk)
68
93
  rescue
69
94
  return nil if $!.respond_to?(:status_code) and $!.status_code == 404
70
95
  process_error($!)
71
96
  end
72
97
 
73
- def insert_dataset(dataset_id, project_id: @default_project_id)
98
+ def insert_dataset(dataset_id, project_id: @default_project_id, &blk)
74
99
  obj = Google::Apis::BigqueryV2::Dataset.new(dataset_reference: Google::Apis::BigqueryV2::DatasetReference.new(project_id: project_id, dataset_id: dataset_id))
75
- @api.insert_dataset(project_id, obj)
100
+ @api.insert_dataset(project_id, obj, &blk)
76
101
  rescue
77
102
  process_error($!)
78
103
  end
79
104
 
80
- def delete_dataset(dataset_id, project_id: @default_project_id, delete_contents: false)
81
- @api.delete_dataset(project_id, dataset_id, delete_contents: delete_contents)
105
+ def delete_dataset(dataset_id, project_id: @default_project_id, delete_contents: false, &blk)
106
+ @api.delete_dataset(project_id, dataset_id, delete_contents: delete_contents, &blk)
82
107
  rescue
83
108
  return nil if $!.respond_to?(:status_code) and $!.status_code == 404
84
109
  process_error($!)
85
110
  end
86
111
 
87
- def patch_dataset(dataset_id, project_id: @default_project_id, access: nil, description: :na, default_table_expiration_ms: :na, friendly_name: :na )
112
+ def patch_dataset(dataset_id, project_id: @default_project_id, access: nil, description: :na, default_table_expiration_ms: :na, friendly_name: :na, &blk)
88
113
  obj = Google::Apis::BigqueryV2::Dataset.new(dataset_reference: Google::Apis::BigqueryV2::DatasetReference.new(project_id: project_id, dataset_id: dataset_id))
89
114
  obj.access = access if access
90
115
  obj.default_table_expiration_ms = default_table_expiration_ms if default_table_expiration_ms != :na
91
116
  obj.description = description if description != :na
92
117
  obj.friendly_name = friendly_name if friendly_name != :na
93
- @api.patch_dataset(project_id, dataset_id, obj)
118
+ @api.patch_dataset(project_id, dataset_id, obj, &blk)
94
119
  rescue
95
120
  process_error($!)
96
121
  end
97
122
 
98
- def tables(dataset_id, project_id: @default_project_id, limit: 1000)
99
- result = @api.list_tables(project_id, dataset_id, max_results: limit)
100
- result.tables
123
+ def tables(dataset_id, project_id: @default_project_id, limit: 1000, &blk)
124
+ if blk
125
+ @api.list_tables(project_id, dataset_id, max_results: limit) do |result, err|
126
+ result &&= result.tables
127
+ blk.call(result, err)
128
+ end
129
+ else
130
+ result = @api.list_tables(project_id, dataset_id, max_results: limit)
131
+ result.tables
132
+ end
101
133
  rescue
102
134
  process_error($!)
103
135
  end
104
136
 
105
- def table(dataset_id, table_id, project_id: @default_project_id)
106
- @api.get_table(project_id, dataset_id, table_id)
137
+ def table(dataset_id, table_id, project_id: @default_project_id, &blk)
138
+ @api.get_table(project_id, dataset_id, table_id, &blk)
107
139
  rescue
108
140
  return nil if $!.respond_to?(:status_code) and $!.status_code == 404
109
141
  process_error($!)
@@ -111,7 +143,7 @@ module Kura
111
143
 
112
144
  def insert_table(dataset_id, table_id, project_id: @default_project_id, expiration_time: nil,
113
145
  friendly_name: nil, schema: nil, description: nil,
114
- query: nil, external_data_configuration: nil)
146
+ query: nil, external_data_configuration: nil, &blk)
115
147
  if expiration_time
116
148
  expiration_time = (expiration_time.to_f * 1000.0).to_i
117
149
  end
@@ -129,12 +161,12 @@ module Kura
129
161
  expiration_time: expiration_time,
130
162
  view: view,
131
163
  external_data_configuration: external_data_configuration)
132
- @api.insert_table(project_id, dataset_id, table)
164
+ @api.insert_table(project_id, dataset_id, table, &blk)
133
165
  rescue
134
166
  process_error($!)
135
167
  end
136
168
 
137
- def patch_table(dataset_id, table_id, project_id: @default_project_id, expiration_time: :na, friendly_name: :na, description: :na)
169
+ def patch_table(dataset_id, table_id, project_id: @default_project_id, expiration_time: :na, friendly_name: :na, description: :na, &blk)
138
170
  if expiration_time != :na and not(expiration_time.nil?)
139
171
  expiration_time = (expiration_time.to_f * 1000.0).to_i
140
172
  end
@@ -142,23 +174,19 @@ module Kura
142
174
  table.friendly_name = friendly_name if friendly_name != :na
143
175
  table.description = description if description != :na
144
176
  table.expiration_time = expiration_time if expiration_time != :na
145
- @api.patch_table(project_id, dataset_id, table_id, table)
177
+ @api.patch_table(project_id, dataset_id, table_id, table, &blk)
146
178
  rescue
147
179
  process_error($!)
148
180
  end
149
181
 
150
- def delete_table(dataset_id, table_id, project_id: @default_project_id)
151
- @api.delete_table(project_id, dataset_id, table_id)
182
+ def delete_table(dataset_id, table_id, project_id: @default_project_id, &blk)
183
+ @api.delete_table(project_id, dataset_id, table_id, &blk)
152
184
  rescue
153
185
  return nil if $!.respond_to?(:status_code) and $!.status_code == 404
154
186
  process_error($!)
155
187
  end
156
188
 
157
- def list_tabledata(dataset_id, table_id, project_id: @default_project_id, start_index: 0, max_result: 100, page_token: nil, schema: nil)
158
- schema ||= table(dataset_id, table_id, project_id: project_id).schema.fields
159
- field_names = schema.map{|f| f.respond_to?(:[]) ? (f["name"] || f[:name]) : f.name }
160
-
161
- r = @api.list_table_data(project_id, dataset_id, table_id, max_results: max_result, start_index: start_index, page_token: page_token)
189
+ def format_tabledata(r, field_names)
162
190
  {
163
191
  total_rows: r.total_rows.to_i,
164
192
  next_token: r.page_token,
@@ -166,6 +194,24 @@ module Kura
166
194
  row.f.zip(field_names).each_with_object({}) do |(v, fn), tbl| tbl[fn] = v.v end
167
195
  end
168
196
  }
197
+ end
198
+ private :format_tabledata
199
+
200
+ def list_tabledata(dataset_id, table_id, project_id: @default_project_id, start_index: 0, max_result: 100, page_token: nil, schema: nil, &blk)
201
+ schema ||= table(dataset_id, table_id, project_id: project_id).schema.fields
202
+ field_names = schema.map{|f| f.respond_to?(:[]) ? (f["name"] || f[:name]) : f.name }
203
+
204
+ if blk
205
+ @api.list_table_data(project_id, dataset_id, table_id, max_results: max_result, start_index: start_index, page_token: page_token) do |r, err|
206
+ if r
207
+ r = format_tabledata(r, field_names)
208
+ end
209
+ blk.call(r, err)
210
+ end
211
+ else
212
+ r = @api.list_table_data(project_id, dataset_id, table_id, max_results: max_result, start_index: start_index, page_token: page_token)
213
+ format_tabledata(r, field_names)
214
+ end
169
215
  rescue
170
216
  process_error($!)
171
217
  end
data/lib/kura/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Kura
2
- VERSION = "0.2.4"
2
+ VERSION = "0.2.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kura
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chikanaga Tomoyuki
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-11-13 00:00:00.000000000 Z
11
+ date: 2015-11-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: google-api-client