google-cloud-pubsub 0.20.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 835dfba62f6801603cf98ecfccd798ba59a575a8
4
+ data.tar.gz: 3c5f2bf501c861ce2fe0e42f6625707df7c54e5b
5
+ SHA512:
6
+ metadata.gz: 7c8a41c01358952bfb9e567ff295ba6f3626d9e5602de29df9d5463346d594b0b54eb8b756c7dcbc45d4464adfb5410401b3d984da327cf9f170e78d5a9ef1ee
7
+ data.tar.gz: 59186803d87e2873ea085bdbff9000f6e8eec623d076c6074ca52eac494cef2ba7c961d43fe91a0c49d13cb49970c5fbf0a9f3ded3ddede2538b6f1a2e21fe74
@@ -0,0 +1,122 @@
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
+ # This file is here to be autorequired by bundler, so that the .bigquery and
17
+ # #bigquery methods can be available, but the library and all dependencies won't
18
+ # be loaded until required and used.
19
+
20
+
21
+ gem "google-cloud-core"
22
+ require "google/cloud"
23
+
24
+ module Google
25
+ module Cloud
26
+ ##
27
+ # Creates a new object for connecting to the Pub/Sub service.
28
+ # Each call creates a new connection.
29
+ #
30
+ # For more information on connecting to Google Cloud see the [Authentication
31
+ # Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/guides/authentication).
32
+ #
33
+ # @param [String, Array<String>] scope The OAuth 2.0 scopes controlling the
34
+ # set of resources and operations that the connection can access. See
35
+ # [Using OAuth 2.0 to Access Google
36
+ # APIs](https://developers.google.com/identity/protocols/OAuth2).
37
+ #
38
+ # The default scope is:
39
+ #
40
+ # * `https://www.googleapis.com/auth/pubsub`
41
+ # @param [Integer] retries Number of times to retry requests on server
42
+ # error. The default value is `3`. Optional.
43
+ # @param [Integer] timeout Default timeout to use in requests. Optional.
44
+ #
45
+ # @return [Google::Cloud::Pubsub::Project]
46
+ #
47
+ # @example
48
+ # require "google/cloud"
49
+ #
50
+ # gcloud = Google::Cloud.new
51
+ # pubsub = gcloud.pubsub
52
+ # topic = pubsub.topic "my-topic"
53
+ # topic.publish "task completed"
54
+ #
55
+ # @example The default scope can be overridden with the `scope` option:
56
+ # require "google/cloud"
57
+ #
58
+ # gcloud = Google::Cloud.new
59
+ # platform_scope = "https://www.googleapis.com/auth/cloud-platform"
60
+ # pubsub = gcloud.pubsub scope: platform_scope
61
+ #
62
+ def pubsub scope: nil, retries: nil, timeout: nil
63
+ Google::Cloud.pubsub @project, @keyfile, scope: scope,
64
+ retries: (retries || @retries),
65
+ timeout: (timeout || @timeout)
66
+ end
67
+
68
+ ##
69
+ # Creates a new object for connecting to the Pub/Sub service.
70
+ # Each call creates a new connection.
71
+ #
72
+ # For more information on connecting to Google Cloud see the [Authentication
73
+ # Guide](https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/guides/authentication).
74
+ #
75
+ # @param [String] project Project identifier for the Pub/Sub service you are
76
+ # connecting to.
77
+ # @param [String, Hash] keyfile Keyfile downloaded from Google Cloud. If
78
+ # file path the file must be readable.
79
+ # @param [String, Array<String>] scope The OAuth 2.0 scopes controlling the
80
+ # set of resources and operations that the connection can access. See
81
+ # [Using OAuth 2.0 to Access Google
82
+ # APIs](https://developers.google.com/identity/protocols/OAuth2).
83
+ #
84
+ # The default scope is:
85
+ #
86
+ # * `https://www.googleapis.com/auth/pubsub`
87
+ # @param [Integer] retries Number of times to retry requests on server
88
+ # error. The default value is `3`. Optional.
89
+ # @param [Integer] timeout Default timeout to use in requests. Optional.
90
+ #
91
+ # @return [Google::Cloud::Pubsub::Project]
92
+ #
93
+ # @example
94
+ # require "google/cloud/pubsub"
95
+ #
96
+ # pubsub = Google::Cloud.pubsub
97
+ #
98
+ # topic = pubsub.topic "my-topic"
99
+ # topic.publish "task completed"
100
+ #
101
+ def self.pubsub project = nil, keyfile = nil, scope: nil, retries: nil,
102
+ timeout: nil
103
+ require "google/cloud/pubsub"
104
+ project ||= Google::Cloud::Pubsub::Project.default_project
105
+ if ENV["PUBSUB_EMULATOR_HOST"]
106
+ ps = Google::Cloud::Pubsub::Project.new(
107
+ project, :this_channel_is_insecure)
108
+ ps.service.host = ENV["PUBSUB_EMULATOR_HOST"]
109
+ return ps
110
+ end
111
+ if keyfile.nil?
112
+ credentials = Google::Cloud::Pubsub::Credentials.default scope: scope
113
+ else
114
+ credentials = Google::Cloud::Pubsub::Credentials.new(
115
+ keyfile, scope: scope)
116
+ end
117
+ Google::Cloud::Pubsub::Project.new(
118
+ Google::Cloud::Pubsub::Service.new(
119
+ project, credentials, retries: retries, timeout: timeout))
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,445 @@
1
+ # Copyright 2015 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 "google-cloud-pubsub"
17
+ require "google/cloud/pubsub/project"
18
+
19
+ module Google
20
+ module Cloud
21
+ ##
22
+ # # Google Cloud Pub/Sub
23
+ #
24
+ # Google Cloud Pub/Sub is designed to provide reliable, many-to-many,
25
+ # asynchronous messaging between applications. Publisher applications can
26
+ # send messages to a "topic" and other applications can subscribe to that
27
+ # topic to receive the messages. By decoupling senders and receivers, Google
28
+ # Cloud Pub/Sub allows developers to communicate between independently
29
+ # written applications.
30
+ #
31
+ # The goal of google-cloud is to provide a API that is comfortable to
32
+ # Rubyists. Authentication is handled by {Google::Cloud#pubsub}. You can
33
+ # provide the project and credential information to connect to the Pub/Sub
34
+ # service, or if you are running on Google Compute Engine this configuration
35
+ # is taken care of for you.
36
+ #
37
+ # ```ruby
38
+ # require "google/cloud"
39
+ #
40
+ # gcloud = Google::Cloud.new
41
+ # pubsub = gcloud.pubsub
42
+ #
43
+ # topic = pubsub.topic "my-topic"
44
+ # topic.publish "task completed"
45
+ # ```
46
+ #
47
+ # To learn more about Pub/Sub, read the [Google Cloud Pub/Sub Overview
48
+ # ](https://cloud.google.com/pubsub/overview).
49
+ #
50
+ # ## Retrieving Topics
51
+ #
52
+ # A Topic is a named resource to which messages are sent by publishers.
53
+ # A Topic is found by its name. (See {Google::Cloud::Pubsub::Project#topic})
54
+ #
55
+ # ```ruby
56
+ # require "google/cloud"
57
+ #
58
+ # gcloud = Google::Cloud.new
59
+ # pubsub = gcloud.pubsub
60
+ # topic = pubsub.topic "my-topic"
61
+ # ```
62
+ #
63
+ # ## Creating a Topic
64
+ #
65
+ # A Topic is created from a Project. (See
66
+ # {Google::Cloud::Pubsub::Project#create_topic})
67
+ #
68
+ # ```ruby
69
+ # require "google/cloud"
70
+ #
71
+ # gcloud = Google::Cloud.new
72
+ # pubsub = gcloud.pubsub
73
+ # topic = pubsub.create_topic "my-topic"
74
+ # ```
75
+ #
76
+ # ## Retrieving Subscriptions
77
+ #
78
+ # A Subscription is a named resource representing the stream of messages
79
+ # from a single, specific Topic, to be delivered to the subscribing
80
+ # application. A Subscription is found by its name. (See
81
+ # {Google::Cloud::Pubsub::Topic#subscription})
82
+ #
83
+ # ```ruby
84
+ # require "google/cloud"
85
+ #
86
+ # gcloud = Google::Cloud.new
87
+ # pubsub = gcloud.pubsub
88
+ #
89
+ # topic = pubsub.topic "my-topic"
90
+ # subscription = topic.subscription "my-topic-subscription"
91
+ # puts subscription.name
92
+ # ```
93
+ #
94
+ # ## Creating a Subscription
95
+ #
96
+ # A Subscription is created from a Topic. (See
97
+ # {Google::Cloud::Pubsub::Topic#subscribe} and
98
+ # {Google::Cloud::Pubsub::Project#subscribe})
99
+ #
100
+ # ```ruby
101
+ # require "google/cloud"
102
+ #
103
+ # gcloud = Google::Cloud.new
104
+ # pubsub = gcloud.pubsub
105
+ #
106
+ # topic = pubsub.topic "my-topic"
107
+ # sub = topic.subscribe "my-topic-sub"
108
+ # puts sub.name # => "my-topic-sub"
109
+ # ```
110
+ #
111
+ # The subscription can be created that specifies the number of seconds to
112
+ # wait to be acknowledged as well as an endpoint URL to push the messages
113
+ # to:
114
+ #
115
+ # ```ruby
116
+ # require "google/cloud"
117
+ #
118
+ # gcloud = Google::Cloud.new
119
+ # pubsub = gcloud.pubsub
120
+ #
121
+ # topic = pubsub.topic "my-topic"
122
+ # sub = topic.subscribe "my-topic-sub",
123
+ # deadline: 120,
124
+ # endpoint: "https://example.com/push"
125
+ # ```
126
+ #
127
+ # ## Publishing Messages
128
+ #
129
+ # Messages are published to a topic. Any message published to a topic
130
+ # without a subscription will be lost. Ensure the topic has a subscription
131
+ # before publishing. (See {Google::Cloud::Pubsub::Topic#publish} and
132
+ # {Google::Cloud::Pubsub::Project#publish})
133
+ #
134
+ # ```ruby
135
+ # require "google/cloud"
136
+ #
137
+ # gcloud = Google::Cloud.new
138
+ # pubsub = gcloud.pubsub
139
+ #
140
+ # topic = pubsub.topic "my-topic"
141
+ # msg = topic.publish "new-message"
142
+ # ```
143
+ #
144
+ # Messages can also be published with attributes:
145
+ #
146
+ # ```ruby
147
+ # require "google/cloud"
148
+ #
149
+ # gcloud = Google::Cloud.new
150
+ # pubsub = gcloud.pubsub
151
+ #
152
+ # topic = pubsub.topic "my-topic"
153
+ # msg = topic.publish "new-message",
154
+ # foo: :bar,
155
+ # this: :that
156
+ # ```
157
+ #
158
+ # Multiple messages can be published at the same time by passing a block:
159
+ #
160
+ # ```ruby
161
+ # require "google/cloud"
162
+ #
163
+ # gcloud = Google::Cloud.new
164
+ # pubsub = gcloud.pubsub
165
+ #
166
+ # topic = pubsub.topic "my-topic"
167
+ # msgs = topic.publish do |batch|
168
+ # batch.publish "new-message-1", foo: :bar
169
+ # batch.publish "new-message-2", foo: :baz
170
+ # batch.publish "new-message-3", foo: :bif
171
+ # end
172
+ # ```
173
+ #
174
+ # ## Pulling Messages
175
+ #
176
+ # Messages are pulled from a Subscription. (See
177
+ # {Google::Cloud::Pubsub::Subscription#pull})
178
+ #
179
+ # ```ruby
180
+ # require "google/cloud"
181
+ #
182
+ # gcloud = Google::Cloud.new
183
+ # pubsub = gcloud.pubsub
184
+ #
185
+ # sub = pubsub.subscription "my-topic-sub"
186
+ # msgs = sub.pull
187
+ # ```
188
+ #
189
+ # A maximum number of messages returned can also be specified:
190
+ #
191
+ # ```ruby
192
+ # require "google/cloud"
193
+ #
194
+ # gcloud = Google::Cloud.new
195
+ # pubsub = gcloud.pubsub
196
+ #
197
+ # sub = pubsub.subscription "my-topic-sub", max: 10
198
+ # msgs = sub.pull
199
+ # ```
200
+ #
201
+ # The request for messages can also block until messages are available.
202
+ # (See {Google::Cloud::Pubsub::Subscription#wait_for_messages})
203
+ #
204
+ # ```ruby
205
+ # require "google/cloud"
206
+ #
207
+ # gcloud = Google::Cloud.new
208
+ # pubsub = gcloud.pubsub
209
+ #
210
+ # sub = pubsub.subscription "my-topic-sub"
211
+ # msgs = sub.wait_for_messages
212
+ # ```
213
+ #
214
+ # ## Acknowledging a Message
215
+ #
216
+ # Messages that are received can be acknowledged in Pub/Sub, marking the
217
+ # message to be removed so it cannot be pulled again.
218
+ #
219
+ # A Message that can be acknowledged is called a ReceivedMessage.
220
+ # ReceivedMessages can be acknowledged one at a time:
221
+ # (See {Google::Cloud::Pubsub::ReceivedMessage#acknowledge!})
222
+ #
223
+ # ```ruby
224
+ # require "google/cloud"
225
+ #
226
+ # gcloud = Google::Cloud.new
227
+ # pubsub = gcloud.pubsub
228
+ #
229
+ # sub = pubsub.subscription "my-topic-sub"
230
+ # sub.pull.each { |msg| msg.acknowledge! }
231
+ # ```
232
+ #
233
+ # Or, multiple messages can be acknowledged in a single API call:
234
+ # (See {Google::Cloud::Pubsub::Subscription#acknowledge})
235
+ #
236
+ # ```ruby
237
+ # require "google/cloud"
238
+ #
239
+ # gcloud = Google::Cloud.new
240
+ # pubsub = gcloud.pubsub
241
+ #
242
+ # sub = pubsub.subscription "my-topic-sub"
243
+ # received_messages = sub.pull
244
+ # sub.acknowledge received_messages
245
+ # ```
246
+ #
247
+ # ## Modifying a Deadline
248
+ #
249
+ # A message must be acknowledged after it is pulled, or Pub/Sub will mark
250
+ # the message for redelivery. The message acknowledgement deadline can
251
+ # delayed if more time is needed. This will allow more time to process the
252
+ # message before the message is marked for redelivery. (See
253
+ # {Google::Cloud::Pubsub::ReceivedMessage#delay!})
254
+ #
255
+ # ```ruby
256
+ # require "google/cloud"
257
+ #
258
+ # gcloud = Google::Cloud.new
259
+ # pubsub = gcloud.pubsub
260
+ #
261
+ # sub = pubsub.subscription "my-topic-sub"
262
+ # received_message = sub.pull.first
263
+ # if received_message
264
+ # puts received_message.message.data
265
+ # # Delay for 2 minutes
266
+ # received_message.delay! 120
267
+ # end
268
+ # ```
269
+ #
270
+ # The message can also be made available for immediate redelivery:
271
+ #
272
+ # ```ruby
273
+ # require "google/cloud"
274
+ #
275
+ # gcloud = Google::Cloud.new
276
+ # pubsub = gcloud.pubsub
277
+ #
278
+ # sub = pubsub.subscription "my-topic-sub"
279
+ # received_message = sub.pull.first
280
+ # if received_message
281
+ # puts received_message.message.data
282
+ # # Mark for redelivery by setting the deadline to now
283
+ # received_message.delay! 0
284
+ # end
285
+ # ```
286
+ #
287
+ # Multiple messages can be delayed or made available for immediate
288
+ # redelivery: (See {Google::Cloud::Pubsub::Subscription#delay})
289
+ #
290
+ # ```ruby
291
+ # require "google/cloud"
292
+ #
293
+ # gcloud = Google::Cloud.new
294
+ # pubsub = gcloud.pubsub
295
+ #
296
+ # sub = pubsub.subscription "my-topic-sub"
297
+ # received_messages = sub.pull
298
+ # sub.delay 120, received_messages
299
+ # ```
300
+ #
301
+ # ## Listening for Messages
302
+ #
303
+ # Long running workers are easy to create with `listen`, which runs an
304
+ # infinitely blocking loop to process messages as they are received. (See
305
+ # {Google::Cloud::Pubsub::Subscription#listen})
306
+ #
307
+ # ```ruby
308
+ # require "google/cloud"
309
+ #
310
+ # gcloud = Google::Cloud.new
311
+ # pubsub = gcloud.pubsub
312
+ #
313
+ # sub = pubsub.subscription "my-topic-sub"
314
+ # sub.listen do |msg|
315
+ # # process msg
316
+ # end
317
+ # ```
318
+ #
319
+ # Messages are retrieved in batches for efficiency. The number of messages
320
+ # pulled per batch can be limited with the `max` option:
321
+ #
322
+ # ```ruby
323
+ # require "google/cloud"
324
+ #
325
+ # gcloud = Google::Cloud.new
326
+ # pubsub = gcloud.pubsub
327
+ #
328
+ # sub = pubsub.subscription "my-topic-sub"
329
+ # sub.listen max: 20 do |msg|
330
+ # # process msg
331
+ # end
332
+ # ```
333
+ #
334
+ # When processing time and the acknowledgement deadline are a concern,
335
+ # messages can be automatically acknowledged as they are pulled with the
336
+ # `autoack` option:
337
+ #
338
+ # ```ruby
339
+ # require "google/cloud"
340
+ #
341
+ # gcloud = Google::Cloud.new
342
+ # pubsub = gcloud.pubsub
343
+ #
344
+ # sub = pubsub.subscription "my-topic-sub"
345
+ # sub.listen autoack: true do |msg|
346
+ # # process msg
347
+ # end
348
+ # ```
349
+ #
350
+ # ## Configuring retries and timeout
351
+ #
352
+ # You can configure how many times API requests may be automatically
353
+ # retried. When an API request fails, the response will be inspected to see
354
+ # if the request meets criteria indicating that it may succeed on retry,
355
+ # such as `500` and `503` status codes or a specific internal error code
356
+ # such as `rateLimitExceeded`. If it meets the criteria, the request will be
357
+ # retried after a delay. If another error occurs, the delay will be
358
+ # increased before a subsequent attempt, until the `retries` limit is
359
+ # reached.
360
+ #
361
+ # You can also set the request `timeout` value in seconds.
362
+ #
363
+ # ```ruby
364
+ # require "google/cloud"
365
+ #
366
+ # gcloud = Google::Cloud.new
367
+ # pubsub = gcloud.pubsub retries: 10, timeout: 120
368
+ # ```
369
+ #
370
+ # See the [Pub/Sub error codes](https://cloud.google.com/pubsub/error-codes)
371
+ # for a list of error conditions.
372
+ #
373
+ # ## Working Across Projects
374
+ #
375
+ # All calls to the Pub/Sub service use the same project and credentials
376
+ # provided to the {Google::Cloud#pubsub} method. However, it is common to
377
+ # reference topics or subscriptions in other projects, which can be achieved
378
+ # by using the `project` option. The main credentials must have permissions
379
+ # to the topics and subscriptions in other projects.
380
+ #
381
+ # ```ruby
382
+ # require "google/cloud"
383
+ #
384
+ # gcloud = Google::Cloud.new # my-project-id
385
+ # pubsub = gcloud.pubsub
386
+ #
387
+ # # Get a topic in the current project
388
+ # my_topic = pubsub.topic "my-topic"
389
+ # my_topic.name #=> "projects/my-project-id/topics/my-topic"
390
+ # # Get a topic in another project
391
+ # other_topic = pubsub.topic "other-topic", project: "other-project-id"
392
+ # other_topic.name #=> "projects/other-project-id/topics/other-topic"
393
+ # ```
394
+ #
395
+ # It is possible to create a subscription in the current project that pulls
396
+ # from a topic in another project:
397
+ #
398
+ # ```ruby
399
+ # require "google/cloud"
400
+ #
401
+ # gcloud = Google::Cloud.new # my-project-id
402
+ # pubsub = gcloud.pubsub
403
+ #
404
+ # # Get a topic in another project
405
+ # topic = pubsub.topic "other-topic", project: "other-project-id"
406
+ # # Create a subscription in the current project that pulls from
407
+ # # the topic in another project
408
+ # sub = topic.subscribe "my-sub"
409
+ # sub.name #=> "projects/my-project-id/subscriptions/my-sub"
410
+ # sub.topic.name #=> "projects/other-project-id/topics/other-topic"
411
+ # ```
412
+ #
413
+ # ## Using the Google Cloud Pub/Sub Emulator
414
+ #
415
+ # To develop and test your application locally, you can use the [Google
416
+ # Cloud Pub/Sub Emulator](https://cloud.google.com/pubsub/emulator), which
417
+ # provides [local
418
+ # emulation](https://cloud.google.com/sdk/gcloud/reference/beta/emulators/)
419
+ # of the production Google Cloud Pub/Sub environment. You can start the
420
+ # Google Cloud Pub/Sub emulator using the `gcloud` command-line tool.
421
+ #
422
+ # To configure your ruby code to use the emulator, set the
423
+ # `PUBSUB_EMULATOR_HOST` environment variable to the host and port where the
424
+ # emulator is running. The value can be set as an environment variable in
425
+ # the shell running the ruby code, or can be set directly in the ruby code
426
+ # as shown below.
427
+ #
428
+ # ```ruby
429
+ # require "google/cloud"
430
+ #
431
+ # # Make Pub/Sub use the emulator
432
+ # ENV["PUBSUB_EMULATOR_HOST"] = "localhost:8918"
433
+ #
434
+ # gcloud = Google::Cloud.new "emulator-project-id"
435
+ # pubsub = gcloud.pubsub
436
+ #
437
+ # # Get a topic in the current project
438
+ # my_topic = pubsub.new_topic "my-topic"
439
+ # my_topic.name #=> "projects/emulator-project-id/topics/my-topic"
440
+ # ```
441
+ #
442
+ module Pubsub
443
+ end
444
+ end
445
+ end