Class: Gcloud::Pubsub::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/gcloud/pubsub/message.rb

Overview

Message

Represents a Pub/Sub Message.

Message objects are created by Topic#publish. Subscription#pull returns an array of ReceivedMessage objects, each of which contains a Message object. Each ReceivedMessage object can be acknowledged and/or delayed.

Examples:

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub

# Publish a message
topic = pubsub.topic "my-topic"
message = topic.publish "new-message"
puts message.data #=>  "new-message"

# Pull a message
sub = pubsub.subscription "my-topic-sub"
received_message = sub.pull.first
puts received_message.message.data #=>  "new-message"

Instance Method Summary collapse

Constructor Details

#initialize(data = nil, attributes = {}) ⇒ Message

Create an empty Message object. This can be used to publish several messages in bulk.


54
55
56
57
58
# File 'lib/gcloud/pubsub/message.rb', line 54

def initialize data = nil, attributes = {}
  @gapi               = {}
  @gapi["data"]       = data
  @gapi["attributes"] = attributes
end

Instance Method Details

#attributesObject

The received attributes.


68
69
70
71
72
# File 'lib/gcloud/pubsub/message.rb', line 68

def attributes
  attrs = @gapi["attributes"]
  attrs = attrs.to_hash if attrs.respond_to? :to_hash
  attrs
end

#dataObject

The received data.


62
63
64
# File 'lib/gcloud/pubsub/message.rb', line 62

def data
  @gapi["data"]
end

#message_idObject Also known as: msg_id

The ID of this message, assigned by the server at publication time. Guaranteed to be unique within the topic.


77
78
79
# File 'lib/gcloud/pubsub/message.rb', line 77

def message_id
  @gapi["messageId"]
end