Class: IronMQ::Messages

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, queue = nil) ⇒ Messages

Returns a new instance of Messages.



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

def initialize(client, queue=nil)
  @client = client
  @queue = queue
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#queueObject (readonly)

Returns the value of attribute queue.



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

def queue
  @queue
end

Class Method Details

.path(options) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/iron_mq/messages.rb', line 20

def self.path(options)
  path = "#{Queues.path(options)}/messages"
  if options[:msg_id]
    path << "/#{options[:msg_id]}"
  end
  path
end

Instance Method Details

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



81
82
83
84
85
# File 'lib/iron_mq/messages.rb', line 81

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


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/iron_mq/messages.rb', line 31

def get(options={})
  if options.is_a?(String)
    # assume it's an id
    puts 'get_by_id'
    return Message.new(self, {'id'=>options}, options)
    #r = @client.get(path(:msg_id=>options))
    #p r
    #return r
  end
  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



14
15
16
17
18
# File 'lib/iron_mq/messages.rb', line 14

def path(options={})
  options[:queue_name] ||= ((@queue ? @queue.name : nil) || @client.queue_name)
  options[:project_id] = @client.project_id
  Messages.path(options)
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.


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/iron_mq/messages.rb', line 61

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



87
88
89
90
91
# File 'lib/iron_mq/messages.rb', line 87

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