Module: SQS::Transport::ClassMethods

Defined in:
lib/sqs/transport.rb

Instance Method Summary collapse

Instance Method Details

#attrs_for_sqs_messageObject

Returns the list of attrs to be sent to SQS; raises an exception if attrs have not yet been defined.



83
84
85
86
# File 'lib/sqs/transport.rb', line 83

def attrs_for_sqs_message
  raise "Message requires at least one attribute. Call include_in_sqs_message to define which attributes are included in SQS message" if @attrs_for_sqs_message.nil? || @attrs_for_sqs_message.empty?
  @attrs_for_sqs_message
end

#include_in_sqs_message(*attrs_to_include) ⇒ Object

Defines the attributes to be included in the message sent to SQS. Not necessary if to_sqs_message is overridden.



78
79
80
# File 'lib/sqs/transport.rb', line 78

def include_in_sqs_message *attrs_to_include
  @attrs_for_sqs_message = attrs_to_include
end

#peek(number_to_view = 1, opts = {}) ⇒ Object

Retrives one or more messages from the queue, but sets their visiblity to zero so that they can immediately be picked up by another worker. Useful for previewing messages on the queue.

  • queue_name is the name of the SQS queue

  • number_to_view sets the maximum number of messages to return. If set to 1, a single instance is returned; if > 1 an array is returned. Defaults to 1.

No options at this time.



115
116
117
# File 'lib/sqs/transport.rb', line 115

def peek number_to_view=1, opts={}
  self.receive(number_to_view, opts.merge(:visibility => 0))
end

#receive(number_to_receive = 1, opts = {}) ⇒ Object

Pops one or more messages off the SQS queue, parses the contents, and returns them as instances of the class.

* <tt>number_to_recieve</tt> is optional.  If not passed, a single message will be returned.  If number_to_receive == 1, a single instance will be returned; otherwise an array will be returned.

Valid options:

  • :visiblity - sets the visiblity timeout on the message in seconds



93
94
95
96
97
98
99
100
101
# File 'lib/sqs/transport.rb', line 93

def receive number_to_receive=1, opts={}
  result = SQS::Message.receive(self.sqs_queue_name, number_to_receive, opts)

  if number_to_receive == 1
    reconstitute_instance(result)
  else
    result.map {|m| reconstitute_instance(m)}
  end
end

#reconstitute_instance(sqs_message) ⇒ Object



103
104
105
106
107
108
# File 'lib/sqs/transport.rb', line 103

def reconstitute_instance sqs_message
  new_instance = self.new
  sqs_message.contents.each { |field, value| new_instance.send("#{field}=", value) }
  new_instance.sqs_message = sqs_message
  return new_instance
end

#set_queue_name(name) ⇒ Object

Defines associated SQS queue



67
68
69
# File 'lib/sqs/transport.rb', line 67

def set_queue_name name
  @sqs_queue_name = name.to_s
end

#sqs_queue_nameObject

Returns name of associated SQS Queue; raises exception if name has not yet been defined



72
73
74
75
# File 'lib/sqs/transport.rb', line 72

def sqs_queue_name
  raise "undefined queue name!" if @sqs_queue_name.nil?
  @sqs_queue_name
end