Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Krekoten Marjan
committed
Aug 16, 2010
0 parents
commit cc48e09
Showing
25 changed files
with
1,242 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
coverage | ||
.bundle | ||
*.swp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
source :gemcutter | ||
|
||
gem 'ruby-hmac', :require => false | ||
gem 'xml-simple' | ||
|
||
group :test do | ||
gem 'rspec' | ||
gem 'rr' | ||
gem 'rcov' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
GEM | ||
remote: http://rubygems.org/ | ||
specs: | ||
rcov (0.9.8) | ||
rr (0.10.11) | ||
rspec (1.3.0) | ||
ruby-hmac (0.4.0) | ||
xml-simple (1.0.12) | ||
|
||
PLATFORMS | ||
ruby | ||
|
||
DEPENDENCIES | ||
rcov | ||
rr | ||
rspec | ||
ruby-hmac | ||
xml-simple |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require 'rubygems' | ||
require 'spec/rake/spectask' | ||
|
||
require 'rake' | ||
require 'rake/testtask' | ||
require 'rake/rdoctask' | ||
|
||
Spec::Rake::SpecTask.new do |t| | ||
t.spec_files = FileList['spec/**/*_spec.rb'] | ||
t.spec_opts = ['--format', 'profile', '--color', '-b'] | ||
end | ||
|
||
desc "Default task is to run specs" | ||
task :default => :spec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
$: << File.expand_path(File.join(File.dirname(__FILE__), '..')) | ||
require 'rubygems' | ||
require 'lib/simple_qs' | ||
|
||
unless ENV['AWS_ACCESS_KEY_ID'] && ENV['AWS_SECRET_ACCESS_KEY'] && ENV['AWS_ACCOUNT_ID'] | ||
abort "Please set AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_ACCOUNT_ID environment variables" | ||
end | ||
|
||
SimpleQS.access_key_id = ENV['AWS_ACCESS_KEY_ID'] | ||
SimpleQS.secret_access_key = ENV['AWS_SECRET_ACCESS_KEY'] | ||
SimpleQS.account_id = ENV['AWS_ACCOUNT_ID'] | ||
|
||
@queue = SimpleQS::Queue.create('testMessageBus') | ||
|
||
while true do | ||
message = STDIN.gets.chomp | ||
@queue.send_message(message) | ||
if message == 'quit' | ||
puts "Quitting..." | ||
break | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
$: << File.expand_path(File.join(File.dirname(__FILE__), '..')) | ||
require 'rubygems' | ||
require 'lib/simple_qs' | ||
|
||
unless ENV['AWS_ACCESS_KEY_ID'] && ENV['AWS_SECRET_ACCESS_KEY'] && ENV['AWS_ACCOUNT_ID'] | ||
abort "Please set AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_ACCOUNT_ID environment variables" | ||
end | ||
|
||
SimpleQS.access_key_id = ENV['AWS_ACCESS_KEY_ID'] | ||
SimpleQS.secret_access_key = ENV['AWS_SECRET_ACCESS_KEY'] | ||
SimpleQS.account_id = ENV['AWS_ACCOUNT_ID'] | ||
|
||
@queue = SimpleQS::Queue.create('testMessageBus') | ||
|
||
while true do | ||
messages = @queue.receive_messages | ||
unless messages.empty? | ||
message = messages.first | ||
message.delete | ||
if message.body == 'quit' | ||
puts "Quitting..." | ||
break | ||
else | ||
puts "[MESSAGE]: #{message.body}" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'rubygems' | ||
|
||
$: << File.join(File.dirname(__FILE__)) | ||
|
||
module SimpleQS | ||
|
||
API_VERSION = '2009-02-01' | ||
|
||
autoload :Message, 'simple_qs/message' | ||
autoload :Queue, 'simple_qs/queue' | ||
autoload :Request, 'simple_qs/request' | ||
autoload :Responce, 'simple_qs/responce' | ||
|
||
class << self | ||
attr_accessor :access_key_id, :secret_access_key | ||
|
||
def account_id= value | ||
@account_id = value.gsub(/[^0-9]/, '') | ||
end | ||
attr_reader :account_id | ||
end | ||
end | ||
|
||
require 'version' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
module SimpleQS | ||
class Message | ||
|
||
class DoubleSendError < StandardError; end | ||
class NotReceivedError < StandardError; end | ||
|
||
attr_accessor :queue | ||
attr_reader :message_id, :receipt_handle, :body, :md5_of_body | ||
attr_reader :sender_id, :sent_timestamp, :approximate_receive_count, :approximate_first_receive_timestamp | ||
|
||
def initialize(queue, params = nil) | ||
@queue = queue | ||
|
||
@body = params if params.class == String | ||
_from_responce(params) if params.class == SimpleQS::Responce | ||
_from_hash(params) if params.class == Hash | ||
end | ||
|
||
def send | ||
raise(DoubleSendError, "Cann't send already sent message. Use resend() method.") if message_id | ||
raise(DoubleSendError, "Cann't send received message. Use resend() method.") if receipt_handle | ||
|
||
params = { | ||
'Action' => 'SendMessage', | ||
'MessageBody' => body | ||
} | ||
request = queue.build_request(:post, params) | ||
responce = request.perform | ||
raise(responce.to_error) unless responce.successful? | ||
_from_responce(responce) | ||
|
||
self | ||
end | ||
|
||
def resend | ||
dup.send | ||
end | ||
|
||
def delete | ||
raise NotReceivedError, "Cann't delete message that was not received" unless receipt_handle | ||
params = { | ||
'Action' => 'DeleteMessage', | ||
'ReceiptHandle' => receipt_handle | ||
} | ||
request = queue.build_request(:get, params) | ||
responce = request.perform | ||
raise responce.to_error unless responce.successful? | ||
end | ||
alias_method :destroy, :delete | ||
|
||
def change_visibility visibility_timeout | ||
SimpleQS::Queue.check_visibility_timeout(visibility_timeout) | ||
raise NotReceivedError, "Cann't change visibility timeout for message that was not received" unless receipt_handle | ||
|
||
params = { | ||
'Action' => 'ChangeMessageVisibility', | ||
'ReceiptHandle' => receipt_handle, | ||
'VisibilityTimeout' => visibility_timeout | ||
} | ||
|
||
request = queue.build_request(:get, params) | ||
responce = request.perform | ||
|
||
raise responce.to_error unless responce.successful? | ||
end | ||
|
||
def dup | ||
self.class.new(queue, body) | ||
end | ||
|
||
def ==(other) | ||
message_id == other.message_id && receipt_handle == other.receipt_handle | ||
end | ||
|
||
class << self | ||
def send(queue, message_body) | ||
new(queue, message_body).send | ||
end | ||
|
||
def receive(queue, attributes = nil, max_number_of_messages = nil, visibility_timeout = nil) | ||
|
||
SimpleQS::Queue.check_visibility_timeout(visibility_timeout) if visibility_timeout | ||
raise(ArgumentError, "Maximum number of messages should be in 1..10 range") if max_number_of_messages && !(1..10).include?(max_number_of_messages) | ||
|
||
params = { | ||
'Action' => 'ReceiveMessage' | ||
} | ||
|
||
if attributes | ||
attributes = [attributes] unless attributes.class == Array | ||
attributes.uniq! | ||
unless (attributes - [:All, :SenderId, :SentTimestamp, :ApproximateReceiveCount, :ApproximateFirstReceiveTimestamp]).empty? | ||
raise ArgumentError,\ | ||
"Allowed attributes: :All, :SenderId, :SentTimestamp, :ApproximateReceiveCount, :ApproximateFirstReceiveTimestamp" | ||
end | ||
attributes.each_index do |i| | ||
params["AttributeName.#{i + 1}"] = attributes[i] | ||
end | ||
end | ||
params['MaxNumberOfMessages'] = max_number_of_messages if max_number_of_messages | ||
params['VisibilityTimeout'] = visibility_timeout if visibility_timeout | ||
|
||
request = queue.build_request(:get, params) | ||
responce = request.perform | ||
if responce.respond_to?(:message) | ||
messages = (responce.message.class == Array ? responce.message : [responce.message]) | ||
messages.map {|message| new(queue, message)} | ||
else | ||
[] | ||
end | ||
end | ||
end | ||
|
||
protected | ||
|
||
def _from_responce(responce) | ||
@message_id = responce.message_id | ||
@md5_of_body = responce.md5_of_message_body | ||
end | ||
|
||
def _from_hash(message) | ||
attributes = message.delete('Attribute') | ||
attributes.each do |attr| | ||
message[attr['Name']] = attr['Value'] | ||
end if attributes | ||
message.each do |key, value| | ||
key = key.gsub(/([A-Z]+)/, '_\1').downcase.gsub(/^_/, '') | ||
instance_variable_set("@#{key}".to_sym, value) if respond_to?(key.to_sym) | ||
end | ||
end | ||
|
||
end | ||
end |
Oops, something went wrong.