google-cloud-pubsub 0.33.1 → 2.15.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/AUTHENTICATION.md +41 -40
  3. data/CHANGELOG.md +610 -0
  4. data/CONTRIBUTING.md +328 -116
  5. data/EMULATOR.md +2 -2
  6. data/LOGGING.md +95 -3
  7. data/OVERVIEW.md +183 -90
  8. data/TROUBLESHOOTING.md +2 -8
  9. data/lib/google/cloud/pubsub/acknowledge_result.rb +79 -0
  10. data/lib/google/cloud/pubsub/async_publisher/batch.rb +306 -0
  11. data/lib/google/cloud/pubsub/async_publisher.rb +270 -161
  12. data/lib/google/cloud/pubsub/batch_publisher.rb +65 -33
  13. data/lib/google/cloud/pubsub/convert.rb +36 -8
  14. data/lib/google/cloud/pubsub/credentials.rb +7 -5
  15. data/lib/google/cloud/pubsub/errors.rb +93 -0
  16. data/lib/google/cloud/pubsub/flow_controller.rb +139 -0
  17. data/lib/google/cloud/pubsub/message.rb +52 -7
  18. data/lib/google/cloud/pubsub/policy.rb +15 -12
  19. data/lib/google/cloud/pubsub/project.rb +341 -75
  20. data/lib/google/cloud/pubsub/publish_result.rb +9 -2
  21. data/lib/google/cloud/pubsub/received_message.rb +182 -20
  22. data/lib/google/cloud/pubsub/retry_policy.rb +88 -0
  23. data/lib/google/cloud/pubsub/schema/list.rb +180 -0
  24. data/lib/google/cloud/pubsub/schema.rb +310 -0
  25. data/lib/google/cloud/pubsub/service.rb +285 -258
  26. data/lib/google/cloud/pubsub/snapshot/list.rb +14 -14
  27. data/lib/google/cloud/pubsub/snapshot.rb +17 -12
  28. data/lib/google/cloud/pubsub/subscriber/enumerator_queue.rb +4 -4
  29. data/lib/google/cloud/pubsub/subscriber/inventory.rb +74 -33
  30. data/lib/google/cloud/pubsub/subscriber/sequencer.rb +115 -0
  31. data/lib/google/cloud/pubsub/subscriber/stream.rb +138 -91
  32. data/lib/google/cloud/pubsub/subscriber/timed_unary_buffer.rb +397 -0
  33. data/lib/google/cloud/pubsub/subscriber.rb +213 -51
  34. data/lib/google/cloud/pubsub/subscription/list.rb +16 -16
  35. data/lib/google/cloud/pubsub/subscription/push_config.rb +268 -0
  36. data/lib/google/cloud/pubsub/subscription.rb +827 -137
  37. data/lib/google/cloud/pubsub/topic/list.rb +14 -14
  38. data/lib/google/cloud/pubsub/topic.rb +565 -93
  39. data/lib/google/cloud/pubsub/version.rb +4 -2
  40. data/lib/google/cloud/pubsub.rb +50 -41
  41. data/lib/google-cloud-pubsub.rb +26 -29
  42. metadata +59 -53
  43. data/lib/google/cloud/pubsub/subscriber/async_stream_pusher.rb +0 -222
  44. data/lib/google/cloud/pubsub/subscriber/async_unary_pusher.rb +0 -270
  45. data/lib/google/cloud/pubsub/v1/credentials.rb +0 -39
  46. data/lib/google/cloud/pubsub/v1/doc/google/iam/v1/iam_policy.rb +0 -63
  47. data/lib/google/cloud/pubsub/v1/doc/google/iam/v1/policy.rb +0 -128
  48. data/lib/google/cloud/pubsub/v1/doc/google/protobuf/duration.rb +0 -91
  49. data/lib/google/cloud/pubsub/v1/doc/google/protobuf/empty.rb +0 -29
  50. data/lib/google/cloud/pubsub/v1/doc/google/protobuf/field_mask.rb +0 -230
  51. data/lib/google/cloud/pubsub/v1/doc/google/protobuf/timestamp.rb +0 -109
  52. data/lib/google/cloud/pubsub/v1/doc/google/pubsub/v1/pubsub.rb +0 -628
  53. data/lib/google/cloud/pubsub/v1/publisher_client.rb +0 -734
  54. data/lib/google/cloud/pubsub/v1/publisher_client_config.json +0 -105
  55. data/lib/google/cloud/pubsub/v1/subscriber_client.rb +0 -1267
  56. data/lib/google/cloud/pubsub/v1/subscriber_client_config.json +0 -144
  57. data/lib/google/cloud/pubsub/v1.rb +0 -17
  58. data/lib/google/pubsub/v1/pubsub_pb.rb +0 -222
  59. data/lib/google/pubsub/v1/pubsub_services_pb.rb +0 -192
@@ -18,7 +18,7 @@ require "google/cloud/pubsub/message"
18
18
 
19
19
  module Google
20
20
  module Cloud
21
- module Pubsub
21
+ module PubSub
22
22
  ##
23
23
  # # ReceivedMessage
24
24
  #
@@ -27,7 +27,7 @@ module Google
27
27
  # @example
28
28
  # require "google/cloud/pubsub"
29
29
  #
30
- # pubsub = Google::Cloud::Pubsub.new
30
+ # pubsub = Google::Cloud::PubSub.new
31
31
  #
32
32
  # sub = pubsub.subscription "my-topic-sub"
33
33
  # subscriber = sub.listen do |received_message|
@@ -39,7 +39,7 @@ module Google
39
39
  # subscriber.start
40
40
  #
41
41
  # # Shut down the subscriber when ready to stop receiving messages.
42
- # subscriber.stop.wait!
42
+ # subscriber.stop!
43
43
  #
44
44
  class ReceivedMessage
45
45
  ##
@@ -47,14 +47,14 @@ module Google
47
47
  attr_accessor :subscription
48
48
 
49
49
  ##
50
- # @private The gRPC Google::Pubsub::V1::ReceivedMessage object.
50
+ # @private The gRPC Google::Cloud::PubSub::V1::ReceivedMessage object.
51
51
  attr_accessor :grpc
52
52
 
53
53
  ##
54
54
  # @private Create an empty {Subscription} object.
55
55
  def initialize
56
56
  @subscription = nil
57
- @grpc = Google::Pubsub::V1::ReceivedMessage.new
57
+ @grpc = Google::Cloud::PubSub::V1::ReceivedMessage.new
58
58
  end
59
59
 
60
60
  ##
@@ -63,6 +63,44 @@ module Google
63
63
  @grpc.ack_id
64
64
  end
65
65
 
66
+ ##
67
+ # Returns the delivery attempt counter for the message. If a dead letter policy is not set on the subscription,
68
+ # this will be `nil`. See {Topic#subscribe}, {Subscription#dead_letter_topic=} and
69
+ # {Subscription#dead_letter_max_delivery_attempts=}.
70
+ #
71
+ # The delivery attempt counter is `1 + (the sum of number of NACKs and number of ack_deadline exceeds)` for the
72
+ # message.
73
+ #
74
+ # A NACK is any call to `ModifyAckDeadline` with a `0` deadline. An `ack_deadline` exceeds event is whenever a
75
+ # message is not acknowledged within `ack_deadline`. Note that `ack_deadline` is initially
76
+ # `Subscription.ackDeadlineSeconds`, but may get extended automatically by the client library.
77
+ #
78
+ # The first delivery of a given message will have this value as `1`. The value is calculated at best effort and
79
+ # is approximate.
80
+ #
81
+ # @return [Integer, nil] A delivery attempt value of `1` or greater, or `nil` if a dead letter policy is not set
82
+ # on the subscription.
83
+ #
84
+ # @example
85
+ # require "google/cloud/pubsub"
86
+ #
87
+ # pubsub = Google::Cloud::PubSub.new
88
+ #
89
+ # topic = pubsub.topic "my-topic"
90
+ # dead_letter_topic = pubsub.topic "my-dead-letter-topic", skip_lookup: true
91
+ # sub = topic.subscribe "my-topic-sub",
92
+ # dead_letter_topic: dead_letter_topic,
93
+ # dead_letter_max_delivery_attempts: 10
94
+ #
95
+ # subscriber = sub.listen do |received_message|
96
+ # puts received_message.message.delivery_attempt
97
+ # end
98
+ #
99
+ def delivery_attempt
100
+ return nil if @grpc.delivery_attempt && @grpc.delivery_attempt < 1
101
+ @grpc.delivery_attempt
102
+ end
103
+
66
104
  ##
67
105
  # The received message.
68
106
  def message
@@ -91,6 +129,28 @@ module Google
91
129
  end
92
130
  alias msg_id message_id
93
131
 
132
+ ##
133
+ # Identifies related messages for which publish order should be
134
+ # respected.
135
+ #
136
+ # Google Cloud Pub/Sub ordering keys provide the ability to ensure
137
+ # related messages are sent to subscribers in the order in which they
138
+ # were published. Messages can be tagged with an ordering key, a string
139
+ # that identifies related messages for which publish order should be
140
+ # respected. The service guarantees that, for a given ordering key and
141
+ # publisher, messages are sent to subscribers in the order in which they
142
+ # were published. Ordering does not require sacrificing high throughput
143
+ # or scalability, as the service automatically distributes messages for
144
+ # different ordering keys across subscribers.
145
+ #
146
+ # See {Topic#publish_async} and {Subscription#listen}.
147
+ #
148
+ # @return [String]
149
+ #
150
+ def ordering_key
151
+ message.ordering_key
152
+ end
153
+
94
154
  ##
95
155
  # The time at which the message was published.
96
156
  def published_at
@@ -101,10 +161,33 @@ module Google
101
161
  ##
102
162
  # Acknowledges receipt of the message.
103
163
  #
164
+ # @yield [callback] The block to be called when reject operation is done.
165
+ # @yieldparam [Google::Cloud::PubSub::AcknowledgeResult] Result object that contains the status and error.
166
+ #
167
+ # @example
168
+ # require "google/cloud/pubsub"
169
+ #
170
+ # pubsub = Google::Cloud::PubSub.new
171
+ #
172
+ # sub = pubsub.subscription "my-topic-sub"
173
+ # subscriber = sub.listen do |received_message|
174
+ # puts received_message.message.data
175
+ #
176
+ # received_message.acknowledge! do |result|
177
+ # puts result.status
178
+ # end
179
+ # end
180
+ #
181
+ # # Start background threads that will call block passed to listen.
182
+ # subscriber.start
183
+ #
184
+ # # Shut down the subscriber when ready to stop receiving messages.
185
+ # subscriber.stop!
186
+ #
104
187
  # @example
105
188
  # require "google/cloud/pubsub"
106
189
  #
107
- # pubsub = Google::Cloud::Pubsub.new
190
+ # pubsub = Google::Cloud::PubSub.new
108
191
  #
109
192
  # sub = pubsub.subscription "my-topic-sub"
110
193
  # subscriber = sub.listen do |received_message|
@@ -117,11 +200,16 @@ module Google
117
200
  # subscriber.start
118
201
  #
119
202
  # # Shut down the subscriber when ready to stop receiving messages.
120
- # subscriber.stop.wait!
203
+ # subscriber.stop!
121
204
  #
122
- def acknowledge!
205
+ def acknowledge! &block
123
206
  ensure_subscription!
124
- subscription.acknowledge ack_id
207
+ if subscription.respond_to?(:exactly_once_delivery_enabled) && subscription.exactly_once_delivery_enabled
208
+ subscription.acknowledge ack_id, &block
209
+ else
210
+ subscription.acknowledge ack_id
211
+ yield AcknowledgeResult.new(AcknowledgeResult::SUCCESS) if block_given?
212
+ end
125
213
  end
126
214
  alias ack! acknowledge!
127
215
 
@@ -137,30 +225,58 @@ module Google
137
225
  # seconds after the call is made. Specifying `0` may immediately make
138
226
  # the message available for another pull request.
139
227
  #
228
+ # @yield [callback] The block to be called when reject operation is done.
229
+ # @yieldparam [Google::Cloud::PubSub::AcknowledgeResult] Result object that contains the status and error.
230
+ #
140
231
  # @example
141
232
  # require "google/cloud/pubsub"
142
233
  #
143
- # pubsub = Google::Cloud::Pubsub.new
234
+ # pubsub = Google::Cloud::PubSub.new
144
235
  #
145
236
  # sub = pubsub.subscription "my-topic-sub"
146
237
  # subscriber = sub.listen do |received_message|
147
238
  # puts received_message.message.data
148
239
  #
149
240
  # # Delay for 2 minutes
150
- # received_message.delay! 120
241
+ # received_message.modify_ack_deadline! 120 do |result|
242
+ # puts result.status
243
+ # end
151
244
  # end
152
245
  #
153
246
  # # Start background threads that will call block passed to listen.
154
247
  # subscriber.start
155
248
  #
156
249
  # # Shut down the subscriber when ready to stop receiving messages.
157
- # subscriber.stop.wait!
250
+ # subscriber.stop!
158
251
  #
159
- def delay! new_deadline
252
+ # @example
253
+ # require "google/cloud/pubsub"
254
+ #
255
+ # pubsub = Google::Cloud::PubSub.new
256
+ #
257
+ # sub = pubsub.subscription "my-topic-sub"
258
+ # subscriber = sub.listen do |received_message|
259
+ # puts received_message.message.data
260
+ #
261
+ # # Delay for 2 minutes
262
+ # received_message.modify_ack_deadline! 120
263
+ # end
264
+ #
265
+ # # Start background threads that will call block passed to listen.
266
+ # subscriber.start
267
+ #
268
+ # # Shut down the subscriber when ready to stop receiving messages.
269
+ # subscriber.stop!
270
+ #
271
+ def modify_ack_deadline! new_deadline, &block
160
272
  ensure_subscription!
161
- subscription.delay new_deadline, ack_id
273
+ if subscription.respond_to?(:exactly_once_delivery_enabled) && subscription.exactly_once_delivery_enabled
274
+ subscription.modify_ack_deadline new_deadline, ack_id, &block
275
+ else
276
+ subscription.modify_ack_deadline new_deadline, ack_id
277
+ yield AcknowledgeResult.new(AcknowledgeResult::SUCCESS) if block_given?
278
+ end
162
279
  end
163
- alias modify_ack_deadline! delay!
164
280
 
165
281
  ##
166
282
  # Resets the acknowledge deadline for the message without acknowledging
@@ -168,10 +284,34 @@ module Google
168
284
  #
169
285
  # This will make the message available for redelivery.
170
286
  #
287
+ # @yield [callback] The block to be called when reject operation is done.
288
+ # @yieldparam [Google::Cloud::PubSub::AcknowledgeResult] Result object that contains the status and error.
289
+ #
171
290
  # @example
172
291
  # require "google/cloud/pubsub"
173
292
  #
174
- # pubsub = Google::Cloud::Pubsub.new
293
+ # pubsub = Google::Cloud::PubSub.new
294
+ #
295
+ # sub = pubsub.subscription "my-topic-sub"
296
+ # subscriber = sub.listen do |received_message|
297
+ # puts received_message.message.data
298
+ #
299
+ # # Release message back to the API.
300
+ # received_message.reject! do |result|
301
+ # puts result.status
302
+ # end
303
+ # end
304
+ #
305
+ # # Start background threads that will call block passed to listen.
306
+ # subscriber.start
307
+ #
308
+ # # Shut down the subscriber when ready to stop receiving messages.
309
+ # subscriber.stop!
310
+ #
311
+ # @example
312
+ # require "google/cloud/pubsub"
313
+ #
314
+ # pubsub = Google::Cloud::PubSub.new
175
315
  #
176
316
  # sub = pubsub.subscription "my-topic-sub"
177
317
  # subscriber = sub.listen do |received_message|
@@ -185,17 +325,37 @@ module Google
185
325
  # subscriber.start
186
326
  #
187
327
  # # Shut down the subscriber when ready to stop receiving messages.
188
- # subscriber.stop.wait!
328
+ # subscriber.stop!
189
329
  #
190
- def reject!
191
- delay! 0
330
+ def reject! &block
331
+ modify_ack_deadline! 0, &block
192
332
  end
193
333
  alias nack! reject!
194
334
  alias ignore! reject!
195
335
 
336
+ # @private
337
+ def hash
338
+ @grpc.hash
339
+ end
340
+
341
+ # @private
342
+ def eql? other
343
+ return false unless other.is_a? self.class
344
+ @grpc.hash == other.hash
345
+ end
346
+ # @private
347
+ alias == eql?
348
+
349
+ # @private
350
+ def <=> other
351
+ return nil unless other.is_a? self.class
352
+ other_grpc = other.instance_variable_get :@grpc
353
+ @grpc <=> other_grpc
354
+ end
355
+
196
356
  ##
197
357
  # @private New ReceivedMessage from a
198
- # Google::Pubsub::V1::ReceivedMessage object.
358
+ # Google::Cloud::PubSub::V1::ReceivedMessage object.
199
359
  def self.from_grpc grpc, subscription
200
360
  new.tap do |rm|
201
361
  rm.grpc = grpc
@@ -212,5 +372,7 @@ module Google
212
372
  end
213
373
  end
214
374
  end
375
+
376
+ Pubsub = PubSub unless const_defined? :Pubsub
215
377
  end
216
378
  end
@@ -0,0 +1,88 @@
1
+ # Copyright 2016 Google LLC
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
+ # https://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 "google/cloud/errors"
17
+
18
+ module Google
19
+ module Cloud
20
+ module PubSub
21
+ ##
22
+ # # RetryPolicy
23
+ #
24
+ # An immutable Retry Policy value object that specifies how Cloud Pub/Sub retries message delivery.
25
+ #
26
+ # Retry delay will be exponential based on provided minimum and maximum backoffs. (See [Exponential
27
+ # backoff](https://en.wikipedia.org/wiki/Exponential_backoff).)
28
+ #
29
+ # Retry Policy will be triggered on NACKs or acknowledgement deadline exceeded events for a given message.
30
+ #
31
+ # Retry Policy is implemented on a best effort basis. At times, the delay between consecutive deliveries may not
32
+ # match the configuration. That is, delay can be more or less than configured backoff.
33
+ #
34
+ # @attr [Numeric] minimum_backoff The minimum delay between consecutive deliveries of a given message. Value
35
+ # should be between 0 and 600 seconds. The default value is 10 seconds.
36
+ # @attr [Numeric] maximum_backoff The maximum delay between consecutive deliveries of a given message. Value
37
+ # should be between 0 and 600 seconds. The default value is 600 seconds.
38
+ #
39
+ # @example
40
+ # require "google/cloud/pubsub"
41
+ #
42
+ # pubsub = Google::Cloud::PubSub.new
43
+ #
44
+ # sub = pubsub.subscription "my-topic-sub"
45
+ #
46
+ # sub.retry_policy = Google::Cloud::PubSub::RetryPolicy.new minimum_backoff: 5, maximum_backoff: 300
47
+ #
48
+ # sub.retry_policy.minimum_backoff #=> 5
49
+ # sub.retry_policy.maximum_backoff #=> 300
50
+ #
51
+ class RetryPolicy
52
+ attr_reader :minimum_backoff
53
+ attr_reader :maximum_backoff
54
+
55
+ ##
56
+ # Creates a new, immutable RetryPolicy value object.
57
+ #
58
+ # @attr [Numeric, nil] minimum_backoff The minimum delay between consecutive deliveries of a given message.
59
+ # Value should be between 0 and 600 seconds. If `nil` is provided, the default value is 10 seconds.
60
+ # @attr [Numeric, nil] maximum_backoff The maximum delay between consecutive deliveries of a given message.
61
+ # Value should be between 0 and 600 seconds. If `nil` is provided, the default value is 600 seconds.
62
+ #
63
+ def initialize minimum_backoff: nil, maximum_backoff: nil
64
+ @minimum_backoff = minimum_backoff
65
+ @maximum_backoff = maximum_backoff
66
+ end
67
+
68
+ ##
69
+ # @private Convert the RetryPolicy to a Google::Cloud::PubSub::V1::RetryPolicy object.
70
+ def to_grpc
71
+ Google::Cloud::PubSub::V1::RetryPolicy.new(
72
+ minimum_backoff: Convert.number_to_duration(minimum_backoff),
73
+ maximum_backoff: Convert.number_to_duration(maximum_backoff)
74
+ )
75
+ end
76
+
77
+ ##
78
+ # @private New RetryPolicy from a Google::Cloud::PubSub::V1::RetryPolicy object.
79
+ def self.from_grpc grpc
80
+ new(
81
+ minimum_backoff: Convert.duration_to_number(grpc.minimum_backoff),
82
+ maximum_backoff: Convert.duration_to_number(grpc.maximum_backoff)
83
+ )
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,180 @@
1
+ # Copyright 2021 Google LLC
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
+ # https://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 PubSub
21
+ class Schema
22
+ ##
23
+ # Schema::List is a special case Array with additional values.
24
+ class List < DelegateClass(::Array)
25
+ ##
26
+ # If not empty, indicates that there are more schemas
27
+ # that match the request and this value should be passed to
28
+ # the next {Google::Cloud::PubSub::Project#schemas} to continue.
29
+ attr_accessor :token
30
+
31
+ ##
32
+ # @private Create a new Schema::List with an array of values.
33
+ def initialize arr = []
34
+ @prefix = nil
35
+ @token = nil
36
+ @view = nil
37
+ @max = nil
38
+ super arr
39
+ end
40
+
41
+ ##
42
+ # Whether there a next page of schemas.
43
+ #
44
+ # @return [Boolean]
45
+ #
46
+ # @example
47
+ # require "google/cloud/pubsub"
48
+ #
49
+ # pubsub = Google::Cloud::PubSub.new
50
+ #
51
+ # schemas = pubsub.schemas
52
+ # if schemas.next?
53
+ # next_schemas = schemas.next
54
+ # end
55
+ #
56
+ def next?
57
+ !token.nil?
58
+ end
59
+
60
+ ##
61
+ # Retrieve the next page of schemas.
62
+ #
63
+ # @return [Schema::List]
64
+ #
65
+ # @example
66
+ # require "google/cloud/pubsub"
67
+ #
68
+ # pubsub = Google::Cloud::PubSub.new
69
+ #
70
+ # schemas = pubsub.schemas
71
+ # if schemas.next?
72
+ # next_schemas = schemas.next
73
+ # end
74
+ #
75
+ def next
76
+ return nil unless next?
77
+ ensure_service!
78
+ next_schemas
79
+ end
80
+
81
+ ##
82
+ # Retrieves remaining results by repeatedly invoking {#next} until
83
+ # {#next?} returns `false`. Calls the given block once for each
84
+ # result, which is passed as the argument to the block.
85
+ #
86
+ # An Enumerator is returned if no block is given.
87
+ #
88
+ # This method will make repeated API calls until all remaining results
89
+ # are retrieved. (Unlike `#each`, for example, which merely iterates
90
+ # over the results returned by a single API call.) Use with caution.
91
+ #
92
+ # @param [Integer] request_limit The upper limit of API requests to
93
+ # make to load all schemas. Default is no limit.
94
+ # @yield [schema] The block for accessing each schema.
95
+ # @yieldparam [Schema] schema The schema object.
96
+ #
97
+ # @return [Enumerator]
98
+ #
99
+ # @example Iterating each schema by passing a block:
100
+ # require "google/cloud/pubsub"
101
+ #
102
+ # pubsub = Google::Cloud::PubSub.new
103
+ #
104
+ # schemas = pubsub.schemas
105
+ # schemas.all do |schema|
106
+ # puts schema.name
107
+ # end
108
+ #
109
+ # @example Using the enumerator by not passing a block:
110
+ # require "google/cloud/pubsub"
111
+ #
112
+ # pubsub = Google::Cloud::PubSub.new
113
+ #
114
+ # schemas = pubsub.schemas
115
+ # all_names = schemas.all.map do |schema|
116
+ # schema.name
117
+ # end
118
+ #
119
+ # @example Limit the number of API calls made:
120
+ # require "google/cloud/pubsub"
121
+ #
122
+ # pubsub = Google::Cloud::PubSub.new
123
+ #
124
+ # schemas = pubsub.schemas
125
+ # schemas.all(request_limit: 10) do |schema|
126
+ # puts schema.name
127
+ # end
128
+ #
129
+ def all request_limit: nil, &block
130
+ request_limit = request_limit.to_i if request_limit
131
+ return enum_for :all, request_limit: request_limit unless block_given?
132
+ results = self
133
+ loop do
134
+ results.each(&block)
135
+ if request_limit
136
+ request_limit -= 1
137
+ break if request_limit.negative?
138
+ end
139
+ break unless results.next?
140
+ results = results.next
141
+ end
142
+ end
143
+
144
+ ##
145
+ # @private New Schemas::List from a
146
+ # Google::Cloud::PubSub::V1::ListSchemasRequest object.
147
+ def self.from_grpc grpc_list, service, view, max = nil
148
+ subs = new(Array(grpc_list.schemas).map do |grpc|
149
+ Schema.from_grpc grpc, service
150
+ end)
151
+ token = grpc_list.next_page_token
152
+ token = nil if token == "".freeze
153
+ subs.instance_variable_set :@token, token
154
+ subs.instance_variable_set :@service, service
155
+ subs.instance_variable_set :@view, view
156
+ subs.instance_variable_set :@max, max
157
+ subs
158
+ end
159
+
160
+ protected
161
+
162
+ ##
163
+ # @private Raise an error unless an active connection to the service
164
+ # is available.
165
+ def ensure_service!
166
+ raise "Must have active connection to service" unless @service
167
+ end
168
+
169
+ def next_schemas
170
+ options = { prefix: @prefix, token: @token, max: @max }
171
+ grpc = @service.list_schemas @view, options
172
+ self.class.from_grpc grpc, @service, @view, @max
173
+ end
174
+ end
175
+ end
176
+ end
177
+
178
+ Pubsub = PubSub unless const_defined? :Pubsub
179
+ end
180
+ end