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.
See Gcloud#pubsub
Instance Attribute Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
-
#create_topic(topic_name) ⇒ Gcloud::Pubsub::Topic
(also: #new_topic)
Creates a new topic.
-
#initialize(project, credentials) ⇒ Project
constructor
A new instance of Project.
-
#project ⇒ Object
The Pub/Sub project connected to.
-
#publish(topic_name, data = nil, attributes = {}) {|batch| ... } ⇒ Message+
Publishes one or more messages to the given topic.
-
#subscribe(topic_name, subscription_name, deadline: nil, endpoint: nil, autocreate: nil) ⇒ Gcloud::Pubsub::Subscription
(also: #create_subscription, #new_subscription)
Creates a new Subscription object for the provided topic.
-
#subscription(subscription_name, project: nil, skip_lookup: nil) ⇒ Gcloud::Pubsub::Subscription?
(also: #get_subscription, #find_subscription)
Retrieves subscription by name.
-
#subscriptions(token: nil, max: nil) ⇒ Array<Gcloud::Pubsub::Subscription>
(also: #find_subscriptions, #list_subscriptions)
Retrieves a list of subscriptions for the given project.
-
#topic(topic_name, autocreate: nil, project: nil, skip_lookup: nil) ⇒ Gcloud::Pubsub::Topic?
(also: #get_topic, #find_topic)
Retrieves topic by name.
-
#topics(token: nil, max: nil) ⇒ Array<Gcloud::Pubsub::Topic>
(also: #find_topics, #list_topics)
Retrieves a list of topics for the given project.
Constructor Details
#initialize(project, credentials) ⇒ Project
Returns a new instance of Project.
52 53 54 55 56 |
# File 'lib/gcloud/pubsub/project.rb', line 52 def initialize project, credentials project = project.to_s # Always cast to a string fail ArgumentError, "project is missing" if project.empty? @service = Service.new project, credentials end |
Instance Attribute Details
#service ⇒ Object
48 49 50 |
# File 'lib/gcloud/pubsub/project.rb', line 48 def service @service end |
Class Method Details
.default_project ⇒ Object
75 76 77 78 79 80 |
# File 'lib/gcloud/pubsub/project.rb', line 75 def self.default_project ENV["PUBSUB_PROJECT"] || ENV["GCLOUD_PROJECT"] || ENV["GOOGLE_CLOUD_PROJECT"] || Gcloud::GCE.project_id end |
Instance Method Details
#create_topic(topic_name) ⇒ Gcloud::Pubsub::Topic Also known as: new_topic
Creates a new topic.
169 170 171 172 173 174 175 |
# File 'lib/gcloud/pubsub/project.rb', line 169 def create_topic topic_name ensure_service! grpc = service.create_topic topic_name Topic.from_grpc grpc, service rescue GRPC::BadStatus => e raise Error.from_error(e) end |
#project ⇒ Object
The Pub/Sub project connected to.
69 70 71 |
# File 'lib/gcloud/pubsub/project.rb', line 69 def project service.project end |
#publish(topic_name, data = nil, attributes = {}) {|batch| ... } ⇒ Message+
Publishes one or more messages to the given topic. The topic will be created if the topic does previously not exist and the autocreate option is provided.
A note about auto-creating the topic: Any message published to a topic without a subscription will be lost.
287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/gcloud/pubsub/project.rb', line 287 def publish topic_name, data = nil, attributes = {} # Fix parameters if data.is_a?(::Hash) && attributes.empty? attributes = data data = nil end # extract autocreate option autocreate = attributes.delete :autocreate ensure_service! batch = Topic::Batch.new data, attributes yield batch if block_given? return nil if batch..count.zero? topic_name, batch, autocreate end |
#subscribe(topic_name, subscription_name, deadline: nil, endpoint: nil, autocreate: nil) ⇒ Gcloud::Pubsub::Subscription Also known as: create_subscription, new_subscription
Creates a new Subscription object for the provided topic. The topic will be created if the topic does previously not exist and the autocreate option is provided.
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/gcloud/pubsub/project.rb', line 359 def subscribe topic_name, subscription_name, deadline: nil, endpoint: nil, autocreate: nil ensure_service! = { deadline: deadline, endpoint: endpoint } grpc = service.create_subscription topic_name, subscription_name, Subscription.from_grpc grpc, service rescue GRPC::BadStatus => e if autocreate && e.code == 5 create_topic topic_name return subscribe(topic_name, subscription_name, deadline: deadline, endpoint: endpoint, autocreate: false) end raise Error.from_error(e) end |
#subscription(subscription_name, project: nil, skip_lookup: nil) ⇒ Gcloud::Pubsub::Subscription? Also known as: get_subscription, find_subscription
Retrieves subscription by name.
412 413 414 415 416 417 418 419 420 421 422 423 |
# File 'lib/gcloud/pubsub/project.rb', line 412 def subscription subscription_name, project: nil, skip_lookup: nil ensure_service! = { project: project } if skip_lookup return Subscription.new_lazy subscription_name, service, end 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<Gcloud::Pubsub::Subscription> Also known as: find_subscriptions, list_subscriptions
Retrieves a list of subscriptions for the given project.
459 460 461 462 463 464 465 466 |
# File 'lib/gcloud/pubsub/project.rb', line 459 def subscriptions token: nil, max: nil ensure_service! = { token: token, max: max } grpc = service.list_subscriptions Subscription::List.from_grpc grpc, service, max rescue GRPC::BadStatus => e raise Error.from_error(e) end |
#topic(topic_name, autocreate: nil, project: nil, skip_lookup: nil) ⇒ Gcloud::Pubsub::Topic? Also known as: get_topic, find_topic
Retrieves topic by name.
The topic will be created if the topic does not exist and the autocreate option is set to true.
139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/gcloud/pubsub/project.rb', line 139 def topic topic_name, autocreate: nil, project: nil, skip_lookup: nil ensure_service! = { project: project } return Topic.new_lazy(topic_name, service, ) if skip_lookup grpc = service.get_topic topic_name Topic.from_grpc grpc, service rescue GRPC::BadStatus => e if e.code == 5 return create_topic(topic_name) if autocreate return nil end raise Error.from_error(e) end |
#topics(token: nil, max: nil) ⇒ Array<Gcloud::Pubsub::Topic> Also known as: find_topics, list_topics
Retrieves a list of topics for the given project.
211 212 213 214 215 216 217 218 |
# File 'lib/gcloud/pubsub/project.rb', line 211 def topics token: nil, max: nil ensure_service! = { token: token, max: max } grpc = service.list_topics Topic::List.from_grpc grpc, service, max rescue GRPC::BadStatus => e raise Error.from_error(e) end |