Class: RAWS::SQS

Inherits:
Object
  • Object
show all
Extended by:
Enumerable
Defined in:
lib/raws/sqs.rb

Defined Under Namespace

Modules: Model Classes: Adapter, Message

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queue_url) ⇒ SQS

Returns a new instance of SQS.



155
156
157
158
# File 'lib/raws/sqs.rb', line 155

def initialize(queue_url)
  @queue_url  = queue_url
  @queue_name = URI.parse(@queue_url).path.split('/').last
end

Class Attribute Details

.httpObject



11
12
13
# File 'lib/raws/sqs.rb', line 11

def http
  @http ||= RAWS.http
end

Instance Attribute Details

#queue_nameObject (readonly)

Returns the value of attribute queue_name.



153
154
155
# File 'lib/raws/sqs.rb', line 153

def queue_name
  @queue_name
end

#queue_urlObject (readonly)

Returns the value of attribute queue_url.



152
153
154
# File 'lib/raws/sqs.rb', line 152

def queue_url
  @queue_url
end

Class Method Details

.[](queue_name_or_url) ⇒ Object

Returns the instance of RAWS::SQS.



68
69
70
# File 'lib/raws/sqs.rb', line 68

def [](queue_name_or_url)
  self.new queue_url(queue_name_or_url)
end

.add_permission(queue_name, label, permissions) ⇒ Object

Adds the permissions.

  • permissions - => [ActionName, …], …

    • ActionName - *, SendMessage, ReceiveMessage, DeleteMessage, ChangeMessageVisibility, GetQueueAttributes



142
143
144
# File 'lib/raws/sqs.rb', line 142

def add_permission(queue_name, label, permissions)
  Adapter.add_permission queue_url(queue_name), label, permissions
end

.change_message_visibility(queue_name_or_url, receipt_handle, visibility_timeout) ⇒ Object

Changes the message visibility timeout.



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/raws/sqs.rb', line 116

def change_message_visibility(
  queue_name_or_url,
  receipt_handle,
  visibility_timeout
)
  Adapter.change_message_visibility(
    queue_url(queue_name_or_url),
    receipt_handle,
    visibility_timeout
  )
end

.create_queue(queue_name, default_visibility_timeout = nil) ⇒ Object

Creates a new queue and returns the instance of RAWS::SQS.



29
30
31
32
33
34
35
# File 'lib/raws/sqs.rb', line 29

def create_queue(queue_name, default_visibility_timeout=nil)
  self.new\
    Adapter.create_queue(
      queue_name,
      default_visibility_timeout
    )['CreateQueueResponse']['CreateQueueResult']['QueueUrl']
end

.delete_message(queue_name, receipt_handle) ⇒ Object

Deletes the message.



129
130
131
# File 'lib/raws/sqs.rb', line 129

def delete_message(queue_name, receipt_handle)
  Adapter.delete_message queue_url(queue_name), receipt_handle
end

.delete_queue(queue_name_or_url) ⇒ Object

Deletes the queue.



38
39
40
# File 'lib/raws/sqs.rb', line 38

def delete_queue(queue_name_or_url)
  Adapter.delete_queue queue_url(queue_name_or_url)
end

.each(prefix = nil, &block) ⇒ Object



55
56
57
# File 'lib/raws/sqs.rb', line 55

def each(prefix=nil, &block)
  list_queues(prefix).each(&block)
end

.get_queue_attributes(queue_name_or_url, *attrs) ⇒ Object

Returns the queue attributes.

  • attrs - All, ApproximateNumberOfMessages, ApproximateNumberOfMessagesNotVisible, VisibilityTimeout, CreatedTimestamp, LastModifiedTimestamp, Policy



82
83
84
85
86
87
# File 'lib/raws/sqs.rb', line 82

def get_queue_attributes(queue_name_or_url, *attrs)
  Adapter.get_queue_attributes(
    queue_url(queue_name_or_url),
    *attrs
  )['GetQueueAttributesResponse']['GetQueueAttributesResult']['Attribute']
end

.list_queues(prefix = nil) ⇒ Object

Returns an array of RAWS::SQS objects.



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/raws/sqs.rb', line 43

def list_queues(prefix=nil)
  doc = Adapter.list_queues(prefix)\
    ['ListQueuesResponse']['ListQueuesResult']
  if doc
    doc['QueueUrl'].map do |val|
      self.new(val)
    end
  else
    []
  end
end

.queue_url(queue_name_or_url) ⇒ Object

Returns the queue URL for the queue_name_or_url if exists the queue.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/raws/sqs.rb', line 16

def queue_url(queue_name_or_url)
  if URI.parse(queue_name_or_url).scheme
    queue_name_or_url
  else
    list_queues(queue_name_or_url).map do |sqs|
      sqs.queue_url
    end.each do |url|
      return url if URI.parse(url).path.split('/').last == queue_name_or_url
    end
  end
end

.queues(&block) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/raws/sqs.rb', line 59

def queues(&block)
  if block_given?
    each(&block)
  else
    map
  end
end

.receiveObject

Receives one or more messages form the queue. Returns an array of message data.



113
114
115
116
117
118
119
120
# File 'lib/raws/sqs.rb', line 113

def receive_message(queue_name_or_url, params={}, *attrs)
  doc = Adapter.receive_message(
    queue_url(queue_name_or_url),
    params,
    *attrs
  )['ReceiveMessageResponse']['ReceiveMessageResult']
  doc ? doc['Message'] : []
end

.receive_message(queue_name_or_url, params = {}, *attrs) ⇒ Object

Receives one or more messages form the queue. Returns an array of message data.



105
106
107
108
109
110
111
112
# File 'lib/raws/sqs.rb', line 105

def receive_message(queue_name_or_url, params={}, *attrs)
  doc = Adapter.receive_message(
    queue_url(queue_name_or_url),
    params,
    *attrs
  )['ReceiveMessageResponse']['ReceiveMessageResult']
  doc ? doc['Message'] : []
end

.remove_permission(queue_name, label) ⇒ Object

Removes the permission.



147
148
149
# File 'lib/raws/sqs.rb', line 147

def remove_permission(queue_name, label)
  Adapter.remove_permission queue_url(queue_name), label
end

.sendObject

Sends a message to the queue.



101
102
103
# File 'lib/raws/sqs.rb', line 101

def send_message(queue_name_or_url, msg)
  Adapter.send_message queue_url(queue_name_or_url), msg
end

.send_message(queue_name_or_url, msg) ⇒ Object

Sends a message to the queue.



98
99
100
# File 'lib/raws/sqs.rb', line 98

def send_message(queue_name_or_url, msg)
  Adapter.send_message queue_url(queue_name_or_url), msg
end

.set_queue_attributes(queue_name_or_url, attrs) ⇒ Object

Sets the queue attributes.

  • attrs - => AttributeValue

    • AttributeName - VisibilityTimeout, Policy



93
94
95
# File 'lib/raws/sqs.rb', line 93

def set_queue_attributes(queue_name_or_url, attrs)
  Adapter.set_queue_attributes queue_url(queue_name_or_url), attrs
end

Instance Method Details

#<=>(a) ⇒ Object



234
235
236
# File 'lib/raws/sqs.rb', line 234

def <=>(a)
  queue_name <=> a.queue_name
end

#add_permission(label, permission) ⇒ Object

Adds the permissions.

  • permissions - => [ActionName, …], …

    • ActionName - *, SendMessage, ReceiveMessage, DeleteMessage, ChangeMessageVisibility, GetQueueAttributes



225
226
227
# File 'lib/raws/sqs.rb', line 225

def add_permission(label, permission)
  self.class.add_permission queue_url, label, permission
end

#change_message_visibility(receipt_handle, visibility_timeout) ⇒ Object

Changes the message visivility timeout.



203
204
205
206
207
208
209
# File 'lib/raws/sqs.rb', line 203

def change_message_visibility(receipt_handle, visibility_timeout)
  self.class.change_message_visibility(
    queue_url,
    receipt_handle,
    visibility_timeout
  )
end

#delete_message(receipt_handle) ⇒ Object

Deletes the message.



212
213
214
# File 'lib/raws/sqs.rb', line 212

def delete_message(receipt_handle)
  self.class.delete_message queue_url, receipt_handle
end

#delete_queueObject

Delete the queue.



161
162
163
# File 'lib/raws/sqs.rb', line 161

def delete_queue
  self.class.delete_queue queue_url
end

#get_queue_attributes(*attrs) ⇒ Object

Returns the queue attributes.

  • attrs - All, ApproximateNumberOfMessages, ApproximateNumberOfMessagesNotVisible, VisibilityTimeout, CreatedTimestamp, LastModifiedTimestamp, Policy



175
176
177
# File 'lib/raws/sqs.rb', line 175

def get_queue_attributes(*attrs)
  self.class.get_queue_attributes queue_url, *attrs
end

#receive_message(params = {}, *attrs) ⇒ Object Also known as: receive

Receives the messages form the queue. Returns an array of RAWS::SQS::Message objects.



195
196
197
198
199
# File 'lib/raws/sqs.rb', line 195

def receive_message(params={}, *attrs)
  self.class.receive_message(queue_url, params, *attrs).map do |val|
    Message.new self, val
  end
end

#remove_permission(label) ⇒ Object

Removes the permission.



230
231
232
# File 'lib/raws/sqs.rb', line 230

def remove_permission(label)
  self.class.remove_permission queue_url, label
end

#send_message(msg) ⇒ Object Also known as: send

Sends the message to the queue.



188
189
190
# File 'lib/raws/sqs.rb', line 188

def send_message(msg)
  self.class.send_message queue_url, msg
end

#set_queue_attributes(attrs) ⇒ Object

Sets the queue attributes.

  • attrs - => AttributeValue

    • AttributeName - VisibilityTimeout, Policy



183
184
185
# File 'lib/raws/sqs.rb', line 183

def set_queue_attributes(attrs)
  self.class.set_queue_attributes queue_url, attrs
end