Class: Gcloud::Pubsub::Project
- Inherits:
-
Object
- Object
- Gcloud::Pubsub::Project
- Defined in:
- lib/gcloud/pubsub/project.rb
Overview
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
Instance Attribute Summary collapse
-
#connection ⇒ Object
The Connection object.
Class Method Summary collapse
-
.default_project ⇒ Object
Default project.
Instance Method Summary collapse
-
#create_topic(topic_name) ⇒ Object
(also: #new_topic)
Creates a new topic.
-
#get_subscription(subscription_name) ⇒ Object
(also: #find_subscription)
Retrieves subscription by name.
-
#get_topic(topic_name) ⇒ Object
(also: #find_topic)
Retrieves topic by name.
-
#initialize(project, credentials) ⇒ Project
constructor
Creates a new Connection instance.
-
#project ⇒ Object
The Pub/Sub project connected to.
-
#subscription(subscription_name) ⇒ Object
Retrieves subscription by name.
-
#subscriptions(options = {}) ⇒ Object
(also: #find_subscriptions, #list_subscriptions)
Retrieves a list of subscriptions for the given project.
-
#topic(topic_name, options = {}) ⇒ Object
Retrieves topic by name.
-
#topics(options = {}) ⇒ Object
(also: #find_topics, #list_topics)
Retrieves a list of topics for the given project.
Constructor Details
#initialize(project, credentials) ⇒ Project
Creates a new Connection instance.
49 50 51 |
# File 'lib/gcloud/pubsub/project.rb', line 49 def initialize project, credentials #:nodoc: @connection = Connection.new project, credentials end |
Instance Attribute Details
#connection ⇒ Object
The Connection object.
45 46 47 |
# File 'lib/gcloud/pubsub/project.rb', line 45 def connection @connection end |
Class Method Details
.default_project ⇒ Object
Default project.
71 72 73 |
# File 'lib/gcloud/pubsub/project.rb', line 71 def self.default_project #:nodoc: ENV["PUBSUB_PROJECT"] || ENV["GOOGLE_CLOUD_PROJECT"] end |
Instance Method Details
#create_topic(topic_name) ⇒ Object Also known as: new_topic
193 194 195 196 197 198 199 200 201 |
# File 'lib/gcloud/pubsub/project.rb', line 193 def create_topic topic_name ensure_connection! resp = connection.create_topic topic_name if resp.success? Topic.from_gapi resp.data, connection else fail ApiError.from_response(resp) end end |
#get_subscription(subscription_name) ⇒ Object Also known as: find_subscription
Retrieves subscription by name. The difference between this method and Project#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
323 324 325 326 327 328 329 330 331 |
# File 'lib/gcloud/pubsub/project.rb', line 323 def get_subscription subscription_name ensure_connection! resp = connection.get_subscription subscription_name if resp.success? Subscription.from_gapi resp.data, connection else nil end end |
#get_topic(topic_name) ⇒ Object Also known as: find_topic
Retrieves topic by name. The difference between this method and Project#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"
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/gcloud/pubsub/project.rb', line 97 def get_topic topic_name ensure_connection! resp = connection.get_topic topic_name if resp.success? Topic.from_gapi resp.data, connection else return nil if resp.data["error"]["code"] == 404 fail ApiError.from_response(resp) end end |
#project ⇒ Object
65 66 67 |
# File 'lib/gcloud/pubsub/project.rb', line 65 def project connection.project end |
#subscription(subscription_name) ⇒ Object
Retrieves subscription by name. The difference between this method and Project#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
292 293 294 295 296 |
# File 'lib/gcloud/pubsub/project.rb', line 292 def subscription subscription_name ensure_connection! Subscription.new_lazy subscription_name, connection end |
#subscriptions(options = {}) ⇒ Object Also known as: find_subscriptions, list_subscriptions
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 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
387 388 389 390 391 392 393 394 395 |
# File 'lib/gcloud/pubsub/project.rb', line 387 def subscriptions = {} ensure_connection! resp = connection.list_subscriptions if resp.success? Subscription::List.from_resp resp, connection else fail ApiError.from_response(resp) end end |
#topic(topic_name, options = {}) ⇒ Object
Retrieves topic by name. The difference between this method and Project#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"
167 168 169 170 171 |
# File 'lib/gcloud/pubsub/project.rb', line 167 def topic topic_name, = {} ensure_connection! Topic.new_lazy topic_name, connection, end |
#topics(options = {}) ⇒ Object Also known as: find_topics, list_topics
Retrieves a list of topics for the given project.
Parameters
options-
An optional Hash for controlling additional behavior. (
Hash) (String) options[:token]-
The
tokenvalue returned by the last call totopics; 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 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
255 256 257 258 259 260 261 262 263 |
# File 'lib/gcloud/pubsub/project.rb', line 255 def topics = {} ensure_connection! resp = connection.list_topics if resp.success? Topic::List.from_resp resp, connection else fail ApiError.from_response(resp) end end |