Class: Gcloud::Pubsub::ReceivedMessage

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

Overview

ReceivedMessage

Represents a Pub/Sub Message that can be acknowledged or delayed.

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub

sub = pubsub.subscription "my-topic-sub"
received_message = sub.pull.first
if received_message
  puts received_message.message.data
  received_message.acknowledge!
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReceivedMessage

Create an empty Subscription object.



49
50
51
52
# File 'lib/gcloud/pubsub/received_message.rb', line 49

def initialize #:nodoc:
  @subscription = nil
  @gapi = {}
end

Instance Attribute Details

#gapiObject

The Google API Client object.



45
46
47
# File 'lib/gcloud/pubsub/received_message.rb', line 45

def gapi
  @gapi
end

#subscriptionObject

The Subscription object.



41
42
43
# File 'lib/gcloud/pubsub/received_message.rb', line 41

def subscription
  @subscription
end

Class Method Details

.from_gapi(gapi, subscription) ⇒ Object

New ReceivedMessage from a Google API Client object.



154
155
156
157
158
159
# File 'lib/gcloud/pubsub/received_message.rb', line 154

def self.from_gapi gapi, subscription #:nodoc:
  new.tap do |f|
    f.gapi         = gapi
    f.subscription = subscription
  end
end

Instance Method Details

#ack_idObject

The acknowledgment ID for the message.



56
57
58
# File 'lib/gcloud/pubsub/received_message.rb', line 56

def ack_id
  @gapi["ackId"]
end

#acknowledge!Object Also known as: ack!

Acknowledges receipt of the message.

Example

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub

sub = pubsub.subscription "my-topic-sub"
received_message = sub.pull.first
if received_message
  puts received_message.message.data
  received_message.acknowledge!
end


104
105
106
107
# File 'lib/gcloud/pubsub/received_message.rb', line 104

def acknowledge!
  ensure_subscription!
  subscription.acknowledge ack_id
end

#attributesObject

The received message’s attributes.



75
76
77
# File 'lib/gcloud/pubsub/received_message.rb', line 75

def attributes
  message.attributes
end

#dataObject

The received message’s data.



69
70
71
# File 'lib/gcloud/pubsub/received_message.rb', line 69

def data
  message.data
end

#delay!(new_deadline) ⇒ Object

Modifies the acknowledge deadline for the message.

This indicates that more time is needed to process the message, or to make the message available for redelivery.

Parameters

deadline

The new ack deadline in seconds from the time this request is sent to the Pub/Sub system. Must be >= 0. For example, if the value is 10, the new ack deadline will expire 10 seconds after the call is made. Specifying 0 may immediately make the message available for another pull request. (Integer)

Example

require "gcloud"

gcloud = Gcloud.new
pubsub = gcloud.pubsub

sub = pubsub.subscription "my-topic-sub"
received_message = sub.pull.first
if received_message
  puts received_message.message.data
  # Delay for 2 minutes
  received_message.delay! 120
end


140
141
142
143
144
145
146
147
148
149
150
# File 'lib/gcloud/pubsub/received_message.rb', line 140

def delay! new_deadline
  ensure_subscription!
  connection = subscription.connection
  resp = connection.modify_ack_deadline subscription.name,
                                        ack_id, new_deadline
  if resp.success?
    true
  else
    fail ApiError.from_response(resp)
  end
end

#messageObject Also known as: msg

The received message.



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

def message
  Message.from_gapi @gapi["message"]
end

#message_idObject Also known as: msg_id

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



82
83
84
# File 'lib/gcloud/pubsub/received_message.rb', line 82

def message_id
  message.message_id
end