Class: SimpleQS::Queue
- Inherits:
-
Object
- Object
- SimpleQS::Queue
- Defined in:
- lib/simple_qs/queue.rb
Defined Under Namespace
Classes: MaxMessageSizeError, MaxVisibilityError, MessageRetentionPeriodError
Constant Summary collapse
- MAX_VISIBILITY_TIMEOUT =
43200
- ALLOWED_ATTRIBUTES =
[ :VisibilityTimeout, :Policy, :MaximumMessageSize, :MessageRetentionPeriod ]
Instance Attribute Summary collapse
-
#queue_url ⇒ Object
Returns the value of attribute queue_url.
Class Method Summary collapse
-
.check_queue_name(name) ⇒ Object
Performs checks on queue name.
-
.check_visibility_timeout(default_visibility_timeout) ⇒ Object
Performs checks on visibility timeout.
-
.create(name, default_visibility_timeout = nil, &block) ⇒ Object
Create new queue Parameters: name - String, name of queue.
- .delete(queue_name) ⇒ Object (also: destroy)
- .exists?(queue_name) ⇒ Boolean
-
.list(pattern = nil) ⇒ Object
List queues Parameters: pattern - String, first letterns of queues names [Optional].
Instance Method Summary collapse
-
#add_permissions(label, permissions) ⇒ Object
label - String, identifier for permissions permissions - Array of Hashes, [=> ‘125074342641’, :action => ‘SendMessage’, …].
- #build_request(method, params) ⇒ Object
-
#delete ⇒ Object
Deletes this queue Returns: Bool - true, if successful Raises: SimpleQS::Responce::Error.
-
#deleted ⇒ Object
(also: #deleted?)
Is this queue deleted? Returns: Bool - true if queue is deleted.
-
#destroy ⇒ Object
Deletes this queue Returns: Bool - true, if successful Raises: SimpleQS::Responce::Error.
- #get_attributes(attribute_or_array = nil) ⇒ Object
-
#initialize(queue_url_or_name) ⇒ Queue
constructor
Initializes new SimpleQS::Queue object Parameters: queue_url_or_name - String, either queue url or queue name.
-
#name ⇒ Object
Get queue name Returns: String - queue name.
- #receive_messages(attributes = nil, max_number_of_messages = nil, visibility_timeout = nil) ⇒ Object
- #remove_permissions(label) ⇒ Object
- #send_message(body) ⇒ Object
- #set_attributes(attributes) ⇒ Object
- #set_maximum_message_size(message_size) ⇒ Object
- #set_message_retention_period(period) ⇒ Object
- #set_policy(policy) ⇒ Object
- #set_visibility_timeout(visibility_timeout) ⇒ Object
Constructor Details
#initialize(queue_url_or_name) ⇒ Queue
Initializes new SimpleQS::Queue object Parameters:
queue_url_or_name - String, either queue url or queue name
15 16 17 18 19 20 21 22 23 |
# File 'lib/simple_qs/queue.rb', line 15 def initialize(queue_url_or_name) begin self.class.check_queue_name(queue_url_or_name) @name = queue_url_or_name @queue_url = "http://#{SimpleQS.host}/#{SimpleQS.account_id}/#{@name}" rescue ArgumentError @queue_url = queue_url_or_name end end |
Instance Attribute Details
#queue_url ⇒ Object
Returns the value of attribute queue_url.
10 11 12 |
# File 'lib/simple_qs/queue.rb', line 10 def queue_url @queue_url end |
Class Method Details
.check_queue_name(name) ⇒ Object
Performs checks on queue name. Raises ArgumentError with message in case of constraint violation
272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/simple_qs/queue.rb', line 272 def check_queue_name(name) raise ArgumentError, "expected to be String, but got #{name.class.to_s}" unless name.class == String raise( ArgumentError, "should be maximum 80 characters long" ) if name.length > 80 raise( ArgumentError, "should contain only alphanumeric characters, hyphens (-), and underscores (_)" ) if name =~ /[^a-zA-Z0-9\_\-]/ end |
.check_visibility_timeout(default_visibility_timeout) ⇒ Object
Performs checks on visibility timeout. Raises ArgumentError or SimpleQS::Queue::MaxVisibilityError with message in case of constraint violation
286 287 288 289 290 291 292 293 294 295 |
# File 'lib/simple_qs/queue.rb', line 286 def check_visibility_timeout(default_visibility_timeout) raise( ArgumentError, "expected visibility timeout to respon to to_i, but #{default_visibility_timeout.class.to_s} doesn't" ) if default_visibility_timeout && default_visibility_timeout.class != Fixnum raise( MaxVisibilityError, "expected visibility timeout to be in 0...#{MAX_VISIBILITY_TIMEOUT} range, got #{default_visibility_timeout}" ) if default_visibility_timeout && !(0...MAX_VISIBILITY_TIMEOUT).include?(default_visibility_timeout) end |
.create(name, default_visibility_timeout = nil, &block) ⇒ Object
Create new queue Parameters:
name - String, name of queue. Should be not longer than 80 chars and contain only a-zA-Z0-9\-\_
default_visibility_timeout - Fixnum, visibility timeout [Optional]. In range 0 to MAX_VISIBILITY_TIMEOUT.
If nil, then default is used (30 seconds).
&block - if given, then newly created queue object passed to it
Returns:
SimpleQS::Queue
Raises:
ArgumentError
SimpleQS::Queue::MaxVisibilityError
SimpleQS::Responce::Error
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/simple_qs/queue.rb', line 211 def create(name, default_visibility_timeout = nil, &block) check_queue_name(name) check_visibility_timeout(default_visibility_timeout) params = { 'Action' => 'CreateQueue', 'QueueName' => name } params['DefaultVisibilityTimeout'] = default_visibility_timeout if default_visibility_timeout request = SimpleQS::Request.build(:get, params) responce = request.perform if responce.successful? queue = new(responce.queue_url) yield queue if block_given? queue else raise responce.to_error end end |
.delete(queue_name) ⇒ Object Also known as: destroy
265 266 267 |
# File 'lib/simple_qs/queue.rb', line 265 def delete(queue_name) new(queue_name).delete end |
.exists?(queue_name) ⇒ Boolean
261 262 263 |
# File 'lib/simple_qs/queue.rb', line 261 def exists?(queue_name) list(queue_name).any? {|queue| queue.name == queue_name} end |
.list(pattern = nil) ⇒ Object
List queues Parameters:
pattern - String, first letterns of queues names [Optional].
Returns:
Array - array of SimpleQS::Queue or empty if nothing found
Rises:
ArgumentError
SimpleQS::Responce::Error
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/simple_qs/queue.rb', line 241 def list(pattern = nil) params = { 'Action' => 'ListQueues' } check_queue_name(pattern) if pattern params['QueueNamePrefix'] = pattern if pattern request = SimpleQS::Request.build(:get, params) responce = request.perform raise responce.to_error unless responce.successful? begin queues = responce.queue_url.class == Array ? responce.queue_url : [responce.queue_url] queues.map {|queue_url| new(queue_url)} rescue NoMethodError [] end end |
Instance Method Details
#add_permissions(label, permissions) ⇒ Object
label - String, identifier for permissions permissions - Array of Hashes, [=> ‘125074342641’, :action => ‘SendMessage’, …]
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/simple_qs/queue.rb', line 155 def (label, ) self.class.check_queue_name(label) params = { 'Action' => 'AddPermission', 'Label' => label } i = 1 .each do || raise ArgumentError, "invalid action: #{[:action]}"\ unless ['*', 'SendMessage', 'ReceiveMessage', 'DeleteMessage', 'ChangeMessageVisibility', 'GetQueueAttributes'].include?([:action]) params["AWSAccountId.#{i}"] = [:account_id] params["ActionName.#{i}"] = [:action] i += 1 end request = build_request(:post, params) responce = request.perform raise responce.to_error unless responce.successful? end |
#build_request(method, params) ⇒ Object
192 193 194 195 196 |
# File 'lib/simple_qs/queue.rb', line 192 def build_request(method, params) request = SimpleQS::Request.build(method, params) request.query_string = [SimpleQS.account_id, name] request end |
#delete ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/simple_qs/queue.rb', line 46 def delete params = { 'Action' => 'DeleteQueue' } request = build_request(:get, params) responce = request.perform raise responce.to_error unless responce.successful? @deleted = true end |
#deleted ⇒ Object Also known as: deleted?
Is this queue deleted?
Returns:
Bool - true if queue is deleted
35 36 37 |
# File 'lib/simple_qs/queue.rb', line 35 def deleted @deleted ||= false end |
#destroy ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/simple_qs/queue.rb', line 58 def delete params = { 'Action' => 'DeleteQueue' } request = build_request(:get, params) responce = request.perform raise responce.to_error unless responce.successful? @deleted = true end |
#get_attributes(attribute_or_array = nil) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/simple_qs/queue.rb', line 68 def get_attributes(attribute_or_array = nil) params = { 'Action' => 'GetQueueAttributes' } if attribute_or_array attribute_or_array = [attribute_or_array] unless attribute_or_array.class == Array attribute_or_array.uniq! unless (attribute_or_array - [:All, :ApproximateNumberOfMessages, :ApproximateNumberOfMessagesNotVisible, :VisibilityTimeout, :CreatedTimestamp, :LastModifiedTimestamp, :Policy]).empty? raise ArgumentError,\ "Allowed attributes: :All, :ApproximateNumberOfMessages, :ApproximateNumberOfMessagesNotVisible, " << ":VisibilityTimeout, :CreatedTimestamp, :LastModifiedTimestamp, :Policy" end attribute_or_array.each_index do |i| params["AttributeName.#{i + 1}"] = attribute_or_array[i] end end request = build_request(:get, params) responce = request.perform raise responce.to_error unless responce.successful? if responce.respond_to?(:attribute) responce.attribute.inject({}) do |result, key_value| result[key_value['Name']] = key_value['Value'] result end else {} end end |
#name ⇒ Object
Get queue name Returns:
String - queue name
28 29 30 |
# File 'lib/simple_qs/queue.rb', line 28 def name @name ||= @queue_url.split(/\//).last end |
#receive_messages(attributes = nil, max_number_of_messages = nil, visibility_timeout = nil) ⇒ Object
64 65 66 |
# File 'lib/simple_qs/queue.rb', line 64 def (attributes = nil, = nil, visibility_timeout = nil) SimpleQS::Message.receive(self, attributes, , visibility_timeout) end |
#remove_permissions(label) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/simple_qs/queue.rb', line 180 def (label) params = { 'Action' => 'RemovePermission', 'Label' => label } request = build_request(:get, params) responce = request.perform raise responce.to_error unless responce.successful? end |
#send_message(body) ⇒ Object
60 61 62 |
# File 'lib/simple_qs/queue.rb', line 60 def (body) SimpleQS::Message.send(self, body) end |
#set_attributes(attributes) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/simple_qs/queue.rb', line 124 def set_attributes(attributes) unless (attributes.keys - ALLOWED_ATTRIBUTES).empty? raise ArgumentError, "Allowed attributes: #{ALLOWED_ATTRIBUTES.map {|attr| ":" << attr.to_s}.join(", ")}" end self.class.check_visibility_timeout(attributes[:VisibilityTimeout]) if attributes[:VisibilityTimeout] if attributes[:MaximumMessageSize] && !(1024..65536).include?(attributes[:MaximumMessageSize]) raise MaxMessageSizeError, 'MaximumMessageSize should be between 1024 and 65536' end if attributes[:MessageRetentionPeriod] && !(3600..1209600).include?(attributes[:MessageRetentionPeriod]) raise MessageRetentionPeriodError, 'MessageRetentionPeriod should be between 3600 and 1209600' end params = { 'Action' => 'SetQueueAttributes' } i = 1 attributes.each do |key, value| params["Attribute.#{i}.Name"] = key.to_s params["Attribute.#{i}.Value"] = value i += 1 end request = build_request(:get, params) responce = request.perform raise responce.to_error unless responce.successful? end |
#set_maximum_message_size(message_size) ⇒ Object
109 110 111 |
# File 'lib/simple_qs/queue.rb', line 109 def () set_attributes({:MaximumMessageSize => }) end |
#set_message_retention_period(period) ⇒ Object
113 114 115 |
# File 'lib/simple_qs/queue.rb', line 113 def (period) set_attributes({:MessageRetentionPeriod => period}) end |
#set_policy(policy) ⇒ Object
105 106 107 |
# File 'lib/simple_qs/queue.rb', line 105 def set_policy(policy) set_attributes({:Policy => policy}) end |
#set_visibility_timeout(visibility_timeout) ⇒ Object
101 102 103 |
# File 'lib/simple_qs/queue.rb', line 101 def set_visibility_timeout(visibility_timeout) set_attributes({:VisibilityTimeout => visibility_timeout}) end |