Class: Gcloud::Pubsub::Topic
- Inherits:
-
Object
- Object
- Gcloud::Pubsub::Topic
- Defined in:
- lib/gcloud/pubsub/topic.rb,
lib/gcloud/pubsub/topic/list.rb,
lib/gcloud/pubsub/topic/batch.rb
Overview
# Topic
A named resource to which messages are published.
Defined Under Namespace
Instance Attribute Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
-
#delete ⇒ Boolean
Permanently deletes the topic.
-
#exists? ⇒ Boolean
Determines whether the topic exists in the Pub/Sub service.
-
#initialize ⇒ Topic
constructor
A new instance of Topic.
-
#lazy? ⇒ Boolean
Determines whether the topic object was created with an HTTP call.
-
#name ⇒ Object
The name of the topic in the form of “/projects/project-identifier/topics/topic-name”.
-
#policy(force: nil) ⇒ Hash
Gets the access control policy.
-
#policy=(new_policy) ⇒ Object
Sets the access control policy.
-
#publish(data = nil, attributes = {}) {|batch| ... } ⇒ Message+
Publishes one or more messages to the topic.
-
#subscribe(subscription_name, deadline: nil, endpoint: nil) ⇒ Gcloud::Pubsub::Subscription
(also: #create_subscription, #new_subscription)
Creates a new Subscription object on the current Topic.
-
#subscription(subscription_name, skip_lookup: nil) ⇒ Gcloud::Pubsub::Subscription?
(also: #get_subscription, #find_subscription)
Retrieves subscription by name.
-
#subscriptions(token: nil, max: nil) ⇒ Array<Subscription>
(also: #find_subscriptions, #list_subscriptions)
Retrieves a list of subscription names for the given project.
-
#test_permissions(*permissions) ⇒ Array<Strings>
Tests the specified permissions against the [Cloud IAM](cloud.google.com/iam/) access control policy.
Constructor Details
#initialize ⇒ Topic
Returns a new instance of Topic.
48 49 50 51 52 53 |
# File 'lib/gcloud/pubsub/topic.rb', line 48 def initialize @service = nil @grpc = Google::Pubsub::V1::Topic.new @name = nil @exists = nil end |
Instance Attribute Details
#grpc ⇒ Object
44 45 46 |
# File 'lib/gcloud/pubsub/topic.rb', line 44 def grpc @grpc end |
#service ⇒ Object
40 41 42 |
# File 'lib/gcloud/pubsub/topic.rb', line 40 def service @service end |
Class Method Details
.from_grpc(grpc, service) ⇒ Object
471 472 473 474 475 476 |
# File 'lib/gcloud/pubsub/topic.rb', line 471 def self.from_grpc grpc, service new.tap do |f| f.grpc = grpc f.service = service end end |
.new_lazy(name, service, options = {}) ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'lib/gcloud/pubsub/topic.rb', line 57 def self.new_lazy name, service, = {} new.tap do |t| t.grpc = nil t.service = service t.instance_eval do @name = service.topic_path(name, ) end end end |
Instance Method Details
#delete ⇒ Boolean
Permanently deletes the topic.
88 89 90 91 92 93 94 |
# File 'lib/gcloud/pubsub/topic.rb', line 88 def delete ensure_service! service.delete_topic name return true rescue GRPC::BadStatus => e raise Error.from_error(e) end |
#exists? ⇒ Boolean
Determines whether the topic exists in the Pub/Sub service.
443 444 445 446 447 448 449 450 |
# File 'lib/gcloud/pubsub/topic.rb', line 443 def exists? # Always true if we have a grpc object return true unless @grpc.nil? # If we have a value, return it return @exists unless @exists.nil? ensure_grpc! @exists = !@grpc.nil? end |
#lazy? ⇒ Boolean
Determines whether the topic object was created with an HTTP call.
465 466 467 |
# File 'lib/gcloud/pubsub/topic.rb', line 465 def lazy? @grpc.nil? end |
#name ⇒ Object
The name of the topic in the form of “/projects/project-identifier/topics/topic-name”.
70 71 72 |
# File 'lib/gcloud/pubsub/topic.rb', line 70 def name @grpc ? @grpc.name : @name end |
#policy(force: nil) ⇒ Hash
Gets the access control policy.
340 341 342 343 344 345 346 347 348 349 |
# File 'lib/gcloud/pubsub/topic.rb', line 340 def policy force: nil @policy = nil if force @policy ||= begin ensure_service! grpc = service.get_topic_policy name JSON.parse(Google::Iam::V1::Policy.encode_json(grpc)) rescue GRPC::BadStatus => e raise Error.from_error(e) end end |
#policy=(new_policy) ⇒ Object
Sets the access control policy.
379 380 381 382 383 384 385 |
# File 'lib/gcloud/pubsub/topic.rb', line 379 def policy= new_policy ensure_service! grpc = service.set_topic_policy name, new_policy @policy = JSON.parse(Google::Iam::V1::Policy.encode_json(grpc)) rescue GRPC::BadStatus => e raise Error.from_error(e) end |
#publish(data = nil, attributes = {}) {|batch| ... } ⇒ Message+
Publishes one or more messages to the topic.
295 296 297 298 299 300 301 |
# File 'lib/gcloud/pubsub/topic.rb', line 295 def publish data = nil, attributes = {} ensure_service! batch = Batch.new data, attributes yield batch if block_given? return nil if batch..count.zero? batch end |
#subscribe(subscription_name, deadline: nil, endpoint: nil) ⇒ Gcloud::Pubsub::Subscription Also known as: create_subscription, new_subscription
Creates a new Subscription object on the current Topic.
143 144 145 146 147 148 149 150 |
# File 'lib/gcloud/pubsub/topic.rb', line 143 def subscribe subscription_name, deadline: nil, endpoint: nil ensure_service! = { deadline: deadline, endpoint: endpoint } grpc = service.create_subscription name, subscription_name, Subscription.from_grpc grpc, service rescue GRPC::BadStatus => e raise Error.from_error(e) end |
#subscription(subscription_name, skip_lookup: nil) ⇒ Gcloud::Pubsub::Subscription? Also known as: get_subscription, find_subscription
Retrieves subscription by name.
186 187 188 189 190 191 192 193 194 |
# File 'lib/gcloud/pubsub/topic.rb', line 186 def subscription subscription_name, skip_lookup: nil ensure_service! return Subscription.new_lazy subscription_name, service if skip_lookup grpc = service.get_subscription subscription_name Subscription.from_grpc grpc, service rescue GRPC::BadStatus => e return nil if e.code == 5 raise Error.from_error(e) end |
#subscriptions(token: nil, max: nil) ⇒ Array<Subscription> Also known as: find_subscriptions, list_subscriptions
Retrieves a list of subscription names for the given project.
239 240 241 242 243 244 245 246 |
# File 'lib/gcloud/pubsub/topic.rb', line 239 def subscriptions token: nil, max: nil ensure_service! = { token: token, max: max } grpc = service.list_topics_subscriptions name, Subscription::List.from_grpc grpc, service rescue GRPC::BadStatus => e raise Error.from_error(e) end |
#test_permissions(*permissions) ⇒ Array<Strings>
Tests the specified permissions against the [Cloud IAM](cloud.google.com/iam/) access control policy.
421 422 423 424 425 426 427 428 429 |
# File 'lib/gcloud/pubsub/topic.rb', line 421 def * = Array().flatten = Array().flatten ensure_service! grpc = service. name, grpc. rescue GRPC::BadStatus => e raise Error.from_error(e) end |