Class: Fog::AWS::SQS::Mock
- Inherits:
-
Object
- Object
- Fog::AWS::SQS::Mock
- Defined in:
- lib/fog/aws/sqs.rb,
lib/fog/aws/requests/sqs/list_queues.rb,
lib/fog/aws/requests/sqs/create_queue.rb,
lib/fog/aws/requests/sqs/delete_queue.rb,
lib/fog/aws/requests/sqs/send_message.rb,
lib/fog/aws/requests/sqs/delete_message.rb,
lib/fog/aws/requests/sqs/receive_message.rb,
lib/fog/aws/requests/sqs/get_queue_attributes.rb,
lib/fog/aws/requests/sqs/set_queue_attributes.rb,
lib/fog/aws/requests/sqs/change_message_visibility.rb
Class Method Summary collapse
Instance Method Summary collapse
- #change_message_visibility(queue_url, receipt_handle, visibility_timeout) ⇒ Object
- #create_queue(name, options = {}) ⇒ Object
- #data ⇒ Object
- #delete_message(queue_url, receipt_handle) ⇒ Object
- #delete_queue(queue_url) ⇒ Object
- #get_queue_attributes(queue_url, attribute_name) ⇒ Object
-
#initialize(options = {}) ⇒ Mock
constructor
A new instance of Mock.
- #list_queues(options = {}) ⇒ Object
- #receive_message(queue_url, options = {}) ⇒ Object
- #reset_data ⇒ Object
- #send_message(queue_url, message) ⇒ Object
- #set_queue_attributes(queue_url, attribute_name, attribute_value) ⇒ Object
- #setup_credentials(options) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Mock
Returns a new instance of Mock.
36 37 38 39 40 41 42 |
# File 'lib/fog/aws/sqs.rb', line 36 def initialize(={}) @use_iam_profile = [:use_iam_profile] setup_credentials() @region = [:region] || 'us-east-1' Fog::AWS.validate_region!(@region) end |
Class Method Details
.data ⇒ Object
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/fog/aws/sqs.rb', line 21 def self.data @data ||= Hash.new do |hash, region| hash[region] = Hash.new do |region_hash, key| region_hash[key] = { :owner_id => Fog::AWS::Mock.owner_id, :queues => {} } end end end |
.reset ⇒ Object
32 33 34 |
# File 'lib/fog/aws/sqs.rb', line 32 def self.reset @data = nil end |
Instance Method Details
#change_message_visibility(queue_url, receipt_handle, visibility_timeout) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/fog/aws/requests/sqs/change_message_visibility.rb', line 30 def (queue_url, receipt_handle, visibility_timeout) Excon::Response.new.tap do |response| if (queue = data[:queues][queue_url]) , _ = queue[:receipt_handles].find { |, receipts| receipts.key?(receipt_handle) } if queue[:messages][]['Attributes']['VisibilityTimeout'] = visibility_timeout response.body = { 'ResponseMetadata' => { 'RequestId' => Fog::AWS::Mock.request_id } } response.status = 200 else response.status = 404 raise(Excon::Errors.status_error({:expects => 200}, response)) end else response.status = 404 raise(Excon::Errors.status_error({:expects => 200}, response)) end end end |
#create_queue(name, options = {}) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/fog/aws/requests/sqs/create_queue.rb', line 28 def create_queue(name, = {}) Excon::Response.new.tap do |response| response.status = 200 now = Time.now queue_url = "https://queue.amazonaws.com/#{data[:owner_id]}/#{name}" queue = { 'QueueName' => name, 'Attributes' => { 'VisibilityTimeout' => 30, 'ApproximateNumberOfMessages' => 0, 'ApproximateNumberOfMessagesNotVisible' => 0, 'CreatedTimestamp' => now, 'LastModifiedTimestamp' => now, 'QueueArn' => Fog::AWS::Mock.arn('sqs', 'us-east-1', data[:owner_id], name), 'MaximumMessageSize' => 8192, 'MessageRetentionPeriod' => 345600 }, :messages => {}, :receipt_handles => {} } data[:queues][queue_url] = queue unless data[:queues][queue_url] response.body = { 'ResponseMetadata' => { 'RequestId' => Fog::AWS::Mock.request_id }, 'QueueUrl' => queue_url } end end |
#data ⇒ Object
44 45 46 |
# File 'lib/fog/aws/sqs.rb', line 44 def data self.class.data[@region][@aws_access_key_id] end |
#delete_message(queue_url, receipt_handle) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/fog/aws/requests/sqs/delete_message.rb', line 28 def (queue_url, receipt_handle) Excon::Response.new.tap do |response| if (queue = data[:queues][queue_url]) , _ = queue[:receipt_handles].find { |msg_id, receipts| receipts.key?(receipt_handle) } if queue[:receipt_handles].delete() queue[:messages].delete() queue['Attributes']['LastModifiedTimestamp'] = Time.now end response.body = { 'ResponseMetadata' => { 'RequestId' => Fog::AWS::Mock.request_id } } response.status = 200 else response.status = 404 raise(Excon::Errors.status_error({:expects => 200}, response)) end end end |
#delete_queue(queue_url) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/fog/aws/requests/sqs/delete_queue.rb', line 26 def delete_queue(queue_url) Excon::Response.new.tap do |response| if (queue = data[:queues][queue_url]) response.status = 200 data[:queues].delete(queue_url) response.body = { 'ResponseMetadata' => { 'RequestId' => Fog::AWS::Mock.request_id } } else response.status = 404 raise(Excon::Errors.status_error({:expects => 200}, response)) end end end |
#get_queue_attributes(queue_url, attribute_name) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/fog/aws/requests/sqs/get_queue_attributes.rb', line 28 def get_queue_attributes(queue_url, attribute_name) Excon::Response.new.tap do |response| if (queue = data[:queues][queue_url]) response.status = 200 response.body = { 'ResponseMetadata' => { 'RequestId' => Fog::AWS::Mock.request_id }, 'Attributes' => queue['Attributes'] } else response.status = 404 raise(Excon::Errors.status_error({:expects => 200}, response)) end end end |
#list_queues(options = {}) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/fog/aws/requests/sqs/list_queues.rb', line 26 def list_queues( = {}) Excon::Response.new.tap do |response| response.status = 200 response.body = { 'ResponseMetadata' => { 'RequestId' => Fog::AWS::Mock.request_id }, 'QueueUrls' => data[:queues].keys } end end |
#receive_message(queue_url, options = {}) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/fog/aws/requests/sqs/receive_message.rb', line 31 def (queue_url, = {}) Excon::Response.new.tap do |response| if (queue = data[:queues][queue_url]) = ['MaxNumberOfMessages'] || 1 now = Time.now = [] queue[:messages].values.each do |m| = m['MessageId'] invisible = if (received_handles = queue[:receipt_handles][]) visibility_timeout = m['Attributes']['VisibilityTimeout'] || queue['Attributes']['VisibilityTimeout'] received_handles.any? { |handle, time| now < time + visibility_timeout } else false end unless invisible receipt_handle = Fog::Mock.random_base64(300) queue[:receipt_handles][] ||= {} queue[:receipt_handles][][receipt_handle] = now m['Attributes'].tap do |attrs| attrs['ApproximateFirstReceiveTimestamp'] ||= now attrs['ApproximateReceiveCount'] = (attrs['ApproximateReceiveCount'] || 0) + 1 end << m.merge({ 'ReceiptHandle' => receipt_handle }) break if .size >= end end response.body = { 'ResponseMetadata' => { 'RequestId' => Fog::AWS::Mock.request_id }, 'Message' => } response.status = 200 else response.status = 404 raise(Excon::Errors.status_error({:expects => 200}, response)) end end end |
#reset_data ⇒ Object
48 49 50 |
# File 'lib/fog/aws/sqs.rb', line 48 def reset_data self.class.data[@region].delete(@aws_access_key_id) end |
#send_message(queue_url, message) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/fog/aws/requests/sqs/send_message.rb', line 28 def (queue_url, ) Excon::Response.new.tap do |response| if (queue = data[:queues][queue_url]) response.status = 200 now = Time.now = Fog::AWS::Mock. md5 = OpenSSL::Digest::MD5.hexdigest() queue[:messages][] = { 'MessageId' => , 'Body' => , 'MD5OfBody' => md5, 'Attributes' => { 'SenderId' => Fog::AWS::Mock., 'SentTimestamp' => now } } queue['Attributes']['LastModifiedTimestamp'] = now response.body = { 'ResponseMetadata' => { 'RequestId' => Fog::AWS::Mock.request_id }, 'MessageId' => , 'MD5OfMessageBody' => md5 } else response.status = 404 raise(Excon::Errors.status_error({:expects => 200}, response)) end end end |
#set_queue_attributes(queue_url, attribute_name, attribute_value) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/fog/aws/requests/sqs/set_queue_attributes.rb', line 30 def set_queue_attributes(queue_url, attribute_name, attribute_value) Excon::Response.new.tap do |response| if (queue = data[:queues][queue_url]) response.status = 200 queue['Attributes'][attribute_name] = attribute_value response.body = { 'ResponseMetadata' => { 'RequestId' => Fog::AWS::Mock.request_id } } else response.status = 404 raise(Excon::Errors.status_error({:expects => 200}, response)) end end end |
#setup_credentials(options) ⇒ Object
52 53 54 |
# File 'lib/fog/aws/sqs.rb', line 52 def setup_credentials() @aws_access_key_id = [:aws_access_key_id] end |