Class: Jungle::SQSClient

Inherits:
AWSClient show all
Defined in:
lib/jungle/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth) ⇒ SQSClient

Return a new SQSClient

+auth+ is a Jungle::Auth object with knowledge of authentication credentials


94
95
96
97
98
99
100
101
102
# File 'lib/jungle/client.rb', line 94

def initialize(auth)
  @auth = auth
  @debug = false
  
  @mode = :lazy
  @queue_lookup = {}
  
  self.service_url = "queue.amazonaws.com"
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



89
90
91
# File 'lib/jungle/client.rb', line 89

def debug
  @debug
end

#modeObject

Returns the value of attribute mode.



90
91
92
# File 'lib/jungle/client.rb', line 90

def mode
  @mode
end

#service_urlObject

Returns the value of attribute service_url.



88
89
90
# File 'lib/jungle/client.rb', line 88

def service_url
  @service_url
end

Instance Method Details

#create_queue(name, options = {}) ⇒ Object

Create a new queue with the given name. If the queue already exists, the operation returns the same response as if it had just been created.

+name+ is the name of the queue you wish to create, e.g. Foo
+options+ is a hash that may specify the following options:
  +visibility_timeout+ is the visibility timeout to set as the default for
                       new messages on this queue

The response has the following readers:

#operation - :create_queue
#code - the http response code (200 on success)
#xml - the full xml body that was returned with the request
#queue_url - the queue path that was created


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/jungle/client.rb', line 129

def create_queue(name, options = {})
  validate_queue_name(name)
  
  # construct request url string
  path = ''
  query = {'QueueName' => name}
  query.merge! options
  
  # get security params as a hash
  security_params = @auth.security_params('POST', path)
  
  # make the call
  http_response = rest_call(self.service_url, path, query, security_params)
  
  # create the sqs response
  sqs_response = SQSResponseFactory.manufacture(:create_queue, http_response)
  
  # add to queue lookup if response was success and mode is lazy
  if @mode == :lazy && sqs_response.code == 200
    @queue_lookup[name] ||= sqs_response.queue_url
  end
  
  sqs_response
end

#delete_message(message_or_queue, message_id = nil) ⇒ Object

Delete a message

+queue+ is the name (lazy mode) or path (strict mode) of the queue
+message_id+ is the message id of the message

The response has the following readers:

#operation - :delete_message
#code - the http response code (200 on success)
#xml - the full xml body that was returned with the request


279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/jungle/client.rb', line 279

def delete_message(message_or_queue, message_id = nil)
  message_values = {}
  
  if message_or_queue.instance_of? SQSMessage
    message = message_or_queue
    message_values[:queue_path] = message.queue_path
    message_values[:id] = message.id
  else
    queue = message_or_queue
    validate_queue_name(queue)
    message_values[:queue_path] = resolve_queue_path(queue)
    message_values[:id] = message_id
  end
  
  # construct request url string
  path = '/' << message_values[:queue_path] << '/' << message_values[:id]
  query = {}
  
  # get security params as a hash
  security_params = @auth.security_params('DELETE', path)
  
  # make the call
  response = rest_call(self.service_url, path, query, security_params)
  
  # return the response
  SQSResponseFactory.manufacture(:delete_message, response)
end

#list_queues(options = {}) ⇒ Object

List the available queues on this account (up to 10,000).

+options+ is a hash that may specify the following options:
  +queue_name_prefix+ is a string that, if specified, will cause
                      only queues that start with the given string
                      to be returned.

The response has the following readers:

#operation - :list_queues
#code - the http response code (200 on success)
#xml - the full xml body that was returned with the request
#queue_urls - an array of queue urls


165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/jungle/client.rb', line 165

def list_queues(options = {})
  # construct request url string
  path = ''
  query = options
  
  # get security params as a hash
  security_params = @auth.security_params('GET', path)
  
  # make the call
  http_response = rest_call(self.service_url, path, query, security_params)
  
  # create the sqs response
  SQSResponseFactory.manufacture(:list_queues, http_response)
end

#peek_message(queue, message_id) ⇒ Object

Look at a message without deleting it from the queue or changing its visibility.

+queue+ is the name (lazy mode) or path (strict mode) of the queue (see mode=)
+message_id+ is the message id of the message

The response has the following readers:

#operation - :peek_message
#code - the http response code (200 on success)
#xml - the full xml body that was returned with the request
#message - the SQSMessage

An SQSMessage has the following readers:

#id - the id of the message
#body - the body of the message


321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/jungle/client.rb', line 321

def peek_message(queue, message_id)
  validate_queue_name(queue)
  
  queue_path = resolve_queue_path(queue)
  
  # construct request url string
  path = '/' << queue_path << '/' << message_id
  query = {}
  
  # get security params as a hash
  security_params = @auth.security_params('GET', path)
  
  # make the call
  response = rest_call(self.service_url, path, query, security_params)
  
  # return the response
  SQSResponseFactory.manufacture(:peek_message, response)
end

#receive_message(queue, options = {}) ⇒ Object

Receive a single message from the given queue

+queue+ is the name (lazy mode) or path (strict mode) of the queue
+options+ is a hash that may specify the following options:
  +visibility_timeout+ is the duration, in seconds, that the received message
                       will not be visible to other receive_message or
                       receive messages calls.

The response has the following readers:

#operation - :receive_message
#code - the http response code (200 on success)
#xml - the full xml body that was returned with the request
#message - the SQSMessage object or nil if none were returned

An SQSMessage has the following readers:

#id - the id of the message
#body - the body of the message


266
267
268
269
# File 'lib/jungle/client.rb', line 266

def receive_message(queue, options = {})
  local_options = { :number_of_messages => 1 }
  receive_messages(queue, options.merge(local_options))
end

#receive_messages(queue, options = {}) ⇒ Object

Receive a message from the given queue

+queue+ is the name (lazy mode) or path (strict mode) of the queue
+options+ is a hash that may specify the following options:
  +number_of_messages+ is the maximum number of messages to return. If the number
                       of messages in the queue is less than the value specified
                       then all of the remaining messages will be received (default 1)
  +visibility_timeout+ is the duration, in seconds, that the received message or messages
                       will not be visible to other receive_message calls.

The response has the following readers:

#operation - :receive_message
#code - the http response code (200 on success)
#xml - the full xml body that was returned with the request
#messages - an array of SQSMessage objects

An SQSMessage has the following readers:

#id - the id of the message
#body - the body of the message


231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/jungle/client.rb', line 231

def receive_messages(queue, options = {})
  validate_queue_name(queue)
  
  queue_path = resolve_queue_path(queue)
  
  # construct request url string
  path = '/' << queue_path << '/front'
  query = options
  
  # get security params as a hash
  security_params = @auth.security_params('GET', path)
  
  # make the call
  response = rest_call(self.service_url, path, query, security_params)
  
  # return the response
  SQSResponseFactory.manufacture(:receive_message, response, {:queue_path => queue_path})
end

#send_message(queue, message) ⇒ Object

Put the specified message on the given queue

+queue+ is the name of the queue on which to add the message
+message+ is the data that this message should contain. Must be between 1 and 256k bytes

The response has the following readers:

#operation - :send_message
#code - the http response code (200 on success)
#xml - the full xml body that was returned with the request
#message_id - the id of the just created message


189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/jungle/client.rb', line 189

def send_message(queue, message)
  validate_queue_name(queue)
  validate_message(message)
  
  # if in lazy mode, find queue path
  queue_path = resolve_queue_path(queue)
  
  # construct request url string
  path = '/' << queue_path << '/back'
  query = {}
  
  # get security params as a hash
  security_params = @auth.security_params('PUT', path)
  
  # prepare the message
  payload = message
  
  # make the call
  response = rest_call(self.service_url, path, query, security_params, payload)
  
  # return the response
  SQSResponseFactory.manufacture(:send_message, response)
end