Class: Bisques::Message

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

Overview

A message received from an SQS queue.

Constant Summary collapse

InvalidObjectError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queue, id, handle, body, attributes = {}) ⇒ Message

Returns a new instance of Message.

Parameters:

  • queue (Queue)
  • id (String)
  • handle (String)
  • body (String)
  • attributes (Hash) (defaults to: {})


25
26
27
# File 'lib/bisques/message.rb', line 25

def initialize(queue, id, handle, body, attributes = {}) #:nodoc:
  @queue, @id, @handle, @body, @attributes = queue, id, handle, body, attributes
end

Instance Attribute Details

#attributes ⇒ Hash (readonly)

Returns Hash of SQS attributes.

Returns:

  • (Hash) —

    Hash of SQS attributes.



15
16
17
# File 'lib/bisques/message.rb', line 15

def attributes
  @attributes
end

#body ⇒ String (readonly)

Returns The raw text body of the message.

Returns:

  • (String) —

    The raw text body of the message.



13
14
15
# File 'lib/bisques/message.rb', line 13

def body
  @body
end

#handle ⇒ String (readonly)

Returns A unique handle used to manipulate the message.

Returns:

  • (String) —

    A unique handle used to manipulate the message.



11
12
13
# File 'lib/bisques/message.rb', line 11

def handle
  @handle
end

#id ⇒ String (readonly)

Returns The AWS Id of the message.

Returns:

  • (String) —

    The AWS Id of the message.



9
10
11
# File 'lib/bisques/message.rb', line 9

def id
  @id
end

#queue ⇒ Queue (readonly)

Returns The queue this message originated from.

Returns:

  • (Queue) —

    The queue this message originated from.



7
8
9
# File 'lib/bisques/message.rb', line 7

def queue
  @queue
end

Instance Method Details

#delete ⇒ Boolean

Delete the message from the queue. This should be called after the message has been received and processed. If not then after a timeout the message will get added back to the queue.

Returns:

  • (Boolean) —

    true if the message was deleted.

Raises:



48
49
50
# File 'lib/bisques/message.rb', line 48

def delete
  queue.delete_message(handle)
end

#object ⇒ Object

The deserialized object in the message. This method is used to retrieve the contents that Queue#post_message placed there.

Examples:


queue.post_message([1,2,3])
queue.retrieve.object == [1,2,3]


37
38
39
40
41
# File 'lib/bisques/message.rb', line 37

def object
  @object ||= JSON.parse(body)
rescue => error
  raise InvalidObjectError.new(error.to_s)
end

#return ⇒ AwsResponse

Return the message to the queue immediately. If a client has taken a message and cannot process it for any reason it can put the message back faster than the default timeout by calling this method.

Returns:

Raises:



57
58
59
# File 'lib/bisques/message.rb', line 57

def return
  queue.return_message(handle)
end