Class: AWS::SQS::QueueCollection

Inherits:
Object
  • Object
show all
Includes:
Core::Model, Enumerable
Defined in:
lib/aws/sqs/queue_collection.rb

Overview

Represents all the Queue objects in your account.

If you have permission to access a queue created by another account, you can also use this collection to access that queue by URL.

Examples:

Printing the URLs of all queues

pp sqs.queues.map(&:url)

Filtering queues by queue name prefix

pp sqs.queues.with_prefix("production_").map(&:url)

Accessing a queue by URL

url = "http://sqs.us-east-1.amazonaws.com/123456789012/myqueue"
sqs.queues[url].send_message("HELLO")

Instance Attribute Summary collapse

Attributes included from Core::Model

#config

Instance Method Summary collapse

Methods included from Core::Model

#client, #config_prefix, #inspect

Constructor Details

#initialize(opts = {}) ⇒ QueueCollection

Returns a new instance of QueueCollection.



38
39
40
41
# File 'lib/aws/sqs/queue_collection.rb', line 38

def initialize(opts = {})
  @prefix = opts[:prefix]
  super
end

Instance Attribute Details

#prefixString (readonly)

Returns The queue name prefix by which this collection is filtered.

Returns:

  • (String)

    The queue name prefix by which this collection is filtered.



45
46
47
# File 'lib/aws/sqs/queue_collection.rb', line 45

def prefix
  @prefix
end

Instance Method Details

#[](url) ⇒ Queue

Returns The queue with the given URL.

Returns:

  • (Queue)

    The queue with the given URL.



130
131
132
# File 'lib/aws/sqs/queue_collection.rb', line 130

def [] url
  Queue.new(url, :config => config)
end

#create(name, options = {}) ⇒ Queue

Note:

If you delete a queue, you must wait at least 60 seconds before creating a queue with the same name.

Creates a new queue.

Parameters:

  • name (String)

    The name to use for the queue created. Constraints: Maximum 80 characters; alphanumeric characters, hyphens (-), and underscores (_) are allowed.

    The name of the queue should be unique within your account. If you provide the name of an existing queue with the same options it was created with then no error is raised and the existing queue will be returned.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :visibility_timeout (Integer) — default: 30

    The number of seconds a message received from a queue will be invisible to others when they ask to receive messages.

  • :policy (Policy)

    A policy object or policy desription (a json string).

  • :maximum_message_size (Integer) — default: 65536

    The maximum number of bytes a message can contain before Amazon SQS rejects it.

  • :delay_seconds (Integer)

    The time in seconds that the delivery of all messages in the queue will be delayed. This can be overriden when sending a message to the queue.

  • :message_retention_period (Integer)

    The number of seconds from 60 (1 minute) to 1209600 (14 days). The default is 345600 (4 days).

Returns:

  • (Queue)

    The newly created queue.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/aws/sqs/queue_collection.rb', line 84

def create name, options = {}

  # SQS removed the default prefix to the visibility timeout option
  # in the 2011-10-01 update -- this allows us to not break existing
  # customers.
  if options[:default_visibility_timeout]
    options[:visibility_timeout] = 
      options.delete(:default_visibility_timeout)
  end

  if policy = options[:policy]
    options[:policy] = policy.to_json unless policy.is_a?(String)
  end

  client_opts = {}
  client_opts[:queue_name] = name
  unless options.empty?
    client_opts[:attributes] = options.inject({}) do |attributes,(k,v)|
      attributes.merge(Core::Inflection.class_name(k.to_s) => v.to_s)
    end
  end

  response = client.create_queue(client_opts)

  Queue.new(response[:queue_url], :config => config)

end

#each {|queue| ... } ⇒ Object

Yield Parameters:



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

def each(&block)
  options = {}
  options[:queue_name_prefix] = prefix if prefix
  client.list_queues(options)[:queue_urls].each do |url|
    queue = self[url]
    yield(queue)
  end
end

#named(queue_name, options = {}) ⇒ Queue

Returns the queue with the given name. This requires making a request to SQS to get the queue url. If you know the url, you should use #[] instead.

queue = AWS::SQS.new.queues.named('my-queue')

Parameters:

  • queue_name (String)

    The name of the queue you need a URL for.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :queue_owner_aws_account_id (String)

    The AWS account ID of the account that created the queue. You can only get the url for queues in other accounts when the account owner has granted you permission.

Returns:

  • (Queue)

    Returns the queue with the given name.



143
144
145
# File 'lib/aws/sqs/queue_collection.rb', line 143

def named queue_name, options = {}
  self[url_for(queue_name, options = {})]
end

#url_for(queue_name, options = {}) ⇒ Object

Returns the url for the given queue.

sqs.queues.url_for('my-queue') 
#=> "https://sqs.us-east-1.amazonaws.com/123456789012/my-queue"

Parameters:

  • queue_name (String)

    The name of the queue you need a URL for.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :queue_owner_aws_account_id (String)

    The AWS account ID of the account that created the queue. You can only get the url for queues in other accounts when the account owner has granted you permission.



161
162
163
164
165
# File 'lib/aws/sqs/queue_collection.rb', line 161

def url_for queue_name, options = {}
  client_opts = {}
  client_opts[:queue_name] = queue_name
  client.get_queue_url(client_opts.merge(options))[:queue_url]
end

#with_prefix(prefix) ⇒ QueueCollection

Returns A new collection representing only the queues whose names start with the given prefix.

Parameters:

  • prefix (String)

    The queue name prefix.

Returns:

  • (QueueCollection)

    A new collection representing only the queues whose names start with the given prefix.



125
126
127
# File 'lib/aws/sqs/queue_collection.rb', line 125

def with_prefix(prefix)
  self.class.new(:prefix => prefix, :config => config)
end