Class: Google::Cloud::Pubsub::Project
- Inherits:
-
Object
- Object
- Google::Cloud::Pubsub::Project
- Defined in:
- lib/google/cloud/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.
Instance Method Summary collapse
-
#create_topic(topic_name) ⇒ Google::Cloud::Pubsub::Topic
(also: #new_topic)
Creates a new topic.
-
#project ⇒ Object
The Pub/Sub project connected to.
-
#publish(topic_name, data = nil, attributes = {}) {|publisher| ... } ⇒ Message+
Publishes one or more messages to the given topic.
-
#snapshots(token: nil, max: nil) ⇒ Array<Google::Cloud::Pubsub::Snapshot>
(also: #find_snapshots, #list_snapshots)
Retrieves a list of snapshots for the given project.
-
#subscribe(topic_name, subscription_name, deadline: nil, retain_acked: false, retention: nil, endpoint: nil, autocreate: nil) ⇒ Google::Cloud::Pubsub::Subscription
(also: #create_subscription, #new_subscription)
Creates a new Subscription object for the provided topic.
-
#subscription(subscription_name, project: nil, skip_lookup: nil) ⇒ Google::Cloud::Pubsub::Subscription?
(also: #get_subscription, #find_subscription)
Retrieves subscription by name.
-
#subscriptions(token: nil, max: nil) ⇒ Array<Google::Cloud::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) ⇒ Google::Cloud::Pubsub::Topic?
(also: #get_topic, #find_topic)
Retrieves topic by name.
-
#topics(token: nil, max: nil) ⇒ Array<Google::Cloud::Pubsub::Topic>
(also: #find_topics, #list_topics)
Retrieves a list of topics for the given project.
Instance Method Details
#create_topic(topic_name) ⇒ Google::Cloud::Pubsub::Topic Also known as: new_topic
Creates a new topic.
162 163 164 165 166 |
# File 'lib/google/cloud/pubsub/project.rb', line 162 def create_topic topic_name ensure_service! grpc = service.create_topic topic_name Topic.from_grpc grpc, service end |
#project ⇒ Object
The Pub/Sub project connected to.
70 71 72 |
# File 'lib/google/cloud/pubsub/project.rb', line 70 def project service.project end |
#publish(topic_name, data = nil, attributes = {}) {|publisher| ... } ⇒ 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.
270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/google/cloud/pubsub/project.rb', line 270 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! publisher = Topic::Publisher.new data, attributes yield publisher if block_given? return nil if publisher..count.zero? topic_name, publisher, autocreate end |
#snapshots(token: nil, max: nil) ⇒ Array<Google::Cloud::Pubsub::Snapshot> Also known as: find_snapshots, list_snapshots
Retrieves a list of snapshots for the given project.
480 481 482 483 484 485 |
# File 'lib/google/cloud/pubsub/project.rb', line 480 def snapshots token: nil, max: nil ensure_service! = { token: token, max: max } grpc = service.list_snapshots Snapshot::List.from_grpc grpc, service, max end |
#subscribe(topic_name, subscription_name, deadline: nil, retain_acked: false, retention: nil, endpoint: nil, autocreate: nil) ⇒ Google::Cloud::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.
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
# File 'lib/google/cloud/pubsub/project.rb', line 342 def subscribe topic_name, subscription_name, deadline: nil, retain_acked: false, retention: nil, endpoint: nil, autocreate: nil ensure_service! = { deadline: deadline, retain_acked: retain_acked, retention: retention, endpoint: endpoint } grpc = service.create_subscription topic_name, subscription_name, Subscription.from_grpc grpc, service rescue Google::Cloud::NotFoundError => e if autocreate create_topic topic_name return subscribe(topic_name, subscription_name, deadline: deadline, retain_acked: retain_acked, retention: retention, endpoint: endpoint, autocreate: false) end raise e end |
#subscription(subscription_name, project: nil, skip_lookup: nil) ⇒ Google::Cloud::Pubsub::Subscription? Also known as: get_subscription, find_subscription
Retrieves subscription by name.
396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/google/cloud/pubsub/project.rb', line 396 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 Google::Cloud::NotFoundError nil end |
#subscriptions(token: nil, max: nil) ⇒ Array<Google::Cloud::Pubsub::Subscription> Also known as: find_subscriptions, list_subscriptions
Retrieves a list of subscriptions for the given project.
440 441 442 443 444 445 |
# File 'lib/google/cloud/pubsub/project.rb', line 440 def subscriptions token: nil, max: nil ensure_service! = { token: token, max: max } grpc = service.list_subscriptions Subscription::List.from_grpc grpc, service, max end |
#topic(topic_name, autocreate: nil, project: nil, skip_lookup: nil) ⇒ Google::Cloud::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.
136 137 138 139 140 141 142 143 144 145 |
# File 'lib/google/cloud/pubsub/project.rb', line 136 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 Google::Cloud::NotFoundError return create_topic(topic_name) if autocreate nil end |
#topics(token: nil, max: nil) ⇒ Array<Google::Cloud::Pubsub::Topic> Also known as: find_topics, list_topics
Retrieves a list of topics for the given project.
200 201 202 203 204 205 |
# File 'lib/google/cloud/pubsub/project.rb', line 200 def topics token: nil, max: nil ensure_service! = { token: token, max: max } grpc = service.list_topics Topic::List.from_grpc grpc, service, max end |