google-cloud-bigquery 0.20.1 → 0.20.2

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: f6129a4396ad062d0e2650f3e535fedbca11ee0e
4
- data.tar.gz: 0899727f3944a1880bf3ffb19c509200e15702c2
3
+ metadata.gz: f16696b5a90b556871480222793c4e66d27c9fe6
4
+ data.tar.gz: b6c55807e67d5888773bccf1dabee042b23a9abe
5
5
  SHA512:
6
- metadata.gz: 058e23892c9ec64ac3823816e25a0c55a2d572f0885972db63b89d06d4510ee29e0aefc7d77cea756da90d291f244faa8aba8e7a8acb8084e1ea5ba64773199d
7
- data.tar.gz: 6b98a5e9edd5daa77b03c525adc5c08602419e4c3bb88e2e5f0ee9180515c14b42925ac433dfed73d54286f15255f8de72f807f6d25ca7653e2877a2f337fd37
6
+ metadata.gz: f07cece664a25f4962b72aef938fc39ff560e2aefbe32eabb375f8e98303f016d032fddce70de0a8f8887b2d4e49f5aa3e0b1e358a09a675d650a082d8984d61
7
+ data.tar.gz: 7b3ebbc30c72952601caf1f293fb8913ce51e9be6b22e2b783baaf7d26b378648009a7bec603b94823d2558befbe9cadcd362de701c3b71905704f45bc0b6d0c
@@ -20,6 +20,7 @@ require "google/cloud/bigquery/credentials"
20
20
  require "google/cloud/bigquery/dataset"
21
21
  require "google/cloud/bigquery/job"
22
22
  require "google/cloud/bigquery/query_data"
23
+ require "google/cloud/bigquery/project/list"
23
24
 
24
25
  module Google
25
26
  module Cloud
@@ -35,7 +36,12 @@ module Google
35
36
  # Google BigQuery. {Google::Cloud::Bigquery::Dataset} objects are created,
36
37
  # accessed, and deleted by Google::Cloud::Bigquery::Project.
37
38
  #
38
- # See {Google::Cloud#bigquery}
39
+ # See {Google::Cloud#bigquery}.
40
+ #
41
+ # @attr_reader [String, nil] name The descriptive name of the project.
42
+ # Can only be present if the project was retrieved with {#projects}.
43
+ # @attr_reader [Integer, nil] numeric_id The numeric ID of the project.
44
+ # Can only be present if the project was retrieved with {#projects}.
39
45
  #
40
46
  # @example
41
47
  # require "google/cloud"
@@ -50,6 +56,8 @@ module Google
50
56
  # @private The Service object.
51
57
  attr_accessor :service
52
58
 
59
+ attr_reader :name, :numeric_id
60
+
53
61
  ##
54
62
  # Creates a new Service instance.
55
63
  #
@@ -468,6 +476,75 @@ module Google
468
476
  Job::List.from_gapi gapi, service, all, max, filter
469
477
  end
470
478
 
479
+ ##
480
+ # Retrieves the list of all projects for which the currently authorized
481
+ # account has been granted any project role. The returned project
482
+ # instances share the same credentials as the project used to retrieve
483
+ # them, but lazily create a new API connection for interactions with the
484
+ # BigQuery service.
485
+ #
486
+ # @param [String] token A previously-returned page token representing
487
+ # part of the larger set of results to view.
488
+ # @param [Integer] max Maximum number of projects to return.
489
+ #
490
+ # @return [Array<Google::Cloud::Bigquery::Project>] (See
491
+ # {Google::Cloud::Bigquery::Project::List})
492
+ #
493
+ # @example
494
+ # require "google/cloud"
495
+ #
496
+ # gcloud = Google::Cloud.new
497
+ # bigquery = gcloud.bigquery
498
+ #
499
+ # projects = bigquery.projects
500
+ # projects.each do |project|
501
+ # puts project.name
502
+ # project.datasets.all.each do |dataset|
503
+ # puts dataset.name
504
+ # end
505
+ # end
506
+ #
507
+ # @example Retrieve all projects: (See {Project::List#all})
508
+ # require "google/cloud"
509
+ #
510
+ # gcloud = Google::Cloud.new
511
+ # bigquery = gcloud.bigquery
512
+ #
513
+ # projects = bigquery.projects
514
+ #
515
+ # projects.all do |project|
516
+ # puts project.name
517
+ # project.datasets.all.each do |dataset|
518
+ # puts dataset.name
519
+ # end
520
+ # end
521
+ #
522
+ def projects token: nil, max: nil
523
+ ensure_service!
524
+ options = { token: token, max: max }
525
+ gapi = service.list_projects options
526
+ Project::List.from_gapi gapi, service, max
527
+ end
528
+
529
+ ##
530
+ # @private New Project from a Google API Client object, using the
531
+ # same Credentials as this project.
532
+ def self.from_gapi gapi, service
533
+ project_service = Service.new gapi.project_reference.project_id,
534
+ service.credentials,
535
+ retries: service.retries,
536
+ timeout: service.timeout
537
+ new(project_service).tap do |p|
538
+ p.instance_variable_set :@name, gapi.friendly_name
539
+
540
+ # TODO: remove `Integer` and set normally after migrating to Gax or
541
+ # to google-api-client 0.10 (See google/google-api-ruby-client#439)
542
+ if gapi.numeric_id
543
+ p.instance_variable_set :@numeric_id, Integer(gapi.numeric_id)
544
+ end
545
+ end
546
+ end
547
+
471
548
  protected
472
549
 
473
550
  ##
@@ -0,0 +1,171 @@
1
+ # Copyright 2016 Google Inc. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ require "delegate"
17
+
18
+ module Google
19
+ module Cloud
20
+ module Bigquery
21
+ class Project
22
+ ##
23
+ # Project::List is a special case Array with additional values.
24
+ class List < DelegateClass(::Array)
25
+ ##
26
+ # If not empty, indicates that there are more records that match
27
+ # the request and this value should be passed to continue.
28
+ attr_accessor :token
29
+
30
+ # A hash of this page of results.
31
+ attr_accessor :etag
32
+
33
+ ##
34
+ # @private Create a new Project::List with an array of
35
+ # Project instances.
36
+ def initialize arr = []
37
+ super arr
38
+ end
39
+
40
+ ##
41
+ # Whether there is a next page of projects.
42
+ #
43
+ # @return [Boolean]
44
+ #
45
+ # @example
46
+ # require "google/cloud"
47
+ #
48
+ # gcloud = Google::Cloud.new
49
+ # bigquery = gcloud.bigquery
50
+ #
51
+ # projects = bigquery.projects
52
+ # if projects.next?
53
+ # next_projects = projects.next
54
+ # end
55
+ def next?
56
+ !token.nil?
57
+ end
58
+
59
+ ##
60
+ # Retrieve the next page of projects.
61
+ #
62
+ # @return [Project::List]
63
+ #
64
+ # @example
65
+ # require "google/cloud"
66
+ #
67
+ # gcloud = Google::Cloud.new
68
+ # bigquery = gcloud.bigquery
69
+ #
70
+ # projects = bigquery.projects
71
+ # if projects.next?
72
+ # next_projects = projects.next
73
+ # end
74
+ def next
75
+ return nil unless next?
76
+ ensure_service!
77
+ options = { all: @hidden, token: token, max: @max }
78
+ gapi = @service.list_projects options
79
+ self.class.from_gapi gapi, @service, @max
80
+ end
81
+
82
+ ##
83
+ # Retrieves all projects by repeatedly loading {#next} until {#next?}
84
+ # returns `false`. Calls the given block once for each project, which
85
+ # is passed as the parameter.
86
+ #
87
+ # An Enumerator is returned if no block is given.
88
+ #
89
+ # This method may make several API calls until all projects are
90
+ # retrieved. Be sure to use as narrow a search criteria as possible.
91
+ # Please use with caution.
92
+ #
93
+ # @param [Integer] request_limit The upper limit of API requests to
94
+ # make to load all projects. Default is no limit.
95
+ # @yield [project] The block for accessing each project.
96
+ # @yieldparam [Project] project The project object.
97
+ #
98
+ # @return [Enumerator]
99
+ #
100
+ # @example Iterating each result by passing a block:
101
+ # require "google/cloud"
102
+ #
103
+ # gcloud = Google::Cloud.new
104
+ # bigquery = gcloud.bigquery
105
+ #
106
+ # bigquery.projects.all do |project|
107
+ # puts project.name
108
+ # end
109
+ #
110
+ # @example Using the enumerator by not passing a block:
111
+ # require "google/cloud"
112
+ #
113
+ # gcloud = Google::Cloud.new
114
+ # bigquery = gcloud.bigquery
115
+ #
116
+ # all_project_ids = bigquery.projects.all.map do |project|
117
+ # project.name
118
+ # end
119
+ #
120
+ # @example Limit the number of API calls made:
121
+ # require "google/cloud"
122
+ #
123
+ # gcloud = Google::Cloud.new
124
+ # bigquery = gcloud.bigquery
125
+ #
126
+ # bigquery.projects.all(request_limit: 10) do |project|
127
+ # puts project.name
128
+ # end
129
+ #
130
+ def all request_limit: nil
131
+ request_limit = request_limit.to_i if request_limit
132
+ unless block_given?
133
+ return enum_for(:all, request_limit: request_limit)
134
+ end
135
+ results = self
136
+ loop do
137
+ results.each { |r| yield r }
138
+ if request_limit
139
+ request_limit -= 1
140
+ break if request_limit < 0
141
+ end
142
+ break unless results.next?
143
+ results = results.next
144
+ end
145
+ end
146
+
147
+ ##
148
+ # @private New Project::List from a response object.
149
+ def self.from_gapi gapi_list, service, max = nil
150
+ projects = List.new(Array(gapi_list.projects).map do |gapi_object|
151
+ Project.from_gapi gapi_object, service
152
+ end)
153
+ projects.instance_variable_set :@token, gapi_list.next_page_token
154
+ projects.instance_variable_set :@etag, gapi_list.etag
155
+ projects.instance_variable_set :@service, service
156
+ projects.instance_variable_set :@max, max
157
+ projects
158
+ end
159
+
160
+ protected
161
+
162
+ ##
163
+ # Raise an error unless an active service is available.
164
+ def ensure_service!
165
+ fail "Must have active connection" unless @service
166
+ end
167
+ end
168
+ end
169
+ end
170
+ end
171
+ end
@@ -36,25 +36,32 @@ module Google
36
36
  # @private
37
37
  attr_accessor :credentials
38
38
 
39
+ # @private
40
+ attr_reader :retries, :timeout
41
+
39
42
  ##
40
43
  # Creates a new Service instance.
41
44
  def initialize project, credentials, retries: nil, timeout: nil
42
45
  @project = project
43
46
  @credentials = credentials
44
47
  @credentials = credentials
45
- @service = API::BigqueryService.new
46
- @service.client_options.application_name = "google-cloud-bigquery"
47
- @service.client_options.application_version = \
48
- Google::Cloud::Bigquery::VERSION
49
- @service.request_options.retries = retries || 3
50
- @service.request_options.timeout_sec = timeout
51
- @service.request_options.open_timeout_sec = timeout
52
- @service.authorization = @credentials.client
48
+ @retries = retries
49
+ @timeout = timeout
53
50
  end
54
51
 
55
52
  def service
56
53
  return mocked_service if mocked_service
57
- @service
54
+ @service ||= begin
55
+ service = API::BigqueryService.new
56
+ service.client_options.application_name = "google-cloud-bigquery"
57
+ service.client_options.application_version = \
58
+ Google::Cloud::Bigquery::VERSION
59
+ service.request_options.retries = @retries || 3
60
+ service.request_options.timeout_sec = @timeout
61
+ service.request_options.open_timeout_sec = @timeout
62
+ service.authorization = @credentials.client
63
+ service
64
+ end
58
65
  end
59
66
  attr_accessor :mocked_service
60
67
 
@@ -278,6 +285,15 @@ module Google
278
285
  Google::Apis::BigqueryV2::TableReference.new new_table_ref_hash
279
286
  end
280
287
 
288
+ ##
289
+ # Lists all projects to which you have been granted any project role.
290
+ def list_projects options = {}
291
+ execute do
292
+ service.list_projects max_results: options[:max],
293
+ page_token: options[:token]
294
+ end
295
+ end
296
+
281
297
  def inspect
282
298
  "#{self.class}(#{@project})"
283
299
  end
@@ -16,7 +16,7 @@
16
16
  module Google
17
17
  module Cloud
18
18
  module Bigquery
19
- VERSION = "0.20.1"
19
+ VERSION = "0.20.2"
20
20
  end
21
21
  end
22
22
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-cloud-bigquery
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.20.1
4
+ version: 0.20.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Moore
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-09-03 00:00:00.000000000 Z
12
+ date: 2016-10-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: google-cloud-core
@@ -151,6 +151,20 @@ dependencies:
151
151
  - - "~>"
152
152
  - !ruby/object:Gem::Version
153
153
  version: '0.9'
154
+ - !ruby/object:Gem::Dependency
155
+ name: yard-doctest
156
+ requirement: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - "~>"
159
+ - !ruby/object:Gem::Version
160
+ version: 0.1.6
161
+ type: :development
162
+ prerelease: false
163
+ version_requirements: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - "~>"
166
+ - !ruby/object:Gem::Version
167
+ version: 0.1.6
154
168
  description: google-cloud-bigquery is the official library for Google BigQuery.
155
169
  email:
156
170
  - mike@blowmage.com
@@ -173,6 +187,7 @@ files:
173
187
  - lib/google/cloud/bigquery/job/list.rb
174
188
  - lib/google/cloud/bigquery/load_job.rb
175
189
  - lib/google/cloud/bigquery/project.rb
190
+ - lib/google/cloud/bigquery/project/list.rb
176
191
  - lib/google/cloud/bigquery/query_data.rb
177
192
  - lib/google/cloud/bigquery/query_job.rb
178
193
  - lib/google/cloud/bigquery/schema.rb
@@ -201,7 +216,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
201
216
  version: '0'
202
217
  requirements: []
203
218
  rubyforge_project:
204
- rubygems_version: 2.6.4
219
+ rubygems_version: 2.5.1
205
220
  signing_key:
206
221
  specification_version: 4
207
222
  summary: API Client library for Google BigQuery