Class: Gcloud::Pubsub::Topic

Inherits:
Object
  • Object
show all
Defined in:
lib/gcloud/pubsub/topic.rb,
lib/gcloud/pubsub/topic/list.rb,
lib/gcloud/pubsub/topic/publisher.rb

Overview

# Topic

A named resource to which messages are published.

Examples:

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub

topic = pubsub.topic "my-topic"
topic.publish "task completed"

Defined Under Namespace

Classes: List, Publisher

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTopic

Returns a new instance of Topic.



49
50
51
52
53
54
# File 'lib/gcloud/pubsub/topic.rb', line 49

def initialize
  @service = nil
  @grpc = Google::Pubsub::V1::Topic.new
  @name = nil
  @exists = nil
end

Instance Attribute Details

#grpcObject



45
46
47
# File 'lib/gcloud/pubsub/topic.rb', line 45

def grpc
  @grpc
end

#serviceObject



41
42
43
# File 'lib/gcloud/pubsub/topic.rb', line 41

def service
  @service
end

Class Method Details

.from_grpc(grpc, service) ⇒ Object



477
478
479
480
481
482
# File 'lib/gcloud/pubsub/topic.rb', line 477

def self.from_grpc grpc, service
  new.tap do |f|
    f.grpc = grpc
    f.service = service
  end
end

.new_lazy(name, service, options = {}) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/gcloud/pubsub/topic.rb', line 58

def self.new_lazy name, service, options = {}
  new.tap do |t|
    t.grpc = nil
    t.service = service
    t.instance_variable_set "@name", service.topic_path(name, options)
  end
end

Instance Method Details

#deleteBoolean

Permanently deletes the topic.

Examples:

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub

topic = pubsub.topic "my-topic"
topic.delete

Returns:

  • (Boolean)

    Returns ‘true` if the topic was deleted.



87
88
89
90
91
# File 'lib/gcloud/pubsub/topic.rb', line 87

def delete
  ensure_service!
  service.delete_topic name
  true
end

#exists?Boolean

Determines whether the topic exists in the Pub/Sub service.

Examples:

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub

topic = pubsub.topic "my-topic"
topic.exists? #=> true

Returns:

  • (Boolean)


449
450
451
452
453
454
455
456
# File 'lib/gcloud/pubsub/topic.rb', line 449

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.

Examples:

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub

topic = pubsub.topic "my-topic"
topic.lazy? #=> false

Returns:

  • (Boolean)


471
472
473
# File 'lib/gcloud/pubsub/topic.rb', line 471

def lazy?
  @grpc.nil?
end

#nameObject

The name of the topic in the form of “/projects/project-identifier/topics/topic-name”.



69
70
71
# File 'lib/gcloud/pubsub/topic.rb', line 69

def name
  @grpc ? @grpc.name : @name
end

#policy(force: nil) {|policy| ... } ⇒ Policy

Gets the [Cloud IAM](cloud.google.com/iam/) access control policy for this topic.

Examples:

Policy values are memoized to reduce the number of API calls:

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub
topic = pubsub.topic "my-topic"

policy = topic.policy # API call
policy_2 = topic.policy # No API call

Use ‘force` to retrieve the latest policy from the service:

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub
topic = pubsub.topic "my-topic"

policy = topic.policy force: true # API call
policy_2 = topic.policy force: true # API call

Update the policy by passing a block:

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub
topic = pubsub.topic "my-topic"

policy = topic.policy do |p|
  p.add "roles/owner", "user:[email protected]"
end # 2 API calls

Parameters:

  • force (Boolean) (defaults to: nil)

    Force the latest policy to be retrieved from the Pub/Sub service when ‘true`. Otherwise the policy will be memoized to reduce the number of API calls made to the Pub/Sub service. The default is `false`.

Yields:

  • (policy)

    A block for updating the policy. The latest policy will be read from the Pub/Sub service and passed to the block. After the block completes, the modified policy will be written to the service.

Yield Parameters:

  • policy (Policy)

    the current Cloud IAM Policy for this topic

Returns:

  • (Policy)

    the current Cloud IAM Policy for this topic

See Also:



348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/gcloud/pubsub/topic.rb', line 348

def policy force: nil
  @policy = nil if force || block_given?
  @policy ||= begin
    ensure_service!
    grpc = service.get_topic_policy name
    Policy.from_grpc grpc
  end
  return @policy unless block_given?
  p = @policy.deep_dup
  yield p
  self.policy = p
end

#policy=(new_policy) ⇒ Object

Updates the [Cloud IAM](cloud.google.com/iam/) access control policy for this topic. The policy should be read from #policy. See Policy for an explanation of the policy ‘etag` property and how to modify policies.

You can also update the policy by passing a block to #policy, which will call this method internally after the block completes.

Examples:

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub
topic = pubsub.topic "my-topic"

policy = topic.policy # API call

policy.add "roles/owner", "user:[email protected]"

topic.policy = policy # API call

Parameters:

  • new_policy (Policy)

    a new or modified Cloud IAM Policy for this topic

See Also:



389
390
391
392
393
# File 'lib/gcloud/pubsub/topic.rb', line 389

def policy= new_policy
  ensure_service!
  grpc = service.set_topic_policy name, new_policy.to_grpc
  @policy = Policy.from_grpc grpc
end

#publish(data = nil, attributes = {}) {|publisher| ... } ⇒ Message+

Publishes one or more messages to the topic.

Examples:

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub

topic = pubsub.topic "my-topic"
msg = topic.publish "new-message"

A message can be published using a File object:

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub

topic = pubsub.topic "my-topic"
msg = topic.publish File.open("message.txt")

Additionally, a message can be published with attributes:

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub

topic = pubsub.topic "my-topic"
msg = topic.publish "new-message",
                    foo: :bar,
                    this: :that

Multiple messages can be sent at the same time using a block:

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub

topic = pubsub.topic "my-topic"
msgs = topic.publish do |t|
  t.publish "new-message-1", foo: :bar
  t.publish "new-message-2", foo: :baz
  t.publish "new-message-3", foo: :bif
end

Parameters:

  • data (String, File) (defaults to: nil)

    The message data.

  • attributes (Hash) (defaults to: {})

    Optional attributes for the message.

Yields:

  • (publisher)

    a block for publishing multiple messages in one request

Yield Parameters:

Returns:

  • (Message, Array<Message>)

    Returns the published message when called without a block, or an array of messages when called with a block.



290
291
292
293
294
295
296
# File 'lib/gcloud/pubsub/topic.rb', line 290

def publish data = nil, attributes = {}
  ensure_service!
  publisher = Publisher.new data, attributes
  yield publisher if block_given?
  return nil if publisher.messages.count.zero?
  publish_batch_messages publisher
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.

Examples:

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub

topic = pubsub.topic "my-topic"
sub = topic.subscribe "my-topic-sub"
puts sub.name # => "my-topic-sub"

The name is optional, and will be generated if not given:

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub

topic = pubsub.topic "my-topic"
sub = topic.subscribe "my-topic-sub"
puts sub.name # => "generated-sub-name"

Wait 2 minutes for acknowledgement and push all to an endpoint:

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub

topic = pubsub.topic "my-topic"
sub = topic.subscribe "my-topic-sub",
                      deadline: 120,
                      endpoint: "https://example.com/push"

Parameters:

  • subscription_name (String)

    Name of the new subscription. Must start with a letter, and contain only letters ([A-Za-z]), numbers ([0-9], dashes (-), underscores (_), periods (.), tildes (~), plus (+) or percent signs (%). It must be between 3 and 255 characters in length, and it must not start with “goog”.

  • deadline (Integer) (defaults to: nil)

    The maximum number of seconds after a subscriber receives a message before the subscriber should acknowledge the message.

  • endpoint (String) (defaults to: nil)

    A URL locating the endpoint to which messages should be pushed.

Returns:



140
141
142
143
144
145
# File 'lib/gcloud/pubsub/topic.rb', line 140

def subscribe subscription_name, deadline: nil, endpoint: nil
  ensure_service!
  options = { deadline: deadline, endpoint: endpoint }
  grpc = service.create_subscription name, subscription_name, options
  Subscription.from_grpc grpc, service
end

#subscription(subscription_name, skip_lookup: nil) ⇒ Gcloud::Pubsub::Subscription? Also known as: get_subscription, find_subscription

Retrieves subscription by name.

Examples:

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub

topic = pubsub.topic "my-topic"
subscription = topic.subscription "my-topic-subscription"
puts subscription.name

Skip the lookup against the service with ‘skip_lookup`:

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub

# No API call is made to retrieve the subscription information.
subscription = pubsub.subscription "my-sub", skip_lookup: true
puts subscription.name

Parameters:

  • subscription_name (String)

    Name of a subscription.

  • skip_lookup (Boolean) (defaults to: nil)

    Optionally create a Subscription object without verifying the subscription resource exists on the Pub/Sub service. Calls made on this object will raise errors if the service resource does not exist. Default is ‘false`.

Returns:



181
182
183
184
185
186
187
188
# File 'lib/gcloud/pubsub/topic.rb', line 181

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 Gcloud::NotFoundError
  nil
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.

Examples:

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub

topic = pubsub.topic "my-topic"
subscription = topic.subscriptions
subscriptions.each do |subscription|
  puts subscription.name
end

Retrieve all subscriptions: (See Subscription::List#all)

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub

topic = pubsub.topic "my-topic"
subscription = topic.subscriptions
subscriptions.all do |subscription|
  puts subscription.name
end

Parameters:

  • token (String) (defaults to: nil)

    The ‘token` value returned by the last call to `subscriptions`; indicates that this is a continuation of a call, and that the system should return the next page of data.

  • max (Integer) (defaults to: nil)

    Maximum number of subscriptions to return.

Returns:



226
227
228
229
230
231
# File 'lib/gcloud/pubsub/topic.rb', line 226

def subscriptions token: nil, max: nil
  ensure_service!
  options = { token: token, max: max }
  grpc = service.list_topics_subscriptions name, options
  Subscription::List.from_topic_grpc grpc, service, name, max
end

#test_permissions(*permissions) ⇒ Array<Strings>

Tests the specified permissions against the [Cloud IAM](cloud.google.com/iam/) access control policy.

Examples:

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub
topic = pubsub.topic "my-topic"
perms = topic.test_permissions "pubsub.topics.get",
                               "pubsub.topics.publish"
perms.include? "pubsub.topics.get" #=> true
perms.include? "pubsub.topics.publish" #=> false

Parameters:

  • permissions (String, Array<String>)

    The set of permissions to check access for. Permissions with wildcards (such as ‘*` or `storage.*`) are not allowed.

    The permissions that can be checked on a topic are:

    • pubsub.topics.publish

    • pubsub.topics.attachSubscription

    • pubsub.topics.get

    • pubsub.topics.delete

    • pubsub.topics.update

    • pubsub.topics.getIamPolicy

    • pubsub.topics.setIamPolicy

Returns:

  • (Array<Strings>)

    The permissions that have access.

See Also:



429
430
431
432
433
434
435
# File 'lib/gcloud/pubsub/topic.rb', line 429

def test_permissions *permissions
  permissions = Array(permissions).flatten
  permissions = Array(permissions).flatten
  ensure_service!
  grpc = service.test_topic_permissions name, permissions
  grpc.permissions
end