Class: IronMQ::Messages

Inherits:
Object
  • Object
show all
Defined in:
lib/iron_mq/messages.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Messages

Returns a new instance of Messages.



8
9
10
# File 'lib/iron_mq/messages.rb', line 8

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



6
7
8
# File 'lib/iron_mq/messages.rb', line 6

def client
  @client
end

Instance Method Details

#delete(message_id, options = {}) ⇒ Object



61
62
63
64
65
# File 'lib/iron_mq/messages.rb', line 61

def delete(message_id, options={})
  path2 = "#{self.path(options)}/#{message_id}"
  res = @client.parse_response(@client.delete(path2))
  return ResponseBase.new(res)
end

#get(options = {}) ⇒ Object

options:

:queue_name => can specify an alternative queue name
:timeout => amount of time before message goes back on the queue


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

def get(options={})
  res = @client.parse_response(@client.get(path(options), options))
  ret = []
  res["messages"].each do |m|
    ret << Message.new(self, m, options)
  end
  if options[:n] || options['n']
    return ret
  else
    if ret.size > 0
      return ret[0]
    else
      return nil
    end
  end
end

#path(options = {}) ⇒ Object



12
13
14
# File 'lib/iron_mq/messages.rb', line 12

def path(options={})
  path = "projects/#{@client.project_id}/queues/#{CGI::escape(options[:queue_name] || options['queue_name'] || @client.queue_name)}/messages"
end

#post(payload, options = {}) ⇒ Object

options:

:queue_name => can specify an alternative queue name
:delay => time to wait before message will be available on the queue
:timeout => The time in seconds to wait after message is taken off the queue, before it is put back on. Delete before :timeout to ensure it does not go back on the queue.
:expires_in => After this time, message will be automatically removed from the queue.


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/iron_mq/messages.rb', line 41

def post(payload, options={})
  batch = false
  if payload.is_a?(Array)
    batch = true
    msgs = payload
  else
    options[:body] = payload
    msgs = []
    msgs << options
  end
  to_send = {}
  to_send[:messages] = msgs
  res = @client.parse_response(@client.post(path(options), to_send))
  if batch
    return res
  else
    return ResponseBase.new({"id"=>res["ids"][0], "msg"=>res["msg"]})
  end
end

#release(message_id, options = {}) ⇒ Object



67
68
69
70
71
# File 'lib/iron_mq/messages.rb', line 67

def release(message_id, options={})
  path2 = "#{self.path(options)}/#{message_id}/release"
  res = @client.parse_response(@client.post(path2, options))
  return ResponseBase.new(res)
end