Class: EventQ::Amazon::SQS

Inherits:
Object
  • Object
show all
Defined in:
lib/eventq/eventq_aws/sqs.rb

Overview

Helper SQS class to handle the API calls

Constant Summary collapse

@@queue_arns =
Concurrent::Hash.new
@@queue_urls =
Concurrent::Hash.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ SQS

Returns a new instance of SQS.



12
13
14
# File 'lib/eventq/eventq_aws/sqs.rb', line 12

def initialize(client)
  @sqs = client
end

Instance Attribute Details

#sqsObject (readonly)

Returns the value of attribute sqs.



10
11
12
# File 'lib/eventq/eventq_aws/sqs.rb', line 10

def sqs
  @sqs
end

Instance Method Details

#aws_safe_name(name) ⇒ Object



107
108
109
# File 'lib/eventq/eventq_aws/sqs.rb', line 107

def aws_safe_name(name)
  return name[0..79].gsub(/[^a-zA-Z\d_\-]/,'')
end

#create_queue(queue, attributes = {}) ⇒ Object

Create a new queue.



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

def create_queue(queue, attributes = {})
  _queue_name = EventQ.create_queue_name(queue)

  url = get_queue_url(queue)
  unless url
    response = sqs.create_queue(
      {
        queue_name: aws_safe_name(_queue_name),
        attributes: attributes
      }
    )
    url = response.queue_url
    @@queue_urls[_queue_name] = url
  end

  url
end

#drop_queue(queue) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/eventq/eventq_aws/sqs.rb', line 96

def drop_queue(queue)
  q = get_queue_url(queue)
  sqs.delete_queue(queue_url: q)

  _queue_name = EventQ.create_queue_name(queue)
  @@queue_urls.delete(_queue_name)
  @@queue_arns.delete(_queue_name)

  true
end

#get_queue_arn(queue) ⇒ Object

Returns the ARN of a queue. If none exists, nil will be returned.

Parameters:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/eventq/eventq_aws/sqs.rb', line 52

def get_queue_arn(queue)
  _queue_name = EventQ.create_queue_name(queue)

  arn = @@queue_arns[_queue_name]
  unless arn
    url = get_queue_url(queue)
    if url
      response = sqs.get_queue_attributes(
        {
          queue_url: url,
          attribute_names: ['QueueArn']
        }
      )
      arn = response.attributes['QueueArn']
    end
  end

  arn
end

#get_queue_url(queue) ⇒ Object

Returns the URL of the queue. If none exists, nil will be returned.

Parameters:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/eventq/eventq_aws/sqs.rb', line 76

def get_queue_url(queue)
  _queue_name = EventQ.create_queue_name(queue)

  url = @@queue_urls[_queue_name]
  unless url
    begin
      response= sqs.get_queue_url(
          queue_name: aws_safe_name(_queue_name)
      )
      url = response.queue_url
    rescue Aws::SQS::Errors::NonExistentQueue
      # Only want to return nil for this method when not found.
    end

    @@queue_urls[_queue_name] = url if url
  end

  url
end

#update_queue(queue, attributes = {}) ⇒ Object

Update a queue



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/eventq/eventq_aws/sqs.rb', line 36

def update_queue(queue, attributes = {})
  url = get_queue_url(queue)
  sqs.set_queue_attributes(
    {
      queue_url: url, # required
      attributes: attributes
    }
  )

  url
end