google-cloud-pubsub 0.26.0 → 2.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. checksums.yaml +5 -5
  2. data/.yardopts +12 -2
  3. data/AUTHENTICATION.md +178 -0
  4. data/CHANGELOG.md +659 -0
  5. data/CODE_OF_CONDUCT.md +40 -0
  6. data/CONTRIBUTING.md +187 -0
  7. data/EMULATOR.md +37 -0
  8. data/LICENSE +2 -2
  9. data/LOGGING.md +32 -0
  10. data/OVERVIEW.md +528 -0
  11. data/TROUBLESHOOTING.md +31 -0
  12. data/lib/google/cloud/pubsub/async_publisher/batch.rb +310 -0
  13. data/lib/google/cloud/pubsub/async_publisher.rb +402 -0
  14. data/lib/google/cloud/pubsub/batch_publisher.rb +100 -0
  15. data/lib/google/cloud/pubsub/convert.rb +91 -0
  16. data/lib/google/cloud/pubsub/credentials.rb +26 -10
  17. data/lib/google/cloud/pubsub/errors.rb +85 -0
  18. data/lib/google/cloud/pubsub/message.rb +80 -17
  19. data/lib/google/cloud/pubsub/policy.rb +17 -14
  20. data/lib/google/cloud/pubsub/project.rb +364 -250
  21. data/lib/google/cloud/pubsub/publish_result.rb +103 -0
  22. data/lib/google/cloud/pubsub/received_message.rb +162 -24
  23. data/lib/google/cloud/pubsub/retry_policy.rb +88 -0
  24. data/lib/google/cloud/pubsub/schema/list.rb +180 -0
  25. data/lib/google/cloud/pubsub/schema.rb +310 -0
  26. data/lib/google/cloud/pubsub/service.rb +281 -265
  27. data/lib/google/cloud/pubsub/snapshot/list.rb +21 -21
  28. data/lib/google/cloud/pubsub/snapshot.rb +55 -15
  29. data/lib/google/cloud/pubsub/subscriber/enumerator_queue.rb +54 -0
  30. data/lib/google/cloud/pubsub/subscriber/inventory.rb +173 -0
  31. data/lib/google/cloud/pubsub/subscriber/sequencer.rb +115 -0
  32. data/lib/google/cloud/pubsub/subscriber/stream.rb +400 -0
  33. data/lib/google/cloud/pubsub/subscriber/timed_unary_buffer.rb +230 -0
  34. data/lib/google/cloud/pubsub/subscriber.rb +417 -0
  35. data/lib/google/cloud/pubsub/subscription/list.rb +28 -28
  36. data/lib/google/cloud/pubsub/subscription/push_config.rb +268 -0
  37. data/lib/google/cloud/pubsub/subscription.rb +900 -172
  38. data/lib/google/cloud/pubsub/topic/list.rb +21 -21
  39. data/lib/google/cloud/pubsub/topic.rb +674 -95
  40. data/lib/google/cloud/pubsub/version.rb +6 -4
  41. data/lib/google/cloud/pubsub.rb +104 -439
  42. data/lib/google-cloud-pubsub.rb +60 -29
  43. metadata +88 -50
  44. data/README.md +0 -69
  45. data/lib/google/cloud/pubsub/topic/publisher.rb +0 -86
  46. data/lib/google/cloud/pubsub/v1/doc/google/protobuf/duration.rb +0 -77
  47. data/lib/google/cloud/pubsub/v1/doc/google/protobuf/field_mask.rb +0 -223
  48. data/lib/google/cloud/pubsub/v1/doc/google/protobuf/timestamp.rb +0 -81
  49. data/lib/google/cloud/pubsub/v1/doc/google/pubsub/v1/pubsub.rb +0 -503
  50. data/lib/google/cloud/pubsub/v1/publisher_client.rb +0 -605
  51. data/lib/google/cloud/pubsub/v1/publisher_client_config.json +0 -96
  52. data/lib/google/cloud/pubsub/v1/subscriber_client.rb +0 -1104
  53. data/lib/google/cloud/pubsub/v1/subscriber_client_config.json +0 -127
  54. data/lib/google/cloud/pubsub/v1.rb +0 -17
  55. data/lib/google/pubsub/v1/pubsub_pb.rb +0 -187
  56. data/lib/google/pubsub/v1/pubsub_services_pb.rb +0 -159
@@ -1,10 +1,10 @@
1
- # Copyright 2016 Google Inc. All rights reserved.
1
+ # Copyright 2016 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
5
5
  # You may obtain a copy of the License at
6
6
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
8
  #
9
9
  # Unless required by applicable law or agreed to in writing, software
10
10
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -17,7 +17,7 @@ require "google/cloud/errors"
17
17
 
18
18
  module Google
19
19
  module Cloud
20
- module Pubsub
20
+ module PubSub
21
21
  ##
22
22
  # # Policy
23
23
  #
@@ -27,7 +27,7 @@ module Google
27
27
  # is to read the current data from the service, update the data locally,
28
28
  # and then send the modified data for writing. This pattern may result in
29
29
  # a conflict if two or more processes attempt the sequence simultaneously.
30
- # IAM solves this problem with the {Google::Cloud::Pubsub::Policy#etag}
30
+ # IAM solves this problem with the {Google::Cloud::PubSub::Policy#etag}
31
31
  # property, which is used to verify whether the policy has changed since
32
32
  # the last request. When you make a request to with an `etag` value, Cloud
33
33
  # IAM compares the `etag` value in the request with the existing `etag`
@@ -37,8 +37,8 @@ module Google
37
37
  # When you update a policy, first read the policy (and its current `etag`)
38
38
  # from the service, then modify the policy locally, and then write the
39
39
  # modified policy to the service. See
40
- # {Google::Cloud::Pubsub::Topic#policy} and
41
- # {Google::Cloud::Pubsub::Topic#policy=}.
40
+ # {Google::Cloud::PubSub::Topic#policy} and
41
+ # {Google::Cloud::PubSub::Topic#policy=}.
42
42
  #
43
43
  # @see https://cloud.google.com/iam/docs/managing-policies Managing
44
44
  # policies
@@ -58,7 +58,7 @@ module Google
58
58
  # @example
59
59
  # require "google/cloud/pubsub"
60
60
  #
61
- # pubsub = Google::Cloud::Pubsub.new
61
+ # pubsub = Google::Cloud::PubSub.new
62
62
  # topic = pubsub.topic "my-topic"
63
63
  #
64
64
  # topic.policy do |p|
@@ -68,7 +68,8 @@ module Google
68
68
  # end
69
69
  #
70
70
  class Policy
71
- attr_reader :etag, :roles
71
+ attr_reader :etag
72
+ attr_reader :roles
72
73
 
73
74
  ##
74
75
  # @private Creates a Policy object.
@@ -93,7 +94,7 @@ module Google
93
94
  # @example
94
95
  # require "google/cloud/pubsub"
95
96
  #
96
- # pubsub = Google::Cloud::Pubsub.new
97
+ # pubsub = Google::Cloud::PubSub.new
97
98
  # topic = pubsub.topic "my-topic"
98
99
  #
99
100
  # topic.policy do |p|
@@ -120,7 +121,7 @@ module Google
120
121
  # @example
121
122
  # require "google/cloud/pubsub"
122
123
  #
123
- # pubsub = Google::Cloud::Pubsub.new
124
+ # pubsub = Google::Cloud::PubSub.new
124
125
  # topic = pubsub.topic "my-topic"
125
126
  #
126
127
  # topic.policy do |p|
@@ -145,7 +146,7 @@ module Google
145
146
  # @example
146
147
  # require "google/cloud/pubsub"
147
148
  #
148
- # pubsub = Google::Cloud::Pubsub.new
149
+ # pubsub = Google::Cloud::PubSub.new
149
150
  # topic = pubsub.topic "my-topic"
150
151
  #
151
152
  # topic.policy do |p|
@@ -160,14 +161,14 @@ module Google
160
161
  # @private Convert the Policy to a Google::Iam::V1::Policy object.
161
162
  def to_grpc
162
163
  Google::Iam::V1::Policy.new(
163
- etag: etag,
164
+ etag: etag,
164
165
  bindings: roles.keys.map do |role_name|
165
166
  next if roles[role_name].empty?
166
167
  Google::Iam::V1::Binding.new(
167
- role: role_name,
168
+ role: role_name,
168
169
  members: roles[role_name]
169
170
  )
170
- end
171
+ end.compact
171
172
  )
172
173
  end
173
174
 
@@ -181,5 +182,7 @@ module Google
181
182
  end
182
183
  end
183
184
  end
185
+
186
+ Pubsub = PubSub unless const_defined? :Pubsub
184
187
  end
185
188
  end
@@ -1,10 +1,10 @@
1
- # Copyright 2015 Google Inc. All rights reserved.
1
+ # Copyright 2015 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
5
5
  # You may obtain a copy of the License at
6
6
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
8
  #
9
9
  # Unless required by applicable law or agreed to in writing, software
10
10
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -14,15 +14,16 @@
14
14
 
15
15
 
16
16
  require "google/cloud/errors"
17
- require "google/cloud/env"
18
17
  require "google/cloud/pubsub/service"
19
18
  require "google/cloud/pubsub/credentials"
20
19
  require "google/cloud/pubsub/topic"
20
+ require "google/cloud/pubsub/batch_publisher"
21
+ require "google/cloud/pubsub/schema"
21
22
  require "google/cloud/pubsub/snapshot"
22
23
 
23
24
  module Google
24
25
  module Cloud
25
- module Pubsub
26
+ module PubSub
26
27
  ##
27
28
  # # Project
28
29
  #
@@ -39,7 +40,7 @@ module Google
39
40
  # @example
40
41
  # require "google/cloud/pubsub"
41
42
  #
42
- # pubsub = Google::Cloud::Pubsub.new
43
+ # pubsub = Google::Cloud::PubSub.new
43
44
  #
44
45
  # topic = pubsub.topic "my-topic"
45
46
  # topic.publish "task completed"
@@ -60,111 +61,189 @@ module Google
60
61
  # @example
61
62
  # require "google/cloud/pubsub"
62
63
  #
63
- # pubsub = Google::Cloud::Pubsub.new(
64
- # project: "my-project",
65
- # keyfile: "/path/to/keyfile.json"
64
+ # pubsub = Google::Cloud::PubSub.new(
65
+ # project_id: "my-project",
66
+ # credentials: "/path/to/keyfile.json"
66
67
  # )
67
68
  #
68
- # pubsub.project #=> "my-project"
69
+ # pubsub.project_id #=> "my-project"
69
70
  #
70
- def project
71
+ def project_id
71
72
  service.project
72
73
  end
73
-
74
- ##
75
- # @private Default project.
76
- def self.default_project
77
- ENV["PUBSUB_PROJECT"] ||
78
- ENV["GOOGLE_CLOUD_PROJECT"] ||
79
- ENV["GCLOUD_PROJECT"] ||
80
- Google::Cloud.env.project_id
81
- end
74
+ alias project project_id
82
75
 
83
76
  ##
84
77
  # Retrieves topic by name.
85
78
  #
86
- # The topic will be created if the topic does not exist and the
87
- # `autocreate` option is set to true.
88
- #
89
- # @param [String] topic_name Name of a topic.
90
- # @param [Boolean] autocreate Flag to control whether the requested
91
- # topic will be created if it does not exist. Ignored if `skip_lookup`
92
- # is `true`. The default value is `false`.
79
+ # @param [String] topic_name Name of a topic. The value can be a simple
80
+ # topic ID (relative name), in which case the current project ID will
81
+ # be supplied, or a fully-qualified topic name in the form
82
+ # `projects/{project_id}/topics/{topic_id}`.
93
83
  # @param [String] project If the topic belongs to a project other than
94
84
  # the one currently connected to, the alternate project ID can be
95
- # specified here.
85
+ # specified here. Optional. Not used if a fully-qualified topic name
86
+ # is provided for `topic_name`.
96
87
  # @param [Boolean] skip_lookup Optionally create a {Topic} object
97
88
  # without verifying the topic resource exists on the Pub/Sub service.
98
89
  # Calls made on this object will raise errors if the topic resource
99
- # does not exist. Default is `false`.
100
- #
101
- # @return [Google::Cloud::Pubsub::Topic, nil] Returns `nil` if topic
102
- # does not exist. Will return a newly created{
103
- # Google::Cloud::Pubsub::Topic} if the topic does not exist and
104
- # `autocreate` is set to `true`.
90
+ # does not exist. Default is `false`. Optional.
91
+ # @param [Hash] async A hash of values to configure the topic's
92
+ # {AsyncPublisher} that is created when {Topic#publish_async}
93
+ # is called. Optional.
94
+ #
95
+ # Hash keys and values may include the following:
96
+ #
97
+ # * `:max_bytes` (Integer) The maximum size of messages to be collected before the batch is published. Default
98
+ # is 1,000,000 (1MB).
99
+ # * `:max_messages` (Integer) The maximum number of messages to be collected before the batch is published.
100
+ # Default is 100.
101
+ # * `:interval` (Numeric) The number of seconds to collect messages before the batch is published. Default is
102
+ # 0.01.
103
+ # * `:threads` (Hash) The number of threads to create to handle concurrent calls by the publisher:
104
+ # * `:publish` (Integer) The number of threads used to publish messages. Default is 2.
105
+ # * `:callback` (Integer) The number of threads to handle the published messages' callbacks. Default is 4.
106
+ #
107
+ # @return [Google::Cloud::PubSub::Topic, nil] Returns `nil` if topic
108
+ # does not exist.
105
109
  #
106
110
  # @example
107
111
  # require "google/cloud/pubsub"
108
112
  #
109
- # pubsub = Google::Cloud::Pubsub.new
113
+ # pubsub = Google::Cloud::PubSub.new
110
114
  # topic = pubsub.topic "existing-topic"
111
115
  #
112
116
  # @example By default `nil` will be returned if topic does not exist.
113
117
  # require "google/cloud/pubsub"
114
118
  #
115
- # pubsub = Google::Cloud::Pubsub.new
119
+ # pubsub = Google::Cloud::PubSub.new
116
120
  # topic = pubsub.topic "non-existing-topic" # nil
117
121
  #
118
- # @example With the `autocreate` option set to `true`.
119
- # require "google/cloud/pubsub"
120
- #
121
- # pubsub = Google::Cloud::Pubsub.new
122
- # topic = pubsub.topic "non-existing-topic", autocreate: true
123
- #
124
122
  # @example Create topic in a different project with the `project` flag.
125
123
  # require "google/cloud/pubsub"
126
124
  #
127
- # pubsub = Google::Cloud::Pubsub.new
125
+ # pubsub = Google::Cloud::PubSub.new
128
126
  # topic = pubsub.topic "another-topic", project: "another-project"
129
127
  #
130
128
  # @example Skip the lookup against the service with `skip_lookup`:
131
129
  # require "google/cloud/pubsub"
132
130
  #
133
- # pubsub = Google::Cloud::Pubsub.new
131
+ # pubsub = Google::Cloud::PubSub.new
134
132
  # topic = pubsub.topic "another-topic", skip_lookup: true
135
133
  #
136
- def topic topic_name, autocreate: nil, project: nil, skip_lookup: nil
134
+ # @example Configuring AsyncPublisher to increase concurrent callbacks:
135
+ # require "google/cloud/pubsub"
136
+ #
137
+ # pubsub = Google::Cloud::PubSub.new
138
+ # topic = pubsub.topic "my-topic",
139
+ # async: { threads: { callback: 16 } }
140
+ #
141
+ # topic.publish_async "task completed" do |result|
142
+ # if result.succeeded?
143
+ # log_publish_success result.data
144
+ # else
145
+ # log_publish_failure result.data, result.error
146
+ # end
147
+ # end
148
+ #
149
+ # topic.async_publisher.stop!
150
+ #
151
+ def topic topic_name, project: nil, skip_lookup: nil, async: nil
137
152
  ensure_service!
138
153
  options = { project: project }
139
- return Topic.new_lazy(topic_name, service, options) if skip_lookup
140
- grpc = service.get_topic topic_name
141
- Topic.from_grpc grpc, service
154
+ return Topic.from_name topic_name, service, options if skip_lookup
155
+ grpc = service.get_topic topic_name, options
156
+ Topic.from_grpc grpc, service, async: async
142
157
  rescue Google::Cloud::NotFoundError
143
- return create_topic(topic_name) if autocreate
144
158
  nil
145
159
  end
146
- alias_method :get_topic, :topic
147
- alias_method :find_topic, :topic
160
+ alias get_topic topic
161
+ alias find_topic topic
148
162
 
149
163
  ##
150
164
  # Creates a new topic.
151
165
  #
152
- # @param [String] topic_name Name of a topic.
153
- #
154
- # @return [Google::Cloud::Pubsub::Topic]
166
+ # @param [String] topic_name Name of a topic. Required.
167
+ # The value can be a simple topic ID (relative name), in which
168
+ # case the current project ID will be supplied, or a fully-qualified
169
+ # topic name in the form `projects/{project_id}/topics/{topic_id}`.
170
+ #
171
+ # The topic ID (relative name) must start with a letter, and
172
+ # contain only letters (`[A-Za-z]`), numbers (`[0-9]`), dashes (`-`),
173
+ # underscores (`_`), periods (`.`), tildes (`~`), plus (`+`) or percent
174
+ # signs (`%`). It must be between 3 and 255 characters in length, and
175
+ # it must not start with `goog`.
176
+ # @param [Hash] labels A hash of user-provided labels associated with
177
+ # the topic. You can use these to organize and group your topics.
178
+ # Label keys and values can be no longer than 63 characters, can only
179
+ # contain lowercase letters, numeric characters, underscores and
180
+ # dashes. International characters are allowed. Label values are
181
+ # optional. Label keys must start with a letter and each label in the
182
+ # list must have a different key. See [Creating and Managing
183
+ # Labels](https://cloud.google.com/pubsub/docs/labels).
184
+ # @param [String] kms_key The Cloud KMS encryption key that will be used
185
+ # to protect access to messages published on this topic. Optional.
186
+ # For example: `projects/a/locations/b/keyRings/c/cryptoKeys/d`
187
+ # @param [Array<String>] persistence_regions The list of GCP region IDs
188
+ # where messages that are published to the topic may be persisted in
189
+ # storage. Optional.
190
+ # @param [Hash] async A hash of values to configure the topic's
191
+ # {AsyncPublisher} that is created when {Topic#publish_async}
192
+ # is called. Optional.
193
+ #
194
+ # Hash keys and values may include the following:
195
+ #
196
+ # * `:max_bytes` (Integer) The maximum size of messages to be collected
197
+ # before the batch is published. Default is 1,000,000 (1MB).
198
+ # * `:max_messages` (Integer) The maximum number of messages to be
199
+ # collected before the batch is published. Default is 100.
200
+ # * `:interval` (Numeric) The number of seconds to collect messages before
201
+ # the batch is published. Default is 0.01.
202
+ # * `:threads` (Hash) The number of threads to create to handle concurrent
203
+ # calls by the publisher:
204
+ #
205
+ # * `:publish` (Integer) The number of threads used to publish messages.
206
+ # Default is 2.
207
+ # * `:callback` (Integer) The number of threads to handle the published
208
+ # messages' callbacks. Default is 4.
209
+ # @param [String] schema_name The name of the schema that messages
210
+ # published should be validated against. Optional. The value can be a
211
+ # simple schema ID (relative name), in which case the current project
212
+ # ID will be supplied, or a fully-qualified schema name in the form
213
+ # `projects/{project_id}/schemas/{schema_id}`. If provided,
214
+ # `message_encoding` must also be provided.
215
+ # @param [String, Symbol] message_encoding The encoding of messages validated
216
+ # against the schema identified by `schema_name`. Optional. Values include:
217
+ #
218
+ # * `JSON` - JSON encoding.
219
+ # * `BINARY` - Binary encoding, as defined by the schema type. For some
220
+ # schema types, binary encoding may not be available.
221
+ #
222
+ # @return [Google::Cloud::PubSub::Topic]
155
223
  #
156
224
  # @example
157
225
  # require "google/cloud/pubsub"
158
226
  #
159
- # pubsub = Google::Cloud::Pubsub.new
227
+ # pubsub = Google::Cloud::PubSub.new
160
228
  # topic = pubsub.create_topic "my-topic"
161
229
  #
162
- def create_topic topic_name
230
+ def create_topic topic_name,
231
+ labels: nil,
232
+ kms_key: nil,
233
+ persistence_regions: nil,
234
+ async: nil,
235
+ schema_name: nil,
236
+ message_encoding: nil
163
237
  ensure_service!
164
- grpc = service.create_topic topic_name
165
- Topic.from_grpc grpc, service
238
+ grpc = service.create_topic topic_name,
239
+ labels: labels,
240
+ kms_key_name: kms_key,
241
+ persistence_regions: persistence_regions,
242
+ schema_name: schema_name,
243
+ message_encoding: message_encoding
244
+ Topic.from_grpc grpc, service, async: async
166
245
  end
167
- alias_method :new_topic, :create_topic
246
+ alias new_topic create_topic
168
247
 
169
248
  ##
170
249
  # Retrieves a list of topics for the given project.
@@ -174,13 +253,13 @@ module Google
174
253
  # the system should return the next page of data.
175
254
  # @param [Integer] max Maximum number of topics to return.
176
255
  #
177
- # @return [Array<Google::Cloud::Pubsub::Topic>] (See
178
- # {Google::Cloud::Pubsub::Topic::List})
256
+ # @return [Array<Google::Cloud::PubSub::Topic>] (See
257
+ # {Google::Cloud::PubSub::Topic::List})
179
258
  #
180
259
  # @example
181
260
  # require "google/cloud/pubsub"
182
261
  #
183
- # pubsub = Google::Cloud::Pubsub.new
262
+ # pubsub = Google::Cloud::PubSub.new
184
263
  #
185
264
  # topics = pubsub.topics
186
265
  # topics.each do |topic|
@@ -190,7 +269,7 @@ module Google
190
269
  # @example Retrieve all topics: (See {Topic::List#all})
191
270
  # require "google/cloud/pubsub"
192
271
  #
193
- # pubsub = Google::Cloud::Pubsub.new
272
+ # pubsub = Google::Cloud::PubSub.new
194
273
  #
195
274
  # topics = pubsub.topics
196
275
  # topics.all do |topic|
@@ -203,183 +282,32 @@ module Google
203
282
  grpc = service.list_topics options
204
283
  Topic::List.from_grpc grpc, service, max
205
284
  end
206
- alias_method :find_topics, :topics
207
- alias_method :list_topics, :topics
208
-
209
- ##
210
- # Publishes one or more messages to the given topic. The topic will be
211
- # created if the topic does previously not exist and the `autocreate`
212
- # option is provided.
213
- #
214
- # A note about auto-creating the topic: Any message published to a topic
215
- # without a subscription will be lost.
216
- #
217
- # @param [String] topic_name Name of a topic.
218
- # @param [String, File] data The message data.
219
- # @param [Hash] attributes Optional attributes for the message.
220
- # @option attributes [Boolean] :autocreate Flag to control whether the
221
- # provided topic will be created if it does not exist.
222
- # @yield [publisher] a block for publishing multiple messages in one
223
- # request
224
- # @yieldparam [Topic::Publisher] publisher the topic publisher object
225
- #
226
- # @return [Message, Array<Message>] Returns the published message when
227
- # called without a block, or an array of messages when called with a
228
- # block.
229
- #
230
- # @example
231
- # require "google/cloud/pubsub"
232
- #
233
- # pubsub = Google::Cloud::Pubsub.new
234
- #
235
- # msg = pubsub.publish "my-topic", "task completed"
236
- #
237
- # @example A message can be published using a File object:
238
- # require "google/cloud/pubsub"
239
- #
240
- # pubsub = Google::Cloud::Pubsub.new
241
- #
242
- # msg = pubsub.publish "my-topic", File.open("message.txt")
243
- #
244
- # @example Additionally, a message can be published with attributes:
245
- # require "google/cloud/pubsub"
246
- #
247
- # pubsub = Google::Cloud::Pubsub.new
248
- #
249
- # msg = pubsub.publish "my-topic", "task completed", foo: :bar,
250
- # this: :that
251
- #
252
- # @example Multiple messages can be sent at the same time using a block:
253
- # require "google/cloud/pubsub"
254
- #
255
- # pubsub = Google::Cloud::Pubsub.new
256
- #
257
- # msgs = pubsub.publish "my-topic" do |p|
258
- # p.publish "task 1 completed", foo: :bar
259
- # p.publish "task 2 completed", foo: :baz
260
- # p.publish "task 3 completed", foo: :bif
261
- # end
262
- #
263
- # @example With `autocreate`:
264
- # require "google/cloud/pubsub"
265
- #
266
- # pubsub = Google::Cloud::Pubsub.new
267
- #
268
- # msg = pubsub.publish "new-topic", "task completed", autocreate: true
269
- #
270
- def publish topic_name, data = nil, attributes = {}
271
- # Fix parameters
272
- if data.is_a?(::Hash) && attributes.empty?
273
- attributes = data
274
- data = nil
275
- end
276
- # extract autocreate option
277
- autocreate = attributes.delete :autocreate
278
- ensure_service!
279
- publisher = Topic::Publisher.new data, attributes
280
- yield publisher if block_given?
281
- return nil if publisher.messages.count.zero?
282
- publish_batch_messages topic_name, publisher, autocreate
283
- end
284
-
285
- ##
286
- # Creates a new {Subscription} object for the provided topic. The topic
287
- # will be created if the topic does previously not exist and the
288
- # `autocreate` option is provided.
289
- #
290
- # @param [String] topic_name Name of a topic.
291
- # @param [String] subscription_name Name of the new subscription. Must
292
- # start with a letter, and contain only letters ([A-Za-z]), numbers
293
- # ([0-9], dashes (-), underscores (_), periods (.), tildes (~), plus
294
- # (+) or percent signs (%). It must be between 3 and 255 characters in
295
- # length, and it must not start with "goog".
296
- # @param [Integer] deadline The maximum number of seconds after a
297
- # subscriber receives a message before the subscriber should
298
- # acknowledge the message.
299
- # @param [Boolean] retain_acked Indicates whether to retain acknowledged
300
- # messages. If `true`, then messages are not expunged from the
301
- # subscription's backlog, even if they are acknowledged, until they
302
- # fall out of the `retention_duration` window. Default is `false`.
303
- # @param [Numeric] retention How long to retain unacknowledged messages
304
- # in the subscription's backlog, from the moment a message is
305
- # published. If `retain_acked` is `true`, then this also configures
306
- # the retention of acknowledged messages, and thus configures how far
307
- # back in time a {#seek} can be done. Cannot be more than 604,800
308
- # seconds (7 days) or less than 600 seconds (10 minutes). Default is
309
- # 604,800 seconds (7 days).
310
- # @param [String] endpoint A URL locating the endpoint to which messages
311
- # should be pushed.
312
- # @param [String] autocreate Flag to control whether the topic will be
313
- # created if it does not exist.
314
- #
315
- # @return [Google::Cloud::Pubsub::Subscription]
316
- #
317
- # @example
318
- # require "google/cloud/pubsub"
319
- #
320
- # pubsub = Google::Cloud::Pubsub.new
321
- #
322
- # sub = pubsub.subscribe "my-topic", "my-topic-sub"
323
- # sub.name #=> "my-topic-sub"
324
- #
325
- # @example Wait 2 minutes for acknowledgement and push all to endpoint:
326
- # require "google/cloud/pubsub"
327
- #
328
- # pubsub = Google::Cloud::Pubsub.new
329
- #
330
- # sub = pubsub.subscribe "my-topic", "my-topic-sub",
331
- # deadline: 120,
332
- # endpoint: "https://example.com/push"
333
- #
334
- # @example With `autocreate`:
335
- # require "google/cloud/pubsub"
336
- #
337
- # pubsub = Google::Cloud::Pubsub.new
338
- #
339
- # sub = pubsub.subscribe "new-topic", "new-topic-sub",
340
- # autocreate: true
341
- #
342
- def subscribe topic_name, subscription_name, deadline: nil,
343
- retain_acked: false, retention: nil, endpoint: nil,
344
- autocreate: nil
345
- ensure_service!
346
- options = { deadline: deadline, retain_acked: retain_acked,
347
- retention: retention, endpoint: endpoint }
348
- grpc = service.create_subscription topic_name,
349
- subscription_name, options
350
- Subscription.from_grpc grpc, service
351
- rescue Google::Cloud::NotFoundError => e
352
- if autocreate
353
- create_topic topic_name
354
- return subscribe(topic_name, subscription_name,
355
- deadline: deadline, retain_acked: retain_acked,
356
- retention: retention, endpoint: endpoint,
357
- autocreate: false)
358
- end
359
- raise e
360
- end
361
- alias_method :create_subscription, :subscribe
362
- alias_method :new_subscription, :subscribe
285
+ alias find_topics topics
286
+ alias list_topics topics
363
287
 
364
288
  ##
365
289
  # Retrieves subscription by name.
366
290
  #
367
- # @param [String] subscription_name Name of a subscription.
291
+ # @param [String] subscription_name Name of a subscription. The value can
292
+ # be a simple subscription ID, in which case the current project ID
293
+ # will be supplied, or a fully-qualified subscription name in the form
294
+ # `projects/{project_id}/subscriptions/{subscription_id}`.
368
295
  # @param [String] project If the subscription belongs to a project other
369
296
  # than the one currently connected to, the alternate project ID can be
370
- # specified here.
297
+ # specified here. Not used if a fully-qualified subscription name is
298
+ # provided for `subscription_name`.
371
299
  # @param [Boolean] skip_lookup Optionally create a {Subscription} object
372
300
  # without verifying the subscription resource exists on the Pub/Sub
373
301
  # service. Calls made on this object will raise errors if the service
374
302
  # resource does not exist. Default is `false`.
375
303
  #
376
- # @return [Google::Cloud::Pubsub::Subscription, nil] Returns `nil` if
304
+ # @return [Google::Cloud::PubSub::Subscription, nil] Returns `nil` if
377
305
  # the subscription does not exist
378
306
  #
379
307
  # @example
380
308
  # require "google/cloud/pubsub"
381
309
  #
382
- # pubsub = Google::Cloud::Pubsub.new
310
+ # pubsub = Google::Cloud::PubSub.new
383
311
  #
384
312
  # sub = pubsub.subscription "my-sub"
385
313
  # sub.name #=> "projects/my-project/subscriptions/my-sub"
@@ -387,7 +315,7 @@ module Google
387
315
  # @example Skip the lookup against the service with `skip_lookup`:
388
316
  # require "google/cloud/pubsub"
389
317
  #
390
- # pubsub = Google::Cloud::Pubsub.new
318
+ # pubsub = Google::Cloud::PubSub.new
391
319
  #
392
320
  # # No API call is made to retrieve the subscription information.
393
321
  # sub = pubsub.subscription "my-sub", skip_lookup: true
@@ -396,16 +324,14 @@ module Google
396
324
  def subscription subscription_name, project: nil, skip_lookup: nil
397
325
  ensure_service!
398
326
  options = { project: project }
399
- if skip_lookup
400
- return Subscription.new_lazy subscription_name, service, options
401
- end
402
- grpc = service.get_subscription subscription_name
327
+ return Subscription.from_name subscription_name, service, options if skip_lookup
328
+ grpc = service.get_subscription subscription_name, options
403
329
  Subscription.from_grpc grpc, service
404
330
  rescue Google::Cloud::NotFoundError
405
331
  nil
406
332
  end
407
- alias_method :get_subscription, :subscription
408
- alias_method :find_subscription, :subscription
333
+ alias get_subscription subscription
334
+ alias find_subscription subscription
409
335
 
410
336
  ##
411
337
  # Retrieves a list of subscriptions for the given project.
@@ -414,13 +340,13 @@ module Google
414
340
  # part of the larger set of results to view.
415
341
  # @param [Integer] max Maximum number of subscriptions to return.
416
342
  #
417
- # @return [Array<Google::Cloud::Pubsub::Subscription>] (See
418
- # {Google::Cloud::Pubsub::Subscription::List})
343
+ # @return [Array<Google::Cloud::PubSub::Subscription>] (See
344
+ # {Google::Cloud::PubSub::Subscription::List})
419
345
  #
420
346
  # @example
421
347
  # require "google/cloud/pubsub"
422
348
  #
423
- # pubsub = Google::Cloud::Pubsub.new
349
+ # pubsub = Google::Cloud::PubSub.new
424
350
  #
425
351
  # subs = pubsub.subscriptions
426
352
  # subs.each do |sub|
@@ -430,7 +356,7 @@ module Google
430
356
  # @example Retrieve all subscriptions: (See {Subscription::List#all})
431
357
  # require "google/cloud/pubsub"
432
358
  #
433
- # pubsub = Google::Cloud::Pubsub.new
359
+ # pubsub = Google::Cloud::PubSub.new
434
360
  #
435
361
  # subs = pubsub.subscriptions
436
362
  # subs.all do |sub|
@@ -443,8 +369,8 @@ module Google
443
369
  grpc = service.list_subscriptions options
444
370
  Subscription::List.from_grpc grpc, service, max
445
371
  end
446
- alias_method :find_subscriptions, :subscriptions
447
- alias_method :list_subscriptions, :subscriptions
372
+ alias find_subscriptions subscriptions
373
+ alias list_subscriptions subscriptions
448
374
 
449
375
 
450
376
  ##
@@ -454,13 +380,13 @@ module Google
454
380
  # part of the larger set of results to view.
455
381
  # @param [Integer] max Maximum number of snapshots to return.
456
382
  #
457
- # @return [Array<Google::Cloud::Pubsub::Snapshot>] (See
458
- # {Google::Cloud::Pubsub::Snapshot::List})
383
+ # @return [Array<Google::Cloud::PubSub::Snapshot>] (See
384
+ # {Google::Cloud::PubSub::Snapshot::List})
459
385
  #
460
386
  # @example
461
387
  # require "google/cloud/pubsub"
462
388
  #
463
- # pubsub = Google::Cloud::Pubsub.new
389
+ # pubsub = Google::Cloud::PubSub.new
464
390
  #
465
391
  # snapshots = pubsub.snapshots
466
392
  # snapshots.each do |snapshot|
@@ -470,7 +396,7 @@ module Google
470
396
  # @example Retrieve all snapshots: (See {Snapshot::List#all})
471
397
  # require "google/cloud/pubsub"
472
398
  #
473
- # pubsub = Google::Cloud::Pubsub.new
399
+ # pubsub = Google::Cloud::PubSub.new
474
400
  #
475
401
  # snapshots = pubsub.snapshots
476
402
  # snapshots.all do |snapshot|
@@ -483,8 +409,200 @@ module Google
483
409
  grpc = service.list_snapshots options
484
410
  Snapshot::List.from_grpc grpc, service, max
485
411
  end
486
- alias_method :find_snapshots, :snapshots
487
- alias_method :list_snapshots, :snapshots
412
+ alias find_snapshots snapshots
413
+ alias list_snapshots snapshots
414
+
415
+ ##
416
+ # Retrieves schema by name.
417
+ #
418
+ # @param [String] schema_name Name of a schema. The value can
419
+ # be a simple schema ID, in which case the current project ID
420
+ # will be supplied, or a fully-qualified schema name in the form
421
+ # `projects/{project_id}/schemas/{schema_id}`.
422
+ # @param view [Symbol, String, nil] Possible values:
423
+ # * `BASIC` - Include the `name` and `type` of the schema, but not the `definition`.
424
+ # * `FULL` - Include all Schema object fields.
425
+ #
426
+ # The default value is `BASIC`.
427
+ # @param [String] project If the schema belongs to a project other
428
+ # than the one currently connected to, the alternate project ID can be
429
+ # specified here. Not used if a fully-qualified schema name is
430
+ # provided for `schema_name`.
431
+ # @param [Boolean] skip_lookup Optionally create a {Schema} object
432
+ # without verifying the schema resource exists on the Pub/Sub
433
+ # service. Calls made on this object will raise errors if the service
434
+ # resource does not exist. Default is `false`.
435
+ #
436
+ # @return [Google::Cloud::PubSub::Schema, nil] Returns `nil` if
437
+ # the schema does not exist.
438
+ #
439
+ # @example
440
+ # require "google/cloud/pubsub"
441
+ #
442
+ # pubsub = Google::Cloud::PubSub.new
443
+ #
444
+ # schema = pubsub.schema "my-schema"
445
+ # schema.name #=> "projects/my-project/schemas/my-schema"
446
+ # schema.type #=> :PROTOCOL_BUFFER
447
+ # # schema.definition # nil - Use view: :full to load complete resource.
448
+ #
449
+ # @example Skip the lookup against the service with `skip_lookup`:
450
+ # require "google/cloud/pubsub"
451
+ #
452
+ # pubsub = Google::Cloud::PubSub.new
453
+ #
454
+ # # No API call is made to retrieve the schema information.
455
+ # # The default project is used in the name.
456
+ # schema = pubsub.schema "my-schema", skip_lookup: true
457
+ # schema.name #=> "projects/my-project/schemas/my-schema"
458
+ # schema.type #=> nil
459
+ # schema.definition #=> nil
460
+ #
461
+ # @example Get the schema definition with `view: :full`:
462
+ # require "google/cloud/pubsub"
463
+ #
464
+ # pubsub = Google::Cloud::PubSub.new
465
+ #
466
+ # schema = pubsub.schema "my-schema", view: :full
467
+ # schema.name #=> "projects/my-project/schemas/my-schema"
468
+ # schema.type #=> :PROTOCOL_BUFFER
469
+ # schema.definition # The schema definition
470
+ #
471
+ def schema schema_name, view: nil, project: nil, skip_lookup: nil
472
+ ensure_service!
473
+ options = { project: project }
474
+ return Schema.from_name schema_name, view, service, options if skip_lookup
475
+ view ||= :BASIC
476
+ grpc = service.get_schema schema_name, view, options
477
+ Schema.from_grpc grpc, service
478
+ rescue Google::Cloud::NotFoundError
479
+ nil
480
+ end
481
+ alias get_schema schema
482
+ alias find_schema schema
483
+
484
+ ##
485
+ # Creates a new schema.
486
+ #
487
+ # @param [String] schema_id The ID to use for the schema, which will
488
+ # become the final component of the schema's resource name. Required.
489
+ #
490
+ # The schema ID (relative name) must start with a letter, and
491
+ # contain only letters (`[A-Za-z]`), numbers (`[0-9]`), dashes (`-`),
492
+ # underscores (`_`), periods (`.`), tildes (`~`), plus (`+`) or percent
493
+ # signs (`%`). It must be between 3 and 255 characters in length, and
494
+ # it must not start with `goog`.
495
+ # @param [String, Symbol] type The type of the schema. Required. Possible
496
+ # values are case-insensitive and include:
497
+ #
498
+ # * `PROTOCOL_BUFFER` - A Protocol Buffer schema definition.
499
+ # * `AVRO` - An Avro schema definition.
500
+ # @param [String] definition The definition of the schema. Required. This
501
+ # should be a string representing the full definition of the schema that
502
+ # is a valid schema definition of the type specified in `type`.
503
+ # @param [String] project If the schema belongs to a project other
504
+ # than the one currently connected to, the alternate project ID can be
505
+ # specified here. Optional.
506
+ #
507
+ # @return [Google::Cloud::PubSub::Schema]
508
+ #
509
+ # @example
510
+ # require "google/cloud/pubsub"
511
+ #
512
+ # pubsub = Google::Cloud::PubSub.new
513
+ #
514
+ # definition = "..."
515
+ # schema = pubsub.create_schema "my-schema", :avro, definition
516
+ # schema.name #=> "projects/my-project/schemas/my-schema"
517
+ #
518
+ def create_schema schema_id, type, definition, project: nil
519
+ ensure_service!
520
+ type = type.to_s.upcase
521
+ grpc = service.create_schema schema_id, type, definition, project: project
522
+ Schema.from_grpc grpc, service
523
+ end
524
+ alias new_schema create_schema
525
+
526
+ ##
527
+ # Retrieves a list of schemas for the given project.
528
+ #
529
+ # @param view [String, Symbol, nil] The set of fields to return in the response. Possible values:
530
+ #
531
+ # * `BASIC` - Include the `name` and `type` of the schema, but not the `definition`.
532
+ # * `FULL` - Include all Schema object fields.
533
+ #
534
+ # The default value is `BASIC`.
535
+ # @param [String] token A previously-returned page token representing
536
+ # part of the larger set of results to view.
537
+ # @param [Integer] max Maximum number of schemas to return.
538
+ #
539
+ # @return [Array<Google::Cloud::PubSub::Schema>] (See
540
+ # {Google::Cloud::PubSub::Schema::List})
541
+ #
542
+ # @example
543
+ # require "google/cloud/pubsub"
544
+ #
545
+ # pubsub = Google::Cloud::PubSub.new
546
+ #
547
+ # schemas = pubsub.schemas
548
+ # schemas.each do |schema|
549
+ # puts schema.name
550
+ # end
551
+ #
552
+ # @example Retrieve all schemas: (See {Schema::List#all})
553
+ # require "google/cloud/pubsub"
554
+ #
555
+ # pubsub = Google::Cloud::PubSub.new
556
+ #
557
+ # schemas = pubsub.schemas
558
+ # schemas.all do |schema|
559
+ # puts schema.name
560
+ # end
561
+ #
562
+ def schemas view: nil, token: nil, max: nil
563
+ ensure_service!
564
+ view ||= :BASIC
565
+ options = { token: token, max: max }
566
+ grpc = service.list_schemas view, options
567
+ Schema::List.from_grpc grpc, service, view, max
568
+ end
569
+ alias find_schemas schemas
570
+ alias list_schemas schemas
571
+
572
+ ##
573
+ # Validates a schema type and definition.
574
+ #
575
+ # @param [String, Symbol] type The type of the schema. Required. Possible
576
+ # values are case-insensitive and include:
577
+ #
578
+ # * `PROTOCOL_BUFFER` - A Protocol Buffer schema definition.
579
+ # * `AVRO` - An Avro schema definition.
580
+ # @param [String] definition The definition of the schema. Required. This
581
+ # should be a string representing the full definition of the schema that
582
+ # is a valid schema definition of the type specified in `type`.
583
+ # @param [String] project If the schema belongs to a project other
584
+ # than the one currently connected to, the alternate project ID can be
585
+ # specified here. Optional.
586
+ #
587
+ # @return [Boolean] `true` if the schema is valid, `false` otherwise.
588
+ #
589
+ # @example
590
+ # require "google/cloud/pubsub"
591
+ #
592
+ # pubsub = Google::Cloud::PubSub.new
593
+ #
594
+ # definition = "..."
595
+ # pubsub.validate_schema :avro, definition #=> true
596
+ #
597
+ def valid_schema? type, definition, project: nil
598
+ ensure_service!
599
+ type = type.to_s.upcase
600
+ service.validate_schema type, definition, project: project # return type is empty
601
+ true
602
+ rescue Google::Cloud::InvalidArgumentError
603
+ false
604
+ end
605
+ alias validate_schema valid_schema?
488
606
 
489
607
  protected
490
608
 
@@ -492,22 +610,18 @@ module Google
492
610
  # @private Raise an error unless an active connection to the service is
493
611
  # available.
494
612
  def ensure_service!
495
- fail "Must have active connection to service" unless service
613
+ raise "Must have active connection to service" unless service
496
614
  end
497
615
 
498
616
  ##
499
617
  # Call the publish API with arrays of data data and attrs.
500
- def publish_batch_messages topic_name, batch, autocreate = false
618
+ def publish_batch_messages topic_name, batch
501
619
  grpc = service.publish topic_name, batch.messages
502
620
  batch.to_gcloud_messages Array(grpc.message_ids)
503
- rescue Google::Cloud::NotFoundError => e
504
- if autocreate
505
- create_topic topic_name
506
- return publish_batch_messages topic_name, batch, false
507
- end
508
- raise e
509
621
  end
510
622
  end
511
623
  end
624
+
625
+ Pubsub = PubSub unless const_defined? :Pubsub
512
626
  end
513
627
  end