Class: Opener::Daemons::SQS

Inherits:
Object
  • Object
show all
Defined in:
lib/opener/daemons/sqs.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ SQS

Returns a new instance of SQS.



12
13
14
15
16
17
18
19
20
21
# File 'lib/opener/daemons/sqs.rb', line 12

def initialize(name)
  @sqs = Aws::SQS::Client.new
  @name = name
  begin
    @url = sqs.get_queue_url(:queue_name=>name)[:queue_url]
  rescue Aws::SQS::Errors::NonExistentQueue => e
    sqs.create_queue(:queue_name=>name)
    retry
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/opener/daemons/sqs.rb', line 6

def name
  @name
end

#sqsObject (readonly)

Returns the value of attribute sqs.



6
7
8
# File 'lib/opener/daemons/sqs.rb', line 6

def sqs
  @sqs
end

#urlObject (readonly)

Returns the value of attribute url.



6
7
8
# File 'lib/opener/daemons/sqs.rb', line 6

def url
  @url
end

Class Method Details

.find(name) ⇒ Object



8
9
10
# File 'lib/opener/daemons/sqs.rb', line 8

def self.find(name)
  new(name)
end

Instance Method Details

#delete_message(handle) ⇒ Object



27
28
29
# File 'lib/opener/daemons/sqs.rb', line 27

def delete_message(handle)
  sqs.delete_message(:queue_url=>url, :receipt_handle=>handle)
end

#queue_urlObject



48
49
50
# File 'lib/opener/daemons/sqs.rb', line 48

def queue_url
  url
end

#receive_messages(limit) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/opener/daemons/sqs.rb', line 31

def receive_messages(limit)
  result = sqs.receive_message(:queue_url=>url,
                               :max_number_of_messages=>limit)[:messages] rescue []
                                      
  result ? to_hash(result) : []                       
                               
end

#send_message(message) ⇒ Object



23
24
25
# File 'lib/opener/daemons/sqs.rb', line 23

def send_message(message)
  sqs.send_message(:queue_url=>url, :message_body=>message)
end

#to_hash(messages) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/opener/daemons/sqs.rb', line 39

def to_hash(messages)
  messages.map do |m| 
    hash = m.to_hash
    json_body = JSON.parse(hash.delete(:body))
    hash[:body] = json_body["body"] ? json_body["body"] : json_body
    hash
  end
end