From 1cd852606eec2e74f806330a6029124f2ccc50a4 Mon Sep 17 00:00:00 2001 From: Nina Berg Date: Fri, 9 Jan 2015 15:05:28 -0500 Subject: [PATCH 1/3] messages can now have attributes --- fake_sqs.gemspec | 3 ++- lib/fake_sqs.rb | 1 + lib/fake_sqs/actions/receive_message.rb | 15 ++++++++++++++- lib/fake_sqs/actions/send_message.rb | 2 +- lib/fake_sqs/extended_hash.rb | 17 +++++++++++++++++ lib/fake_sqs/message.rb | 4 +++- 6 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 lib/fake_sqs/extended_hash.rb diff --git a/fake_sqs.gemspec b/fake_sqs.gemspec index df9566a..9fdfb51 100644 --- a/fake_sqs.gemspec +++ b/fake_sqs.gemspec @@ -20,8 +20,9 @@ Gem::Specification.new do |gem| gem.add_dependency "sinatra" gem.add_dependency "builder" + gem.add_dependency "deep_merge" - gem.add_development_dependency "rspec" + gem.add_development_dependency "rspec", "< 3.0" gem.add_development_dependency "rake" gem.add_development_dependency "aws-sdk" gem.add_development_dependency "faraday" diff --git a/lib/fake_sqs.rb b/lib/fake_sqs.rb index e70394b..0c24aa3 100644 --- a/lib/fake_sqs.rb +++ b/lib/fake_sqs.rb @@ -11,6 +11,7 @@ require 'fake_sqs/version' require 'fake_sqs/memory_database' require 'fake_sqs/file_database' +require 'fake_sqs/extended_hash' module FakeSQS diff --git a/lib/fake_sqs/actions/receive_message.rb b/lib/fake_sqs/actions/receive_message.rb index 55d3e55..bc2de39 100644 --- a/lib/fake_sqs/actions/receive_message.rb +++ b/lib/fake_sqs/actions/receive_message.rb @@ -10,7 +10,7 @@ def initialize(options = {}) def call(name, params) queue = @queues.get(name) - messages = queue.receive_message(params) + messages = queue.receive_message(params.dotted_to_nested_hash) @responder.call :ReceiveMessage do |xml| messages.each do |receipt, message| xml.Message do @@ -18,6 +18,19 @@ def call(name, params) xml.ReceiptHandle receipt xml.MD5OfBody message.md5 xml.Body message.body + + # TODO: should only return the requested attribtues + message.message_attributes.each do |index, attribute| + xml.MessageAttribute do + xml.Name attribute['Name'] + xml.Value do |value| + xml.DataType attribute['Value']['DataType'] + + # TODO: other types exist + xml.StringValue attribute['Value']['StringValue'] + end + end + end end end end diff --git a/lib/fake_sqs/actions/send_message.rb b/lib/fake_sqs/actions/send_message.rb index 79adcd1..bf1e5cd 100644 --- a/lib/fake_sqs/actions/send_message.rb +++ b/lib/fake_sqs/actions/send_message.rb @@ -10,7 +10,7 @@ def initialize(options = {}) def call(name, params) queue = @queues.get(name) - message = queue.send_message(params) + message = queue.send_message(params.dotted_to_nested_hash) @responder.call :SendMessage do |xml| xml.MD5OfMessageBody message.md5 xml.MessageId message.id diff --git a/lib/fake_sqs/extended_hash.rb b/lib/fake_sqs/extended_hash.rb new file mode 100644 index 0000000..b9c97ae --- /dev/null +++ b/lib/fake_sqs/extended_hash.rb @@ -0,0 +1,17 @@ +require 'deep_merge' + +module FakeSQS + module ExtendedHash + def dotted_to_nested_hash + self.map do |dotted_key, value| + chunk = dotted_key.split('.').reverse.inject(value) do |v, k| + {k => v} + end + end.inject(&:deep_merge) + end + end +end + +class Hash + include FakeSQS::ExtendedHash +end diff --git a/lib/fake_sqs/message.rb b/lib/fake_sqs/message.rb index 225147c..e675523 100644 --- a/lib/fake_sqs/message.rb +++ b/lib/fake_sqs/message.rb @@ -3,13 +3,14 @@ module FakeSQS class Message - attr_reader :body, :id, :md5 + attr_reader :body, :id, :md5, :message_attributes attr_accessor :visibility_timeout def initialize(options = {}) @body = options.fetch("MessageBody") @id = options.fetch("Id") { SecureRandom.uuid } @md5 = options.fetch("MD5") { Digest::MD5.hexdigest(@body) } + @message_attributes = options.fetch("MessageAttribute", []) end def expire! @@ -29,6 +30,7 @@ def attributes "MessageBody" => body, "Id" => id, "MD5" => md5, + "MessageAttribute" => message_attributes } end From eb87e68a5ba60ab850804763576c7e9572a16315 Mon Sep 17 00:00:00 2001 From: Nina Berg Date: Fri, 9 Jan 2015 16:33:06 -0500 Subject: [PATCH 2/3] Removed unused var --- lib/fake_sqs/extended_hash.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fake_sqs/extended_hash.rb b/lib/fake_sqs/extended_hash.rb index b9c97ae..68f2c6d 100644 --- a/lib/fake_sqs/extended_hash.rb +++ b/lib/fake_sqs/extended_hash.rb @@ -4,7 +4,7 @@ module FakeSQS module ExtendedHash def dotted_to_nested_hash self.map do |dotted_key, value| - chunk = dotted_key.split('.').reverse.inject(value) do |v, k| + dotted_key.split('.').reverse.inject(value) do |v, k| {k => v} end end.inject(&:deep_merge) From e4a0ee13eef6ff78aa301b237943f5af734c5927 Mon Sep 17 00:00:00 2001 From: Nina Berg Date: Mon, 12 Jan 2015 14:01:47 -0500 Subject: [PATCH 3/3] Added missing requires to show_output spec --- lib/fake_sqs/show_output.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/fake_sqs/show_output.rb b/lib/fake_sqs/show_output.rb index cd210b3..5873e2b 100644 --- a/lib/fake_sqs/show_output.rb +++ b/lib/fake_sqs/show_output.rb @@ -1,4 +1,6 @@ +require 'rack' require 'rack/request' +require 'yaml' module FakeSQS class ShowOutput