Sha256: 298a6d6655f1b5c2c221afb5e55a7d10162faeb173aa2e733a528b88bed59e7b

Contents?: true

Size: 1.8 KB

Versions: 3

Compression:

Stored size: 1.8 KB

Contents

# Copyright 2015 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require 'googleauth'
require 'google/apis/pubsub_v1beta2'

Pubsub = Google::Apis::PubsubV1beta2

pubsub = Pubsub::PubsubService.new
pubsub.authorization = Google::Auth.get_application_default([Pubsub::AUTH_PUBSUB])

project = ARGV[0] || 'YOUR_PROJECT_NAME'

topic = "projects/#{project}/topics/foo"
subscription = "projects/#{project}/subscriptions/bar"

# Create topic & subscription
pubsub.create_topic(topic)
pubsub.create_subscription(subscription, Pubsub::Subscription.new(topic: topic))

# Publish messages
request = Pubsub::PublishRequest.new(messages: [])
request.messages << Pubsub::Message.new(attributes: { "language" => "en" }, data: 'Hello')
request.messages << Pubsub::Message.new(attributes: { "language" => "en" }, data: 'World')
pubsub.publish(topic, request)

# Pull messages
response = pubsub.pull(subscription, Pubsub::PullRequest.new(max_messages: 5))
response.received_messages.each do |received_message|
  data = received_message.message.data
  puts "Received #{data}"
end

# Acknowledge receipt
ack_ids = response.received_messages.map{ |msg| msg.ack_id }
pubsub.acknowledge(subscription, Pubsub::AcknowledgeRequest.new(ack_ids: ack_ids))

# Delete the subscription & topic
pubsub.delete_subscription(subscription)
pubsub.delete_topic(topic)

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
google-api-client-0.9.pre3 samples/pubsub/pubsub.rb
google-api-client-0.9.pre2 samples/pubsub/pubsub.rb
google-api-client-0.9.pre1 samples/pubsub/pubsub.rb