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.



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

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



4
5
6
# File 'lib/iron_mq/messages.rb', line 4

def client
  @client
end

Instance Method Details

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



59
60
61
62
63
# File 'lib/iron_mq/messages.rb', line 59

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


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

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



10
11
12
# File 'lib/iron_mq/messages.rb', line 10

def path(options={})
  path = "projects/#{@client.project_id}/queues/#{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.


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

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



65
66
67
68
69
# File 'lib/iron_mq/messages.rb', line 65

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