gcloud 0.12.2 → 0.20.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (169) hide show
  1. checksums.yaml +5 -13
  2. data/lib/gcloud.rb +27 -456
  3. data/lib/gcloud/bigquery.rb +2 -382
  4. data/lib/gcloud/datastore.rb +2 -576
  5. data/lib/gcloud/dns.rb +2 -321
  6. data/lib/gcloud/logging.rb +1 -322
  7. data/lib/gcloud/pubsub.rb +2 -476
  8. data/lib/gcloud/resource_manager.rb +2 -273
  9. data/lib/gcloud/storage.rb +2 -440
  10. data/lib/gcloud/translate.rb +1 -250
  11. data/lib/gcloud/version.rb +2 -2
  12. data/lib/gcloud/vision.rb +1 -501
  13. metadata +36 -332
  14. data/AUTHENTICATION.md +0 -75
  15. data/CHANGELOG.md +0 -382
  16. data/OVERVIEW.md +0 -259
  17. data/lib/gcloud/backoff.rb +0 -150
  18. data/lib/gcloud/bigquery/copy_job.rb +0 -97
  19. data/lib/gcloud/bigquery/credentials.rb +0 -29
  20. data/lib/gcloud/bigquery/data.rb +0 -239
  21. data/lib/gcloud/bigquery/dataset.rb +0 -753
  22. data/lib/gcloud/bigquery/dataset/access.rb +0 -507
  23. data/lib/gcloud/bigquery/dataset/list.rb +0 -169
  24. data/lib/gcloud/bigquery/extract_job.rb +0 -117
  25. data/lib/gcloud/bigquery/insert_response.rb +0 -81
  26. data/lib/gcloud/bigquery/job.rb +0 -299
  27. data/lib/gcloud/bigquery/job/list.rb +0 -172
  28. data/lib/gcloud/bigquery/load_job.rb +0 -202
  29. data/lib/gcloud/bigquery/project.rb +0 -475
  30. data/lib/gcloud/bigquery/query_data.rb +0 -234
  31. data/lib/gcloud/bigquery/query_job.rb +0 -137
  32. data/lib/gcloud/bigquery/schema.rb +0 -359
  33. data/lib/gcloud/bigquery/service.rb +0 -506
  34. data/lib/gcloud/bigquery/table.rb +0 -1141
  35. data/lib/gcloud/bigquery/table/list.rb +0 -180
  36. data/lib/gcloud/bigquery/view.rb +0 -475
  37. data/lib/gcloud/credentials.rb +0 -129
  38. data/lib/gcloud/datastore/commit.rb +0 -148
  39. data/lib/gcloud/datastore/credentials.rb +0 -35
  40. data/lib/gcloud/datastore/cursor.rb +0 -76
  41. data/lib/gcloud/datastore/dataset.rb +0 -660
  42. data/lib/gcloud/datastore/dataset/lookup_results.rb +0 -219
  43. data/lib/gcloud/datastore/dataset/query_results.rb +0 -386
  44. data/lib/gcloud/datastore/entity.rb +0 -449
  45. data/lib/gcloud/datastore/errors.rb +0 -41
  46. data/lib/gcloud/datastore/gql_query.rb +0 -211
  47. data/lib/gcloud/datastore/grpc_utils.rb +0 -132
  48. data/lib/gcloud/datastore/key.rb +0 -281
  49. data/lib/gcloud/datastore/properties.rb +0 -128
  50. data/lib/gcloud/datastore/query.rb +0 -348
  51. data/lib/gcloud/datastore/service.rb +0 -167
  52. data/lib/gcloud/datastore/transaction.rb +0 -362
  53. data/lib/gcloud/dns/change.rb +0 -158
  54. data/lib/gcloud/dns/change/list.rb +0 -173
  55. data/lib/gcloud/dns/credentials.rb +0 -29
  56. data/lib/gcloud/dns/importer.rb +0 -183
  57. data/lib/gcloud/dns/project.rb +0 -247
  58. data/lib/gcloud/dns/record.rb +0 -170
  59. data/lib/gcloud/dns/record/list.rb +0 -174
  60. data/lib/gcloud/dns/service.rb +0 -167
  61. data/lib/gcloud/dns/zone.rb +0 -759
  62. data/lib/gcloud/dns/zone/list.rb +0 -168
  63. data/lib/gcloud/dns/zone/transaction.rb +0 -176
  64. data/lib/gcloud/errors.rb +0 -206
  65. data/lib/gcloud/gce.rb +0 -56
  66. data/lib/gcloud/grpc_utils.rb +0 -87
  67. data/lib/gcloud/logging/credentials.rb +0 -29
  68. data/lib/gcloud/logging/entry.rb +0 -465
  69. data/lib/gcloud/logging/entry/http_request.rb +0 -141
  70. data/lib/gcloud/logging/entry/list.rb +0 -177
  71. data/lib/gcloud/logging/entry/operation.rb +0 -90
  72. data/lib/gcloud/logging/logger.rb +0 -307
  73. data/lib/gcloud/logging/metric.rb +0 -169
  74. data/lib/gcloud/logging/metric/list.rb +0 -172
  75. data/lib/gcloud/logging/project.rb +0 -642
  76. data/lib/gcloud/logging/resource.rb +0 -84
  77. data/lib/gcloud/logging/resource_descriptor.rb +0 -137
  78. data/lib/gcloud/logging/resource_descriptor/list.rb +0 -174
  79. data/lib/gcloud/logging/service.rb +0 -267
  80. data/lib/gcloud/logging/sink.rb +0 -227
  81. data/lib/gcloud/logging/sink/list.rb +0 -171
  82. data/lib/gcloud/pubsub/credentials.rb +0 -29
  83. data/lib/gcloud/pubsub/message.rb +0 -94
  84. data/lib/gcloud/pubsub/policy.rb +0 -204
  85. data/lib/gcloud/pubsub/project.rb +0 -482
  86. data/lib/gcloud/pubsub/received_message.rb +0 -160
  87. data/lib/gcloud/pubsub/service.rb +0 -334
  88. data/lib/gcloud/pubsub/subscription.rb +0 -565
  89. data/lib/gcloud/pubsub/subscription/list.rb +0 -208
  90. data/lib/gcloud/pubsub/topic.rb +0 -511
  91. data/lib/gcloud/pubsub/topic/list.rb +0 -174
  92. data/lib/gcloud/pubsub/topic/publisher.rb +0 -85
  93. data/lib/gcloud/resource_manager/credentials.rb +0 -30
  94. data/lib/gcloud/resource_manager/manager.rb +0 -266
  95. data/lib/gcloud/resource_manager/policy.rb +0 -211
  96. data/lib/gcloud/resource_manager/project.rb +0 -484
  97. data/lib/gcloud/resource_manager/project/list.rb +0 -167
  98. data/lib/gcloud/resource_manager/project/updater.rb +0 -130
  99. data/lib/gcloud/resource_manager/service.rb +0 -127
  100. data/lib/gcloud/storage/bucket.rb +0 -775
  101. data/lib/gcloud/storage/bucket/acl.rb +0 -810
  102. data/lib/gcloud/storage/bucket/cors.rb +0 -153
  103. data/lib/gcloud/storage/bucket/list.rb +0 -172
  104. data/lib/gcloud/storage/credentials.rb +0 -29
  105. data/lib/gcloud/storage/errors.rb +0 -65
  106. data/lib/gcloud/storage/file.rb +0 -842
  107. data/lib/gcloud/storage/file/acl.rb +0 -425
  108. data/lib/gcloud/storage/file/list.rb +0 -191
  109. data/lib/gcloud/storage/file/verifier.rb +0 -67
  110. data/lib/gcloud/storage/project.rb +0 -316
  111. data/lib/gcloud/storage/service.rb +0 -347
  112. data/lib/gcloud/translate/api.rb +0 -241
  113. data/lib/gcloud/translate/detection.rb +0 -137
  114. data/lib/gcloud/translate/language.rb +0 -69
  115. data/lib/gcloud/translate/service.rb +0 -80
  116. data/lib/gcloud/translate/translation.rb +0 -112
  117. data/lib/gcloud/vision/annotate.rb +0 -224
  118. data/lib/gcloud/vision/annotation.rb +0 -455
  119. data/lib/gcloud/vision/annotation/entity.rb +0 -234
  120. data/lib/gcloud/vision/annotation/face.rb +0 -1750
  121. data/lib/gcloud/vision/annotation/properties.rb +0 -245
  122. data/lib/gcloud/vision/annotation/safe_search.rb +0 -161
  123. data/lib/gcloud/vision/annotation/text.rb +0 -236
  124. data/lib/gcloud/vision/annotation/vertex.rb +0 -108
  125. data/lib/gcloud/vision/credentials.rb +0 -29
  126. data/lib/gcloud/vision/image.rb +0 -590
  127. data/lib/gcloud/vision/location.rb +0 -115
  128. data/lib/gcloud/vision/project.rb +0 -278
  129. data/lib/gcloud/vision/service.rb +0 -66
  130. data/lib/google/api/annotations.rb +0 -14
  131. data/lib/google/api/http.rb +0 -30
  132. data/lib/google/api/label.rb +0 -24
  133. data/lib/google/api/monitored_resource.rb +0 -25
  134. data/lib/google/datastore/v1beta3/datastore.rb +0 -115
  135. data/lib/google/datastore/v1beta3/datastore_services.rb +0 -33
  136. data/lib/google/datastore/v1beta3/entity.rb +0 -63
  137. data/lib/google/datastore/v1beta3/query.rb +0 -128
  138. data/lib/google/devtools/cloudtrace/v1/trace.rb +0 -78
  139. data/lib/google/devtools/cloudtrace/v1/trace_services.rb +0 -32
  140. data/lib/google/example/library/v1/library.rb +0 -91
  141. data/lib/google/example/library/v1/library_services.rb +0 -40
  142. data/lib/google/iam/v1/iam_policy.rb +0 -33
  143. data/lib/google/iam/v1/iam_policy_services.rb +0 -30
  144. data/lib/google/iam/v1/policy.rb +0 -25
  145. data/lib/google/logging/type/http_request.rb +0 -28
  146. data/lib/google/logging/type/log_severity.rb +0 -27
  147. data/lib/google/logging/v2/log_entry.rb +0 -44
  148. data/lib/google/logging/v2/logging.rb +0 -56
  149. data/lib/google/logging/v2/logging_config.rb +0 -59
  150. data/lib/google/logging/v2/logging_config_services.rb +0 -32
  151. data/lib/google/logging/v2/logging_metrics.rb +0 -51
  152. data/lib/google/logging/v2/logging_metrics_services.rb +0 -32
  153. data/lib/google/logging/v2/logging_services.rb +0 -31
  154. data/lib/google/longrunning/operations.rb +0 -50
  155. data/lib/google/longrunning/operations_services.rb +0 -29
  156. data/lib/google/protobuf/descriptor.rb +0 -0
  157. data/lib/google/pubsub/v1/pubsub.rb +0 -129
  158. data/lib/google/pubsub/v1/pubsub_services.rb +0 -56
  159. data/lib/google/pubsub/v1beta2/pubsub.rb +0 -126
  160. data/lib/google/pubsub/v1beta2/pubsub_services.rb +0 -56
  161. data/lib/google/rpc/code.rb +0 -32
  162. data/lib/google/rpc/error_details.rb +0 -61
  163. data/lib/google/rpc/status.rb +0 -19
  164. data/lib/google/type/color.rb +0 -20
  165. data/lib/google/type/date.rb +0 -18
  166. data/lib/google/type/dayofweek.rb +0 -23
  167. data/lib/google/type/latlng.rb +0 -17
  168. data/lib/google/type/money.rb +0 -18
  169. data/lib/google/type/timeofday.rb +0 -19
@@ -1,219 +0,0 @@
1
- # Copyright 2014 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 Gcloud
19
- module Datastore
20
- class Dataset
21
- ##
22
- # LookupResults is a special case Array with additional values.
23
- # A LookupResults object is returned from Dataset#find_all and
24
- # contains the entities as well as the Keys that were deferred from
25
- # the results and the Entities that were missing in the dataset.
26
- #
27
- # Please be cautious when treating the QueryResults as an Array.
28
- # Many common Array methods will return a new Array instance.
29
- #
30
- # @example
31
- # tasks = datastore.find_all task_key1, task_key2, task_key3
32
- # tasks.size #=> 3
33
- # tasks.deferred #=> []
34
- # tasks.missing #=> []
35
- #
36
- # @example Caution, many Array methods will return a new Array instance:
37
- # tasks = datastore.find_all task_key1, task_key2, task_key3
38
- # tasks.size #=> 3
39
- # tasks.deferred #=> []
40
- # tasks.missing #=> []
41
- # descriptions = tasks.map { |task| task["description"] }
42
- # descriptions.size #=> 3
43
- # descriptions.deferred #=> NoMethodError
44
- # descriptions.missing #=> NoMethodError
45
- #
46
- class LookupResults < DelegateClass(::Array)
47
- ##
48
- # Keys that were not looked up due to resource constraints.
49
- attr_accessor :deferred
50
-
51
- ##
52
- # Entities not found, with only the key populated.
53
- attr_accessor :missing
54
-
55
- ##
56
- # @private Create a new LookupResults with an array of values.
57
- def initialize arr = []
58
- super arr
59
- end
60
-
61
- ##
62
- # Whether there are more results available.
63
- #
64
- # @return [Boolean]
65
- #
66
- # @example
67
- # gcloud = Gcloud.new
68
- # datastore = gcloud.datastore
69
- #
70
- # task_key1 = datastore.key "Task", "sampleTask1"
71
- # task_key2 = datastore.key "Task", "sampleTask2"
72
- # tasks = datastore.find_all task_key1, task_key2
73
- # if tasks.next?
74
- # next_tasks = tasks.next
75
- # end
76
- #
77
- def next?
78
- Array(@deferred).any?
79
- end
80
-
81
- ##
82
- # Retrieve the next page of results.
83
- #
84
- # @return [LookupResults]
85
- #
86
- # @example
87
- # gcloud = Gcloud.new
88
- # datastore = gcloud.datastore
89
- #
90
- # task_key1 = datastore.key "Task", "sampleTask1"
91
- # task_key2 = datastore.key "Task", "sampleTask2"
92
- # tasks = datastore.find_all task_key1, task_key2
93
- # if tasks.next?
94
- # next_tasks = tasks.next
95
- # end
96
- #
97
- def next
98
- return nil unless next?
99
- ensure_service!
100
- lookup_res = @service.lookup(*Array(@deferred).flatten.map(&:to_grpc),
101
- consistency: @consistency,
102
- transaction: @transaction)
103
- self.class.from_grpc lookup_res, @service, @consistency
104
- end
105
-
106
- ##
107
- # Retrieves all lookup results by repeatedly loading {#next} until
108
- # {#next?} returns `false`. Calls the given block once for each result,
109
- # which is passed as the parameter.
110
- #
111
- # An Enumerator is returned if no block is given.
112
- #
113
- # This method may make several API calls until all lookup results are
114
- # retrieved. Be sure to use as narrow a search criteria as possible.
115
- # Please use with caution.
116
- #
117
- # @param [Integer] request_limit The upper limit of API requests to make
118
- # to load all lookup results. Default is no limit.
119
- # @yield [result] The block for accessing each lookup result.
120
- # @yieldparam [Entity] result The lookup result object.
121
- #
122
- # @return [Enumerator]
123
- #
124
- # @example Iterating each result by passing a block:
125
- # gcloud = Gcloud.new
126
- # datastore = gcloud.datastore
127
- #
128
- # task_key1 = datastore.key "Task", "sampleTask1"
129
- # task_key2 = datastore.key "Task", "sampleTask2"
130
- # tasks = datastore.find_all task_key1, task_key2
131
- # tasks.all do |task|
132
- # puts "Task #{task.key.id} (#cursor)"
133
- # end
134
- #
135
- # @example Using the enumerator by not passing a block:
136
- # gcloud = Gcloud.new
137
- # datastore = gcloud.datastore
138
- #
139
- # task_key1 = datastore.key "Task", "sampleTask1"
140
- # task_key2 = datastore.key "Task", "sampleTask2"
141
- # tasks = datastore.find_all task_key1, task_key2
142
- # all_keys = tasks.all.map(&:key).each do |task|
143
- # task.key
144
- # end
145
- #
146
- # @example Limit the number of API calls made:
147
- # gcloud = Gcloud.new
148
- # datastore = gcloud.datastore
149
- #
150
- # task_key1 = datastore.key "Task", "sampleTask1"
151
- # task_key2 = datastore.key "Task", "sampleTask2"
152
- # tasks = datastore.find_all task_key1, task_key2
153
- # tasks.all(request_limit: 10) do |task|
154
- # puts "Task #{task.key.id} (#cursor)"
155
- # end
156
- #
157
- def all request_limit: nil
158
- request_limit = request_limit.to_i if request_limit
159
- unless block_given?
160
- return enum_for(:all, request_limit: request_limit)
161
- end
162
- results = self
163
- loop do
164
- results.each { |r| yield r }
165
- if request_limit
166
- request_limit -= 1
167
- break if request_limit < 0
168
- end
169
- break unless results.next?
170
- results = results.next
171
- end
172
- end
173
-
174
- ##
175
- # @private New Dataset::LookupResults from a
176
- # Google::Dataset::V1beta3::LookupResponse object.
177
- def self.from_grpc lookup_res, service, consistency = nil, tx = nil
178
- entities = to_gcloud_entities lookup_res.found
179
- deferred = to_gcloud_keys lookup_res.deferred
180
- missing = to_gcloud_entities lookup_res.missing
181
- new(entities).tap do |lr|
182
- lr.instance_variable_set :@service, service
183
- lr.instance_variable_set :@consistency, consistency
184
- lr.instance_variable_set :@transaction, tx
185
- lr.instance_variable_set :@deferred, deferred
186
- lr.instance_variable_set :@missing, missing
187
- end
188
- end
189
-
190
- protected
191
-
192
- ##
193
- # @private Raise an error unless an active connection to the service is
194
- # available.
195
- def ensure_service!
196
- msg = "Must have active connection to datastore service to get next"
197
- fail msg if @service.nil?
198
- end
199
-
200
- ##
201
- # Convenience method to convert GRPC entities to gcloud-ruby entities.
202
- def self.to_gcloud_entities grpc_entity_results
203
- # Entities are nested in an object.
204
- Array(grpc_entity_results).map do |result|
205
- # TODO: Make this return an EntityResult with cursor...
206
- Entity.from_grpc result.entity
207
- end
208
- end
209
-
210
- ##
211
- # Convenience method to convert GRPC keys to gcloud-ruby keys.
212
- def self.to_gcloud_keys grpc_keys
213
- # Keys are not nested in an object like entities are.
214
- Array(grpc_keys).map { |key| Key.from_grpc key }
215
- end
216
- end
217
- end
218
- end
219
- end
@@ -1,386 +0,0 @@
1
- # Copyright 2014 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 Gcloud
19
- module Datastore
20
- class Dataset
21
- ##
22
- # QueryResults is a special case Array with additional values.
23
- # A QueryResults object is returned from Dataset#run and contains
24
- # the Entities from the query as well as the query's cursor and
25
- # more_results value.
26
- #
27
- # Please be cautious when treating the QueryResults as an Array.
28
- # Many common Array methods will return a new Array instance.
29
- #
30
- # @example
31
- # require "gcloud"
32
- #
33
- # gcloud = Gcloud.new
34
- # datastore = gcloud.datastore
35
- #
36
- # query = datastore.query("Task")
37
- # tasks = datastore.run query
38
- #
39
- # tasks.size #=> 3
40
- # tasks.cursor #=> Gcloud::Datastore::Cursor(c3VwZXJhd2Vzb21lIQ)
41
- #
42
- # @example Caution, many Array methods will return a new Array instance:
43
- # require "gcloud"
44
- #
45
- # gcloud = Gcloud.new
46
- # datastore = gcloud.datastore
47
- #
48
- # query = datastore.query("Task")
49
- # tasks = datastore.run query
50
- #
51
- # tasks.size #=> 3
52
- # tasks.end_cursor #=> Gcloud::Datastore::Cursor(c3VwZXJhd2Vzb21lIQ)
53
- # descriptions = tasks.map { |task| task["description"] }
54
- # descriptions.size #=> 3
55
- # descriptions.cursor #=> NoMethodError
56
- #
57
- class QueryResults < DelegateClass(::Array)
58
- ##
59
- # The end_cursor of the QueryResults.
60
- #
61
- # @return [Gcloud::Datastore::Cursor]
62
- attr_reader :end_cursor
63
- alias_method :cursor, :end_cursor
64
-
65
- ##
66
- # The state of the query after the current batch.
67
- #
68
- # Expected values are:
69
- #
70
- # * `:NOT_FINISHED`
71
- # * `:MORE_RESULTS_AFTER_LIMIT`
72
- # * `:MORE_RESULTS_AFTER_CURSOR`
73
- # * `:NO_MORE_RESULTS`
74
- attr_reader :more_results
75
-
76
- ##
77
- # @private
78
- attr_accessor :service, :namespace, :cursors, :query
79
-
80
- ##
81
- # @private
82
- attr_writer :end_cursor, :more_results
83
-
84
- ##
85
- # Convenience method for determining if the `more_results` value
86
- # is `:NOT_FINISHED`
87
- def not_finished?
88
- more_results == :NOT_FINISHED
89
- end
90
-
91
- ##
92
- # Convenience method for determining if the `more_results` value
93
- # is `:MORE_RESULTS_AFTER_LIMIT`
94
- def more_after_limit?
95
- more_results == :MORE_RESULTS_AFTER_LIMIT
96
- end
97
-
98
- ##
99
- # Convenience method for determining if the `more_results` value
100
- # is `:MORE_RESULTS_AFTER_CURSOR`
101
- def more_after_cursor?
102
- more_results == :MORE_RESULTS_AFTER_CURSOR
103
- end
104
-
105
- ##
106
- # Convenience method for determining if the `more_results` value
107
- # is `:NO_MORE_RESULTS`
108
- def no_more?
109
- more_results == :NO_MORE_RESULTS
110
- end
111
-
112
- ##
113
- # @private Create a new QueryResults with an array of values.
114
- def initialize arr = []
115
- super arr
116
- end
117
-
118
- ##
119
- # Whether there are more results available.
120
- #
121
- # @return [Boolean]
122
- #
123
- # @example
124
- # require "gcloud"
125
- #
126
- # gcloud = Gcloud.new
127
- # datastore = gcloud.datastore
128
- # query = datastore.query "Task"
129
- # tasks = datastore.run query
130
- #
131
- # if tasks.next?
132
- # next_tasks = tasks.next
133
- # end
134
- #
135
- def next?
136
- not_finished?
137
- end
138
-
139
- ##
140
- # Retrieve the next page of results.
141
- #
142
- # @return [QueryResults]
143
- #
144
- # @example
145
- # require "gcloud"
146
- #
147
- # gcloud = Gcloud.new
148
- # datastore = gcloud.datastore
149
- # query = datastore.query "Task"
150
- # tasks = datastore.run query
151
- #
152
- # if tasks.next?
153
- # next_tasks = tasks.next
154
- # end
155
- #
156
- def next
157
- return nil unless next?
158
- return nil if end_cursor.nil?
159
- ensure_service!
160
- query.start_cursor = cursor.to_grpc # should always be a Cursor...
161
- query_res = service.run_query query, namespace
162
- self.class.from_grpc query_res, service, namespace, query
163
- end
164
-
165
- ##
166
- # Retrieve the {Cursor} for the provided result.
167
- #
168
- # @param [Entity] result The entity object to get a cursor for.
169
- #
170
- # @return [Cursor]
171
- #
172
- # @example
173
- # require "gcloud"
174
- #
175
- # gcloud = Gcloud.new
176
- # datastore = gcloud.datastore
177
- # query = datastore.query "Task"
178
- # tasks = datastore.run query
179
- #
180
- # first_task = tasks.first
181
- # first_cursor = tasks.cursor_for first_task
182
- #
183
- def cursor_for result
184
- cursor_index = index result
185
- return nil if cursor_index.nil?
186
- cursors[cursor_index]
187
- end
188
-
189
- ##
190
- # Calls the given block once for each result and cursor combination,
191
- # which are passed as parameters.
192
- #
193
- # An Enumerator is returned if no block is given.
194
- #
195
- # @yield [result, cursor] The block for accessing each query result and
196
- # cursor.
197
- # @yieldparam [Entity] result The query result object.
198
- # @yieldparam [Cursor] cursor The cursor object.
199
- #
200
- # @return [Enumerator]
201
- #
202
- # @example
203
- # require "gcloud"
204
- #
205
- # gcloud = Gcloud.new
206
- # datastore = gcloud.datastore
207
- # query = datastore.query "Task"
208
- # tasks = datastore.run query
209
- # tasks.each_with_cursor do |task, cursor|
210
- # puts "Task #{task.key.id} (#cursor)"
211
- # end
212
- #
213
- def each_with_cursor
214
- return enum_for(:each_with_cursor) unless block_given?
215
- zip(cursors).each { |r, c| yield [r, c] }
216
- end
217
-
218
- ##
219
- # Retrieves all query results by repeatedly loading {#next} until
220
- # {#next?} returns `false`. Calls the given block once for each query
221
- # result, which is passed as the parameter.
222
- #
223
- # An Enumerator is returned if no block is given.
224
- #
225
- # This method may make several API calls until all query results are
226
- # retrieved. Be sure to use as narrow a search criteria as possible.
227
- # Please use with caution.
228
- #
229
- # @param [Integer] request_limit The upper limit of API requests to make
230
- # to load all query results. Default is no limit.
231
- # @yield [result] The block for accessing each query result.
232
- # @yieldparam [Entity] result The query result object.
233
- #
234
- # @return [Enumerator]
235
- #
236
- # @example Iterating each query result by passing a block:
237
- # require "gcloud"
238
- #
239
- # gcloud = Gcloud.new
240
- # datastore = gcloud.datastore
241
- # query = datastore.query "Task"
242
- # tasks = datastore.run query
243
- # tasks.all do |task|
244
- # puts "Task #{task.key.id} (#cursor)"
245
- # end
246
- #
247
- # @example Using the enumerator by not passing a block:
248
- # require "gcloud"
249
- #
250
- # gcloud = Gcloud.new
251
- # datastore = gcloud.datastore
252
- # query = datastore.query "Task"
253
- # tasks = datastore.run query
254
- # tasks.all.map(&:key).each do |key|
255
- # puts "Key #{key.id}"
256
- # end
257
- #
258
- # @example Limit the number of API calls made:
259
- # require "gcloud"
260
- #
261
- # gcloud = Gcloud.new
262
- # datastore = gcloud.datastore
263
- # query = datastore.query "Task"
264
- # tasks = datastore.run query
265
- # tasks.all(request_limit: 10) do |task|
266
- # puts "Task #{task.key.id} (#cursor)"
267
- # end
268
- #
269
- def all request_limit: nil
270
- request_limit = request_limit.to_i if request_limit
271
- unless block_given?
272
- return enum_for(:all, request_limit: request_limit)
273
- end
274
- results = self
275
- loop do
276
- results.each { |r| yield r }
277
- if request_limit
278
- request_limit -= 1
279
- break if request_limit < 0
280
- end
281
- break unless results.next?
282
- results = results.next
283
- end
284
- end
285
-
286
- ##
287
- # Retrieves all query results and cursors by repeatedly loading {#next}
288
- # until {#next?} returns `false`. Calls the given block once for each
289
- # result and cursor combination, which are passed as parameters.
290
- #
291
- # An Enumerator is returned if no block is given.
292
- #
293
- # This method may make several API calls until all query results are
294
- # retrieved. Be sure to use as narrow a search criteria as possible.
295
- # Please use with caution.
296
- #
297
- # @param [Integer] request_limit The upper limit of API requests to make
298
- # to load all tables. Default is no limit.
299
- # @yield [result, cursor] The block for accessing each query result and
300
- # cursor.
301
- # @yieldparam [Entity] result The query result object.
302
- # @yieldparam [Cursor] cursor The cursor object.
303
- #
304
- # @return [Enumerator]
305
- #
306
- # @example Iterating all results and cursors by passing a block:
307
- # require "gcloud"
308
- #
309
- # gcloud = Gcloud.new
310
- # datastore = gcloud.datastore
311
- # query = datastore.query "Task"
312
- # tasks = datastore.run query
313
- # tasks.all_with_cursor do |task, cursor|
314
- # puts "Task #{task.key.id} (#cursor)"
315
- # end
316
- #
317
- # @example Using the enumerator by not passing a block:
318
- # require "gcloud"
319
- #
320
- # gcloud = Gcloud.new
321
- # datastore = gcloud.datastore
322
- # query = datastore.query "Task"
323
- # tasks = datastore.run query
324
- # tasks.all_with_cursor.count #=> number of result/cursor pairs
325
- #
326
- # @example Limit the number of API calls made:
327
- # require "gcloud"
328
- #
329
- # gcloud = Gcloud.new
330
- # datastore = gcloud.datastore
331
- # query = datastore.query "Task"
332
- # tasks = datastore.run query
333
- # tasks.all_with_cursor(request_limit: 10) do |task, cursor|
334
- # puts "Task #{task.key.id} (#cursor)"
335
- # end
336
- #
337
- def all_with_cursor request_limit: nil
338
- request_limit = request_limit.to_i if request_limit
339
- unless block_given?
340
- return enum_for(:all_with_cursor, request_limit: request_limit)
341
- end
342
- results = self
343
-
344
- loop do
345
- results.zip(results.cursors).each { |r, c| yield r, c }
346
- if request_limit
347
- request_limit -= 1
348
- break if request_limit < 0
349
- end
350
- break unless results.next?
351
- results = results.next
352
- end
353
- end
354
-
355
- ##
356
- # @private New Dataset::QueryResults from a
357
- # Google::Dataset::V1beta3::RunQueryResponse object.
358
- def self.from_grpc query_res, service, namespace, query
359
- r, c = Array(query_res.batch.entity_results).map do |result|
360
- [Entity.from_grpc(result.entity), Cursor.from_grpc(result.cursor)]
361
- end.transpose
362
- r ||= []
363
- c ||= []
364
- new(r).tap do |qr|
365
- qr.cursors = c
366
- qr.end_cursor = Cursor.from_grpc query_res.batch.end_cursor
367
- qr.more_results = query_res.batch.more_results
368
- qr.service = service
369
- qr.namespace = namespace
370
- qr.query = query_res.query || query
371
- end
372
- end
373
-
374
- protected
375
-
376
- ##
377
- # @private Raise an error unless an active connection to the service is
378
- # available.
379
- def ensure_service!
380
- msg = "Must have active connection to datastore service to get next"
381
- fail msg if @service.nil? || @query.nil?
382
- end
383
- end
384
- end
385
- end
386
- end