Gcloud::Pubsub::Project

Project

Represents the project that pubsub messages are pushed to and pulled from. Topic is a named resource to which messages are sent by publishers. Subscription is a named resource representing the stream of messages from a single, specific topic, to be delivered to the subscribing application. Message is a combination of data and attributes that a publisher sends to a topic and is eventually delivered to subscribers.

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub

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

See Gcloud#pubsub

Methods

Public Instance Methods

create_topic(topic_name)

Creates a new topic.

Parameters

topic_name

Name of a topic. (String)

Returns

Gcloud::Pubsub::Topic

Examples

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub
topic = pubsub.create_topic "my-topic"
Also aliased as: new_topic

find_subscription(subscription_name)

Alias for: get_subscription

find_subscriptions(options = {})

Alias for: subscriptions

find_topic(topic_name)

Alias for: get_topic

find_topics(options = {})

Alias for: topics

get_subscription(subscription_name)

Retrieves subscription by name. The difference between this method and #subscription is that this method makes an API call to Pub/Sub to verify the subscription exists.

Parameters

subscription_name

Name of a subscription. (String)

Returns

Gcloud::Pubsub::Subscription or nil if the subscription does not exist

Example

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub

subscription = pubsub.get_subscription "my-topic-subscription"
puts subscription.name
Also aliased as: find_subscription

get_topic(topic_name)

Retrieves topic by name. The difference between this method and #topic is that this method makes an API call to Pub/Sub to verify the topic exists.

Parameters

topic_name

Name of a topic. (String)

Returns

Gcloud::Pubsub::Topic or nil if topic does not exist

Example

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub
topic = pubsub.get_topic "my-topic"
Also aliased as: find_topic

list_subscriptions(options = {})

Alias for: subscriptions

list_topics(options = {})

Alias for: topics

new_topic(topic_name)

Alias for: create_topic

project()

The Pub/Sub project connected to.

Example

require "gcloud"

gcloud = Gcloud.new "my-todo-project",
                    "/path/to/keyfile.json"
pubsub = gcloud.pubsub

pubsub.project #=> "my-todo-project"

subscription(subscription_name)

Retrieves subscription by name. The difference between this method and #get_subscription is that this method does not make an API call to Pub/Sub to verify the subscription exists.

Parameters

subscription_name

Name of a subscription. (String)

Returns

Gcloud::Pubsub::Subscription

Example

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub

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

subscriptions(options = {})

Retrieves a list of subscriptions for the given project.

Parameters

options

An optional Hash for controlling additional behavior. (Hash)

options[:prefix]

Filter results to subscriptions whose names begin with this prefix. (String)

options[:token]

A previously-returned page token representing part of the larger set of results to view. (String)

options[:max]

Maximum number of subscriptions to return. (Integer)

Returns

Array of Gcloud::Pubsub::Subscription (Gcloud::Pubsub::Subscription::List)

Examples

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub

subscriptions = pubsub.subscriptions
subscriptions.each do |subscription|
  puts subscription.name
end

If you have a significant number of subscriptions, you may need to paginate through them: (See Gcloud::Pubsub::Subscription::List#token)

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub

all_subs = []
tmp_subs = pubsub.subscriptions
while tmp_subs.any? do
  tmp_subs.each do |subscription|
    all_subs << subscription
  end
  # break loop if no more subscriptions available
  break if tmp_subs.token.nil?
  # get the next group of subscriptions
  tmp_subs = pubsub.subscriptions token: tmp_subs.token
end

topic(topic_name, options = {})

Retrieves topic by name. The difference between this method and #get_topic is that this method does not make an API call to Pub/Sub to verify the topic exists.

Parameters

topic_name

Name of a topic. (String)

options

An optional Hash for controlling additional behavior. (Hash)

options[:autocreate]

Flag to control whether the topic should be created when needed. The default value is true. (Boolean)

options[:project]

If the topic belongs to a project other than the one currently connected to, the alternate project ID can be specified here. (String)

Returns

Gcloud::Pubsub::Topic

Examples

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub
topic = pubsub.topic "existing-topic"
msg = topic.publish "This is the first API call to Pub/Sub."

By default the topic will be created in Pub/Sub when needed.

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub
topic = pubsub.topic "non-existing-topic"
msg = topic.publish "This will create the topic in Pub/Sub."

Setting the autocomplete flag to false will not create the topic.

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub
topic = pubsub.topic "non-existing-topic"
msg = topic.publish "This raises." #=> Gcloud::Pubsub::NotFoundError

A topic in a different project can be created using the project flag.

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub
topic = pubsub.topic "another-topic", project: "another-project"

topics(options = {})

Retrieves a list of topics for the given project.

Parameters

options

An optional Hash for controlling additional behavior. (Hash) (String)

options[:token]

The token value returned by the last call to topics; indicates that this is a continuation of a call, and that the system should return the next page of data. (String)

options[:max]

Maximum number of topics to return. (Integer)

Returns

Array of Gcloud::Pubsub::Topic (Gcloud::Pubsub::Topic::List)

Examples

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub

topics = pubsub.topics
topics.each do |topic|
  puts topic.name
end

If you have a significant number of topics, you may need to paginate through them: (See Gcloud::Pubsub::Topic::List#token)

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub

all_topics = []
tmp_topics = pubsub.topics
while tmp_topics.any? do
  tmp_topics.each do |topic|
    all_topics << topic
  end
  # break loop if no more topics available
  break if tmp_topics.token.nil?
  # get the next group of topics
  tmp_topics = pubsub.topics token: tmp_topics.token
end
Also aliased as: find_topics, list_topics