Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature message attributes #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion fake_sqs.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions lib/fake_sqs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 14 additions & 1 deletion lib/fake_sqs/actions/receive_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,27 @@ 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
xml.MessageId message.id
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
Expand Down
2 changes: 1 addition & 1 deletion lib/fake_sqs/actions/send_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions lib/fake_sqs/extended_hash.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'deep_merge'

module FakeSQS
module ExtendedHash
def dotted_to_nested_hash
self.map do |dotted_key, value|
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
4 changes: 3 additions & 1 deletion lib/fake_sqs/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand All @@ -29,6 +30,7 @@ def attributes
"MessageBody" => body,
"Id" => id,
"MD5" => md5,
"MessageAttribute" => message_attributes
}
end

Expand Down
2 changes: 2 additions & 0 deletions lib/fake_sqs/show_output.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require 'rack'
require 'rack/request'
require 'yaml'

module FakeSQS
class ShowOutput
Expand Down