Class: SimpleQS::Message

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

Defined Under Namespace

Classes: DoubleSendError, NotReceivedError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queue, params = nil) ⇒ Message

Returns a new instance of Message.



11
12
13
14
15
16
17
# File 'lib/simple_qs/message.rb', line 11

def initialize(queue, params = nil)
  @queue = queue

  @body = params if params.class == String
  _from_responce(params) if params.class == SimpleQS::Responce
  _from_hash(params) if params.class == Hash
end

Instance Attribute Details

#approximate_first_receive_timestampObject (readonly)

Returns the value of attribute approximate_first_receive_timestamp.



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

def approximate_first_receive_timestamp
  @approximate_first_receive_timestamp
end

#approximate_receive_countObject (readonly)

Returns the value of attribute approximate_receive_count.



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

def approximate_receive_count
  @approximate_receive_count
end

#bodyObject (readonly)

Returns the value of attribute body.



8
9
10
# File 'lib/simple_qs/message.rb', line 8

def body
  @body
end

#md5_of_bodyObject (readonly)

Returns the value of attribute md5_of_body.



8
9
10
# File 'lib/simple_qs/message.rb', line 8

def md5_of_body
  @md5_of_body
end

#message_idObject (readonly)

Returns the value of attribute message_id.



8
9
10
# File 'lib/simple_qs/message.rb', line 8

def message_id
  @message_id
end

#queueObject

Returns the value of attribute queue.



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

def queue
  @queue
end

#receipt_handleObject (readonly)

Returns the value of attribute receipt_handle.



8
9
10
# File 'lib/simple_qs/message.rb', line 8

def receipt_handle
  @receipt_handle
end

#sender_idObject (readonly)

Returns the value of attribute sender_id.



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

def sender_id
  @sender_id
end

#sent_timestampObject (readonly)

Returns the value of attribute sent_timestamp.



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

def sent_timestamp
  @sent_timestamp
end

Class Method Details

.receive(queue, attributes = nil, max_number_of_messages = nil, visibility_timeout = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/simple_qs/message.rb', line 80

def receive(queue, attributes = nil, max_number_of_messages = nil, visibility_timeout = nil)

  SimpleQS::Queue.check_visibility_timeout(visibility_timeout) if visibility_timeout
  if max_number_of_messages && !(1..10).include?(max_number_of_messages)
    raise ArgumentError, "Maximum number of messages should be in 1..10 range"
  end

  params = {
    'Action'    => 'ReceiveMessage'
  }

  if attributes
    attributes = [attributes] unless attributes.class == Array
    attributes.uniq!
    unless (attributes - [:All, :SenderId, :SentTimestamp, :ApproximateReceiveCount, :ApproximateFirstReceiveTimestamp]).empty?
      raise ArgumentError,\
        "Allowed attributes: :All, :SenderId, :SentTimestamp, :ApproximateReceiveCount, :ApproximateFirstReceiveTimestamp"
    end
    attributes.each_index do |i|
      params["AttributeName.#{i + 1}"] = attributes[i]
    end
  end
  params['MaxNumberOfMessages'] = max_number_of_messages if max_number_of_messages
  params['VisibilityTimeout'] = visibility_timeout if visibility_timeout

  request = queue.build_request(:get, params)
  responce = request.perform
  if responce.respond_to?(:message)
    messages = (responce.message.class == Array ? responce.message : [responce.message])
    messages.map {|message| new(queue, message)}
  else
    []
  end
end

.send(queue, message_body) ⇒ Object



76
77
78
# File 'lib/simple_qs/message.rb', line 76

def send(queue, message_body)
  new(queue, message_body).send
end

Instance Method Details

#==(other) ⇒ Object



71
72
73
# File 'lib/simple_qs/message.rb', line 71

def ==(other)
  message_id == other.message_id && receipt_handle == other.receipt_handle
end

#change_visibility(visibility_timeout) ⇒ Object

Raises:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/simple_qs/message.rb', line 51

def change_visibility(visibility_timeout)
  SimpleQS::Queue.check_visibility_timeout(visibility_timeout)
  raise NotReceivedError, "Cann't change visibility timeout for message that was not received" unless receipt_handle

  params = {
    'Action'            => 'ChangeMessageVisibility',
    'ReceiptHandle'      => receipt_handle,
    'VisibilityTimeout'  => visibility_timeout
  }

  request = queue.build_request(:get, params)
  responce = request.perform
  
  raise responce.to_error unless responce.successful?
end

#deleteObject Also known as: destroy

Raises:



39
40
41
42
43
44
45
46
47
48
# File 'lib/simple_qs/message.rb', line 39

def delete
  raise NotReceivedError, "Cann't delete message that was not received" unless receipt_handle
  params = {
    'Action'        => 'DeleteMessage',
    'ReceiptHandle'  => receipt_handle
  }
  request = queue.build_request(:get, params)
  responce = request.perform
  raise responce.to_error unless responce.successful?
end

#dupObject



67
68
69
# File 'lib/simple_qs/message.rb', line 67

def dup
  self.class.new(queue, body)
end

#resendObject



35
36
37
# File 'lib/simple_qs/message.rb', line 35

def resend
  dup.send
end

#sendObject

Raises:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/simple_qs/message.rb', line 19

def send
  raise DoubleSendError, "Cann't send already sent message. Use resend() method." if message_id
  raise DoubleSendError, "Cann't send received message. Use resend() method." if receipt_handle

  params = {
    'Action'      => 'SendMessage',
    'MessageBody'  => body
  }
  request = queue.build_request(:post, params)
  responce = request.perform
  raise responce.to_error unless responce.successful?
  _from_responce(responce)

  self
end