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.
38 39 40 41 42 43 44 45 46 |
# File 'lib/fog/aws/sqs.rb', line 38 def initialize(={}) @use_iam_profile = [:use_iam_profile] setup_credentials() @region = [:region] || 'us-east-1' unless ['ap-northeast-1', 'ap-southeast-1', 'ap-southeast-2', 'eu-west-1', 'us-east-1', 'us-west-1', 'us-west-2', 'sa-east-1'].include?(@region) raise ArgumentError, "Unknown region: #{@region.inspect}" end end |
Class Method Details
.data ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/fog/aws/sqs.rb', line 23 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
34 35 36 |
# File 'lib/fog/aws/sqs.rb', line 34 def self.reset @data = nil end |
Instance Method Details
#change_message_visibility(queue_url, receipt_handle, visibility_timeout) ⇒ Object
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 |
# File 'lib/fog/aws/requests/sqs/change_message_visibility.rb', line 33 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.has_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
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/create_queue.rb', line 30 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
48 49 50 |
# File 'lib/fog/aws/sqs.rb', line 48 def data self.class.data[@region][@aws_access_key_id] end |
#delete_message(queue_url, receipt_handle) ⇒ 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 |
# File 'lib/fog/aws/requests/sqs/delete_message.rb', line 31 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.has_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
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/fog/aws/requests/sqs/delete_queue.rb', line 29 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
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/fog/aws/requests/sqs/get_queue_attributes.rb', line 31 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
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/fog/aws/requests/sqs/list_queues.rb', line 27 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
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 80 81 82 |
# File 'lib/fog/aws/requests/sqs/receive_message.rb', line 34 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
52 53 54 |
# File 'lib/fog/aws/sqs.rb', line 52 def reset_data self.class.data[@region].delete(@aws_access_key_id) end |
#send_message(queue_url, message) ⇒ 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 |
# File 'lib/fog/aws/requests/sqs/send_message.rb', line 31 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 = 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
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/fog/aws/requests/sqs/set_queue_attributes.rb', line 32 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
56 57 58 |
# File 'lib/fog/aws/sqs.rb', line 56 def setup_credentials() @aws_access_key_id = [:aws_access_key_id] end |