sqs_async 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/sqs.rb CHANGED
@@ -12,17 +12,75 @@ require 'em-http-request'
12
12
  require 'sqs_message'
13
13
  require 'sqs_queue'
14
14
  require 'sqs_attributes'
15
+ require 'sqs_utilities'
16
+ require 'core_ext/hash'
15
17
  require 'logger'
16
18
 
17
19
  module SQS
20
+ include SQS::Utilities
18
21
  attr_accessor :aws_key, :aws_secret, :regions, :default_parameters, :post_options
19
22
 
23
+ def change_message_visibility(options={})
24
+ raise "no Message specified" unless options[:message]
25
+ raise "no new visibility_timeout specified" unless options[:visibility_timeout]
26
+ options.merge!( :receipt_handle => options.delete(:message).receipt_handle,
27
+ :visibility_timeout => options.delete(:visibility_timeout).to_i)
28
+ call_amazon(options)
29
+ end
30
+
31
+ def set_queue_attributes(options={})
32
+ raise "no target queue specified" unless options[:queue]
33
+ %w[ :visibility_timeout :policy :maximum_message_size :message_retention_period ].each do |attr_type|
34
+ if options[attr_type]
35
+ val = options.delete(attr_type)
36
+ options.merge! "Attribute.Name" => camelize(attr_type), "Attribute.Value" => val
37
+ end
38
+ end
39
+
40
+ call_amazon(options)
41
+ end
42
+
43
+ def send_message(options={})
44
+ raise "no object(:message) or string(:message_body) specified" unless (options[:message] || options[:message_body])
45
+ options.merge!( :message_body => options.delete(:message).body ) if options[:message]
46
+ body = options[:message_body]
47
+
48
+ call_amazon(options){ |req| SQSSendMessageResponse.parse(body, req.response) }
49
+ end
50
+
51
+ def add_permission(options={})
52
+ raise "no target queue specified" unless options[:queue]
53
+ raise "no permissions objects specified" unless options[:permissions]
54
+
55
+ [options[:permissions]].flatten.each_with_index do |perm, index|
56
+ ordinal = index+1
57
+ options.merge!(perm.to_params(ordinal)) # last label wins.
58
+ end
59
+
60
+ options.delete(:permissions)
61
+
62
+ call_amazon(options)
63
+ end
64
+
65
+ def remove_permission(options={})
66
+ raise "no target queue specified" unless options[:queue]
67
+ raise "no permissions objects specified" unless options[:permissions]
68
+
69
+ [options[:permissions]].flatten.each_with_index do |perm, index|
70
+ ordinal = index+1
71
+ options.merge!(perm.to_params(ordinal)) # last label wins.
72
+ end
73
+
74
+ options.delete(:permissions)
75
+
76
+ call_amazon(options)
77
+ end
78
+
20
79
  def list_queues(options={})
21
80
  prefix = options.delete(:prefix)
22
81
  match = options.delete(:match)
23
82
 
24
- options.merge!( "Action" => "ListQueues" )
25
- options.merge!( "QueueNamePrefix" => encode(prefix) ) if prefix
83
+ options.merge!( :queue_name_prefix => encode(prefix) ) if prefix
26
84
 
27
85
  call_amazon(options) do |req|
28
86
  queues = SQSQueue.parse(req.response)
@@ -33,42 +91,57 @@ module SQS
33
91
 
34
92
  def receive_message(options={})
35
93
  raise "no target queue specified" unless options[:queue]
36
- options.merge!("Action" => "ReceiveMessage", "MaxNumberOfMessages" => 10 )
94
+ options = { :max_number_of_messages => 10 }.merge(options)
37
95
  call_amazon(options){ |req| SQSMessage.parse(req.response) }
38
96
  end
39
97
 
40
98
  def delete_message(options={})
41
99
  raise "no Message specified" unless options[:message]
42
- options.merge!("Action" => "DeleteMessage", "ReceiptHandle" => options.delete(:message).receipt_handle)
100
+ options.merge!(:receipt_handle => options.delete(:message).receipt_handle)
43
101
  call_amazon(options)
44
102
  end
45
103
 
46
104
  def get_queue_attributes(options={})
47
105
  raise "no target queue specified" unless options[:queue]
48
- options.merge!( "Action" => "GetQueueAttributes", "AttributeName" => "All" )
106
+ options = {:attribute_name => "All" }.merge(options)
49
107
  call_amazon(options){ |req| SQSAttributes.parse(req.response) }
50
108
  end
51
109
 
110
+ def delete_queue(options={})
111
+ raise "no target queue specified" unless options[:queue]
112
+ call_amazon(options)
113
+ end
114
+
115
+ def create_queue(options={})
116
+ raise "no queue name specified" unless options[:queue_name]
117
+ options[:default_visibility_timeout] = 30 unless options[:default_visibility_timeout]
118
+ call_amazon(options){ |req| SQSQueue.parse(req.response) }
119
+ end
120
+
52
121
  private
53
122
 
54
123
  def call_amazon(options)
55
124
  endpoint = (options[:queue] != nil) ? options.delete(:queue).queue_url : "http://" << ( options.delete(:host) || region_host(:us_east) )
56
125
  callbacks = options.delete(:callbacks) || {:success=>nil, :failure =>nil }
57
126
 
127
+ if( who_called_us = caller(0)[1] )
128
+ options = {:action => action_from_caller(who_called_us)}.merge(options)
129
+ end
130
+
131
+ options.amazonize_keys!
132
+
58
133
  params = sign_params( endpoint, options )
59
134
  req = EM::HttpRequest.new("#{endpoint}?#{params}").get
60
- req.callback do |req|
61
- if(req.response.to_s.match(/<ErrorResponse>/i))
62
- on_failure(req, callbacks)
135
+ req.callback do |req_ref|
136
+ if(req_ref.response.to_s.match(/<ErrorResponse>/i))
137
+ on_failure(req_ref, callbacks)
63
138
  else
64
- result = req
65
- result = yield req if block_given?
139
+ result = req_ref
140
+ result = yield req_ref if block_given?
66
141
  callbacks[:success].call(result) if callbacks[:success]
67
142
  end
68
143
  end
69
- req.errback do |req|
70
- on_failure(req, callbacks)
71
- end
144
+ req.errback { |req_ref| on_failure(req_ref, callbacks) }
72
145
  end
73
146
 
74
147
  def on_failure(req, callbacks)
@@ -98,7 +171,7 @@ module SQS
98
171
  end
99
172
 
100
173
  def encoding_exclusions
101
- /[^\w\d\-\_\.\~]/
174
+ /[^\w\d\-_\.~]/
102
175
  end
103
176
 
104
177
  def encode(val)
@@ -110,17 +183,17 @@ module SQS
110
183
  end
111
184
 
112
185
  def regions
113
- @regions ||= Regions
186
+ @regions ||= REGIONS
114
187
  @regions
115
188
  end
116
189
 
117
190
  def default_paramters
118
- @default_paramters ||= Parameters.merge("AWSAccessKeyId" => aws_key)
191
+ @default_paramters ||= PARAMETERS.merge("AWSAccessKeyId" => aws_key)
119
192
  @default_paramters.merge("Expires" => (Time.now+(60*30)).utc.iso8601)
120
193
  end
121
194
 
122
195
  def post_options
123
- @post_options ||= PostOptions
196
+ @post_options ||= POSTOPTIONS
124
197
  @post_options
125
198
  end
126
199
 
@@ -141,7 +214,7 @@ module SQS
141
214
  logger.error(log_msg.join("\n"))
142
215
  end
143
216
 
144
- Regions = {
217
+ REGIONS = {
145
218
  :us_east => { :name => "US-East (Northern Virginia) Region", :uri => "sqs.us-east-1.amazonaws.com"},
146
219
  :us_west => { :name => "US-West (Northern California) Region", :uri => "sqs.us-west-1.amazonaws.com"},
147
220
  :eu => { :name => "EU (Ireland) Region", :uri => "sqs.eu-west-1.amazonaws.com"},
@@ -150,13 +223,13 @@ module SQS
150
223
  }
151
224
 
152
225
 
153
- Parameters = {
226
+ PARAMETERS = {
154
227
  "Version" => "2009-02-01",
155
228
  "SignatureVersion"=>"2",
156
229
  "SignatureMethod"=>"HmacSHA256",
157
230
  }
158
231
 
159
- PostOptions = {
232
+ POSTOPTIONS = {
160
233
  "Content-Type" => "application/x-www-form-urlencoded"
161
234
  }
162
235
  end
@@ -2,8 +2,11 @@ require 'nokogiri'
2
2
  require 'json'
3
3
  require 'base64'
4
4
  require 'ostruct'
5
+ require 'sqs_utilities'
5
6
 
6
7
  class SQSAttributes
8
+ extend SQS::Utilities
9
+
7
10
  attr_accessor :approximate_number_of_messages,
8
11
  :approximate_number_of_messages_not_visible,
9
12
  :visibility_timeout,
@@ -18,21 +21,12 @@ class SQSAttributes
18
21
  doc = Nokogiri::XML(xml)
19
22
  queue_data = SQSAttributes.new
20
23
  doc.search("Attribute").each do |attribute|
21
- meth = underscore(attribute.at("Name").text)
24
+ meth = underscore(attribute.at("Name").text.strip)
22
25
  if queue_data.respond_to? meth.to_sym
23
- queue_data.send "#{meth}=", attribute.at("Value").text
26
+ queue_data.send "#{meth}=", attribute.at("Value").text.strip
24
27
  end
25
28
  end
26
29
  queue_data
27
30
  end
28
31
 
29
- # Taken from ActiveSupport. License information can be found at rubyonrails.org
30
- def self.underscore(camel_cased_word)
31
- word = camel_cased_word.to_s.dup
32
- word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
33
- word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
34
- word.tr!("-", "_")
35
- word.downcase!
36
- word
37
- end
38
32
  end
data/lib/sqs_message.rb CHANGED
@@ -11,9 +11,9 @@ class SQSMessage
11
11
  messages = []
12
12
  doc.search("Message").each do |message_element|
13
13
  s = SQSMessage.new
14
- s.body = message_element.at("Body").text
15
- s.md5_of_body = message_element.at("MD5OfBody").text
16
- s.message_id = message_element.at("MessageId").text
14
+ s.body = message_element.at("Body").text.strip
15
+ s.md5_of_body = message_element.at("MD5OfBody").text.strip
16
+ s.message_id = message_element.at("MessageId").text.strip
17
17
  s.receipt_handle = message_element.at("ReceiptHandle").text.strip
18
18
  messages << s
19
19
  end
@@ -0,0 +1,41 @@
1
+ module SQS
2
+ module Permissions
3
+ def self.all
4
+ "*"
5
+ end
6
+
7
+ def self.send_message
8
+ "SendMessage"
9
+ end
10
+
11
+ def self.receive_message
12
+ "ReceiveMessage"
13
+ end
14
+
15
+ def self.delete_message
16
+ "DeleteMessage"
17
+ end
18
+
19
+ def self.change_message_visibility
20
+ "ChangeMessageVisibility"
21
+ end
22
+
23
+ def self.get_queue_attributes
24
+ "GetQueueAttributes"
25
+ end
26
+ end
27
+ end
28
+
29
+ class SQSPermission
30
+ include SQS::Permissions
31
+ attr_accessor :aws_account_id, :permission
32
+
33
+ def to_params(ordinal=1)
34
+ {
35
+ "Label" => "#{aws_account_id}-#{permission.to_s}",
36
+ "AWSAccountId.#{ordinal}" => aws_account_id,
37
+ "ActionName.#{ordinal}" => permission.to_s
38
+ }
39
+ end
40
+ end
41
+
data/lib/sqs_queue.rb CHANGED
@@ -10,7 +10,7 @@ class SQSQueue
10
10
  queues = []
11
11
  doc.search("QueueUrl").each do |element|
12
12
  s = SQSQueue.new
13
- s.queue_url = URI.parse(element.text)
13
+ s.queue_url = URI.parse(element.text.strip)
14
14
  queues << s
15
15
  end
16
16
  queues
@@ -0,0 +1,19 @@
1
+ require 'nokogiri'
2
+ require 'json'
3
+ require 'base64'
4
+ require 'ostruct'
5
+
6
+ class SQSSendMessageResponse
7
+ attr_accessor :body, :md5_of_body, :message_id, :receipt_handle
8
+
9
+ def self.parse(original_body, xml)
10
+ doc = Nokogiri::XML(xml)
11
+ message = SQSMessage.new
12
+ doc.search("SendMessageResult").each do |message_element|
13
+ message.body = original_body || ""
14
+ message.md5_of_body = message_element.at("MD5OfMessageBody").text.strip
15
+ message.message_id = message_element.at("MessageId").text.strip
16
+ end
17
+ message
18
+ end
19
+ end
@@ -0,0 +1,26 @@
1
+ # These mixins are lovingly taken from active support.
2
+ # License information is available online at rubyonrails.org
3
+
4
+ module SQS
5
+ module Utilities
6
+
7
+ def action_from_caller(first_element_in_caller)
8
+ camelize(first_element_in_caller.scan(/\`(\w+)\'/).flatten.first)
9
+ end
10
+
11
+ def camelize(str)
12
+ str.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
13
+ end
14
+
15
+ def underscore(camel_cased_word)
16
+ word = camel_cased_word.to_s.dup
17
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
18
+ word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
19
+ word.tr!("-", "_")
20
+ word.downcase!
21
+ word
22
+ end
23
+
24
+ end
25
+ end
26
+
@@ -0,0 +1,2 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <AddPermissionResponse><ResponseMetadata><RequestId>9a285199-c8d6-47c2-bdb2-314cb47d599d</RequestId></ResponseMetadata></AddPermissionResponse>
@@ -0,0 +1,2 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <ChangeMessageVisibilityResponse><ResponseMetadata><RequestId>6a7a282a-d013-4a59-aba9-335b0fa48bed</RequestId></ResponseMetadata></ChangeMessageVisibilityResponse>
@@ -0,0 +1,2 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <CreateQueueResponse><CreateQueueResult><QueueUrl>http://sqs.us-east-1.amazonaws.com/123456789012/testQueue</QueueUrl></CreateQueueResult><ResponseMetadata><RequestId>7a62c49f-347e-4fc4-9331-6e8e7a96aa73</RequestId></ResponseMetadata></CreateQueueResponse>
@@ -1,8 +1,2 @@
1
1
  <?xml version="1.0" encoding="UTF-8"?>
2
- <DeleteQueueResponse>
3
- <ResponseMetadata>
4
- <RequestId>
5
- 6fde8d1e-52cd-4581-8cd9-c512f4c64223
6
- </RequestId>
7
- </ResponseMetadata>
8
- </DeleteQueueResponse>
2
+ <DeleteQueueResponse><ResponseMetadata><RequestId>6fde8d1e-52cd-4581-8cd9-c512f4c64223</RequestId></ResponseMetadata></DeleteQueueResponse>
@@ -0,0 +1,2 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <DeleteQueueResponse><ResponseMetadata><RequestId>6fde8d1e-52cd-4581-8cd9-c512f4c64223</RequestId></ResponseMetadata></DeleteQueueResponse>
@@ -1,18 +1,2 @@
1
1
  <?xml version="1.0" encoding="UTF-8"?>
2
- <ErrorResponse>
3
- <Error>
4
- <Type>
5
- Sender
6
- </Type>
7
- <Code>
8
- AuthFailure
9
- </Code>
10
- <Message>
11
- The provided signature is not valid for this access token
12
- </Message>
13
- <Detail/>
14
- </Error>
15
- <RequestId>
16
- ef3aba6a-dc84-4937-91bf-cef2ddd6775a
17
- </RequestId>
18
- </ErrorResponse>
2
+ <ErrorResponse><Error><Type>Sender</Type><Code>AuthFailure</Code><Message>The provided signature is not valid for this access token</Message><Detail/></Error><RequestId>ef3aba6a-dc84-4937-91bf-cef2ddd6775a</RequestId></ErrorResponse>
@@ -1,13 +1,2 @@
1
1
  <?xml version="1.0" encoding="UTF-8"?>
2
- <ListQueuesResponse>
3
- <ListQueuesResult>
4
- <QueueUrl>
5
- http://sqs.us-east-1.amazonaws.com/123456789012/testQueue
6
- </QueueUrl>
7
- </ListQueuesResult>
8
- <ResponseMetadata>
9
- <RequestId>
10
- 725275ae-0b9b-4762-b238-436d7c65a1ac
11
- </RequestId>
12
- </ResponseMetadata>
13
- </ListQueuesResponse>
2
+ <ListQueuesResponse><ListQueuesResult><QueueUrl>http://sqs.us-east-1.amazonaws.com/123456789012/testQueue</QueueUrl></ListQueuesResult><ResponseMetadata><RequestId>725275ae-0b9b-4762-b238-436d7c65a1ac</RequestId></ResponseMetadata></ListQueuesResponse>
@@ -1,40 +1,2 @@
1
1
  <?xml version="1.0" encoding="UTF-8"?>
2
- <ReceiveMessageResponse>
3
- <ReceiveMessageResult>
4
- <Message>
5
- <MessageId>
6
- 5fea7756-0ea4-451a-a703-a558b933e274
7
- </MessageId>
8
- <ReceiptHandle>
9
- MbZj6wDWli+JvwwJaBV+3dcjk2YW2vA3+STFFljTM8tJJg6HRG6PYSasuWXPJB+Cw
10
- Lj1FjgXUv1uSj1gUPAWV66FU/WeR4mq2OKpEGYWbnLmpRCJVAyeMjeU5ZBdtcQ+QE
11
- auMZc8ZRv37sIW2iJKq3M9MFx1YvV11A2x/KSbkJ0=
12
- </ReceiptHandle>
13
- <MD5OfBody>
14
- fafb00f5732ab283681e124bf8747ed1
15
- </MD5OfBody>
16
- <Body>This is a test message</Body>
17
- <Attribute>
18
- <Name>SenderId</Name>
19
- <Value>195004372649</Value>
20
- </Attribute>
21
- <Attribute>
22
- <Name>SentTimestamp</Name>
23
- <Value>1238099229000</Value>
24
- </Attribute>
25
- <Attribute>
26
- <Name>ApproximateReceiveCount</Name>
27
- <Value>5</Value>
28
- </Attribute>
29
- <Attribute>
30
- <Name>ApproximateFirstReceiveTimestamp</Name>
31
- <Value>1250700979248</Value>
32
- </Attribute>
33
- </Message>
34
- </ReceiveMessageResult>
35
- <ResponseMetadata>
36
- <RequestId>
37
- b6633655-283d-45b4-aee4-4e84e0ae6afa
38
- </RequestId>
39
- </ResponseMetadata>
40
- </ReceiveMessageResponse>
2
+ <ReceiveMessageResponse><ReceiveMessageResult><Message><MessageId>5fea7756-0ea4-451a-a703-a558b933e274</MessageId><ReceiptHandle>MbZj6wDWli+JvwwJaBV+3dcjk2YW2vA3+STFFljTM8tJJg6HRG6PYSasuWXPJB+CwLj1FjgXUv1uSj1gUPAWV66FU/WeR4mq2OKpEGYWbnLmpRCJVAyeMjeU5ZBdtcQ+QEauMZc8ZRv37sIW2iJKq3M9MFx1YvV11A2x/KSbkJ0=</ReceiptHandle><MD5OfBody>fafb00f5732ab283681e124bf8747ed1</MD5OfBody><Body>This is a test message</Body><Attribute><Name>SenderId</Name><Value>195004372649</Value></Attribute><Attribute><Name>SentTimestamp</Name><Value>1238099229000</Value></Attribute><Attribute><Name>ApproximateReceiveCount</Name><Value>5</Value></Attribute><Attribute><Name>ApproximateFirstReceiveTimestamp</Name><Value>1250700979248</Value></Attribute></Message></ReceiveMessageResult><ResponseMetadata><RequestId>b6633655-283d-45b4-aee4-4e84e0ae6afa</RequestId></ResponseMetadata></ReceiveMessageResponse>
@@ -0,0 +1,2 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <RemovePermissionResponse><ResponseMetadata><RequestId>f8bdb362-6616-42c0-977a-ce9a8bcce3bb</RequestId></ResponseMetadata></RemovePermissionResponse>
@@ -0,0 +1,2 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <SendMessageResponse><SendMessageResult><MD5OfMessageBody>fafb00f5732ab283681e124bf8747ed1</MD5OfMessageBody><MessageId>5fea7756-0ea4-451a-a703-a558b933e274</MessageId></SendMessageResult><ResponseMetadata><RequestId>27daac76-34dd-47df-bd01-1f6e873584a0</RequestId></ResponseMetadata></SendMessageResponse>
@@ -0,0 +1,2 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <SetQueueAttributesResponse><ResponseMetadata><RequestId>e5cca473-4fc0-4198-a451-8abb94d02c75</RequestId></ResponseMetadata></SetQueueAttributesResponse>
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  ENV["BUBBLE_ENV"] = "test"
2
2
 
3
3
  $:.unshift(File.dirname(__FILE__))
4
- %w{ ../ lib config vendor/sqs_async/lib }.each do |d|
4
+ %w{ ../ lib config vendor/sqs_async/lib lib/core_ext }.each do |d|
5
5
  $:.unshift(File.dirname(__FILE__)+"/#{d}/")
6
6
  end
7
7
 
@@ -10,11 +10,7 @@ require 'rspec'
10
10
  require 'timecop'
11
11
  require 'em-http-request'
12
12
 
13
- require 'sqs'
14
- require 'sqs_message'
15
- require 'sqs_queue'
16
- require 'sqs_attributes'
17
-
13
+ require 'sqs_async'
18
14
 
19
15
  RSpec.configure do |config|
20
16
  config.mock_framework = :mocha
@@ -32,11 +28,11 @@ module EventMachine
32
28
  end
33
29
 
34
30
  def get *args
35
- return self
31
+ self
36
32
  end
37
33
 
38
34
  def post *args
39
- return self
35
+ self
40
36
  end
41
37
 
42
38
  def callback &block
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe "SQSAttributes" do
4
- describe ".parse" do
4
+ context ".parse" do
5
5
  let(:attributes_obj) { SQSAttributes.parse(xml_fixture(:queue_attributes)) }
6
6
 
7
7
  it "returns SQSAttributes objects" do
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe "SQSMessage" do
4
- describe ".parse" do
4
+ context ".parse" do
5
5
  let(:messages) { SQSMessage.parse(xml_fixture(:receive_message)) }
6
6
 
7
7
  it "returns SQSMessage objects" do
@@ -13,8 +13,8 @@ describe "SQSMessage" do
13
13
  message = messages.first
14
14
  message.body.should == "This is a test message"
15
15
  message.receipt_handle.should match /MbZj6wDWli.+?/im
16
- message.md5_of_body.should == "\n fafb00f5732ab283681e124bf8747ed1\n "
17
- message.message_id.should == "\n 5fea7756-0ea4-451a-a703-a558b933e274\n "
16
+ message.md5_of_body.should == "fafb00f5732ab283681e124bf8747ed1"
17
+ message.message_id.should == "5fea7756-0ea4-451a-a703-a558b933e274"
18
18
  end
19
19
  end
20
20
  end
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
  require 'sqs_message'
3
3
 
4
4
  describe "SQSQueue" do
5
- describe ".parse" do
5
+ context ".parse" do
6
6
  let(:queues) { SQSQueue.parse(xml_fixture(:list_queues)) }
7
7
 
8
8
  it "returns SQSQueue objects" do
data/spec/sqs_spec.rb CHANGED
@@ -46,6 +46,75 @@ describe "SQS" do
46
46
  end
47
47
 
48
48
  context "calls Amazon Endpoints asynchronously to" do
49
+
50
+ it "change_message_visibility" do
51
+ mock_obj = mock()
52
+ mock_obj.expects(:call).once
53
+ client.change_message_visibility(
54
+ :queue => queue,
55
+ :message => message,
56
+ :visibility_timeout => Time.now.to_i + (30*60),
57
+ :callbacks => { :success => mock_obj }
58
+ )
59
+ EM::HttpRequest.succeed(EM::MockResponse.new(xml_fixture(:change_message_visibility)))
60
+ end
61
+
62
+ it "set_queue_attributes" do
63
+ mock_obj = mock()
64
+ mock_obj.expects(:call).once
65
+ client.set_queue_attributes(
66
+ :queue => queue,
67
+ :visibility_timeout => Time.now.to_i + (30*60),
68
+ :callbacks => { :success => mock_obj }
69
+ )
70
+ EM::HttpRequest.succeed(EM::MockResponse.new(xml_fixture(:set_queue_attributes)))
71
+ end
72
+
73
+ context "putting a message on the queue" do
74
+ it "send_message with SQSMessage object" do
75
+ msg = SQSMessage.new
76
+ msg.body = "foo"
77
+ client.send_message(
78
+ :queue => queue,
79
+ :message => msg,
80
+ :callbacks => {
81
+ :success => lambda { |sqs_message_obj|
82
+ sqs_message_obj.body.should == "foo"
83
+ sqs_message_obj.md5_of_body.should_not be_nil
84
+ }
85
+ }
86
+ )
87
+ EM::HttpRequest.succeed(EM::MockResponse.new(xml_fixture(:send_message)))
88
+ end
89
+
90
+ it "send_message with message body" do
91
+ client.send_message(
92
+ :queue => queue,
93
+ :message_body => "foo",
94
+ :callbacks => {
95
+ :success => lambda { |sqs_message_obj|
96
+ sqs_message_obj.body.should == "foo"
97
+ sqs_message_obj.md5_of_body.should_not be_nil
98
+ }
99
+ }
100
+ )
101
+ EM::HttpRequest.succeed(EM::MockResponse.new(xml_fixture(:send_message)))
102
+ end
103
+ end
104
+
105
+ it "creates a queue" do
106
+ client.create_queue(
107
+ :queue_name => "testQueue",
108
+ :callbacks => {
109
+ :success => lambda{|queues|
110
+ queues.length.should == 1
111
+ queues.first.queue_url.to_s.should == "http://sqs.us-east-1.amazonaws.com/123456789012/testQueue"
112
+ }
113
+ }
114
+ )
115
+ EM::HttpRequest.succeed(EM::MockResponse.new(xml_fixture(:create_queue)))
116
+ end
117
+
49
118
  it "list available queues" do
50
119
  client.list_queues(
51
120
  :callbacks => {
@@ -58,6 +127,44 @@ describe "SQS" do
58
127
  EM::HttpRequest.succeed(EM::MockResponse.new(xml_fixture(:list_queues)))
59
128
  end
60
129
 
130
+ context "permissions management" do
131
+ let(:permissions) do
132
+ leon = SQSPermission.new
133
+ leon.aws_account_id = "a12digitcode"
134
+ leon.permission = SQS::Permissions.all
135
+
136
+ john = SQSPermission.new
137
+ john.aws_account_id = "b12digitcode"
138
+ john.permission = SQS::Permissions.send_message
139
+
140
+ [leon, john]
141
+ end
142
+
143
+ let(:mock_closure) do
144
+ mock_closure = mock()
145
+ mock_closure.expects(:call).once
146
+ mock_closure
147
+ end
148
+
149
+ it "adds permissions to a queue" do
150
+ client.add_permission(
151
+ :queue => queue,
152
+ :permissions => permissions,
153
+ :callbacks => { :success => mock_closure }
154
+ )
155
+ EM::HttpRequest.succeed(EM::MockResponse.new(xml_fixture(:add_permission)))
156
+ end
157
+
158
+ it "adds permissions to a queue" do
159
+ client.remove_permission(
160
+ :queue => queue,
161
+ :permissions => permissions,
162
+ :callbacks => { :success => mock_closure }
163
+ )
164
+ EM::HttpRequest.succeed(EM::MockResponse.new(xml_fixture(:remove_permission)))
165
+ end
166
+ end
167
+
61
168
  it "pull a message from the queue" do
62
169
  queue.queue_url = URI.parse("http://sqs.us-east-1.amazonaws.com/123456789012/testQueue")
63
170
  client.receive_message(
@@ -75,7 +182,7 @@ describe "SQS" do
75
182
  end
76
183
 
77
184
  it "delete a message from the queue" do
78
- mock_obj = mock();
185
+ mock_obj = mock()
79
186
  mock_obj.expects(:call).once
80
187
  client.delete_message(
81
188
  :queue => queue,
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe "SQS::Utilities" do
4
+ include SQS::Utilities # i <3 ruby. :)
5
+
6
+ it "amazonizes the keys of a hash" do
7
+ hash = {:queue_name =>"foo", :default_visibility_timeout => 30}
8
+ hash.amazonize_keys!
9
+ hash["QueueName"].should == "foo"
10
+ end
11
+
12
+ it "infers an amazonized action from the calling context" do
13
+ def this_would_be_the_same_as_an_action_name
14
+ sample_target
15
+ end
16
+
17
+ def sample_target
18
+ action_from_caller(caller(0)[1]).should == "ThisWouldBeTheSameAsAnActionName"
19
+ end
20
+
21
+ this_would_be_the_same_as_an_action_name
22
+ end
23
+ end
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sqs_async
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
5
4
  prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 1
10
- version: 0.0.1
5
+ version: 0.0.2
11
6
  platform: ruby
12
7
  authors:
13
8
  - EdgeCase <contact@edgecase.com>
@@ -17,7 +12,7 @@ autorequire:
17
12
  bindir: bin
18
13
  cert_chain: []
19
14
 
20
- date: 2011-05-31 00:00:00 -04:00
15
+ date: 2011-06-03 00:00:00 -04:00
21
16
  default_executable:
22
17
  dependencies:
23
18
  - !ruby/object:Gem::Dependency
@@ -28,13 +23,6 @@ dependencies:
28
23
  requirements:
29
24
  - - ~>
30
25
  - !ruby/object:Gem::Version
31
- hash: 62196357
32
- segments:
33
- - 1
34
- - 0
35
- - 0
36
- - beta
37
- - 3
38
26
  version: 1.0.0.beta.3
39
27
  type: :runtime
40
28
  version_requirements: *id001
@@ -46,13 +34,6 @@ dependencies:
46
34
  requirements:
47
35
  - - ~>
48
36
  - !ruby/object:Gem::Version
49
- hash: 62196363
50
- segments:
51
- - 1
52
- - 0
53
- - 0
54
- - beta
55
- - 4
56
37
  version: 1.0.0.beta.4
57
38
  type: :runtime
58
39
  version_requirements: *id002
@@ -64,9 +45,6 @@ dependencies:
64
45
  requirements:
65
46
  - - ">="
66
47
  - !ruby/object:Gem::Version
67
- hash: 3
68
- segments:
69
- - 0
70
48
  version: "0"
71
49
  type: :runtime
72
50
  version_requirements: *id003
@@ -78,9 +56,6 @@ dependencies:
78
56
  requirements:
79
57
  - - ">="
80
58
  - !ruby/object:Gem::Version
81
- hash: 3
82
- segments:
83
- - 0
84
59
  version: "0"
85
60
  type: :runtime
86
61
  version_requirements: *id004
@@ -92,9 +67,6 @@ dependencies:
92
67
  requirements:
93
68
  - - ">="
94
69
  - !ruby/object:Gem::Version
95
- hash: 3
96
- segments:
97
- - 0
98
70
  version: "0"
99
71
  type: :runtime
100
72
  version_requirements: *id005
@@ -106,11 +78,6 @@ dependencies:
106
78
  requirements:
107
79
  - - ">="
108
80
  - !ruby/object:Gem::Version
109
- hash: 23
110
- segments:
111
- - 2
112
- - 6
113
- - 0
114
81
  version: 2.6.0
115
82
  type: :development
116
83
  version_requirements: *id006
@@ -122,9 +89,6 @@ dependencies:
122
89
  requirements:
123
90
  - - ">="
124
91
  - !ruby/object:Gem::Version
125
- hash: 3
126
- segments:
127
- - 0
128
92
  version: "0"
129
93
  type: :development
130
94
  version_requirements: *id007
@@ -136,9 +100,6 @@ dependencies:
136
100
  requirements:
137
101
  - - ">="
138
102
  - !ruby/object:Gem::Version
139
- hash: 3
140
- segments:
141
- - 0
142
103
  version: "0"
143
104
  type: :development
144
105
  version_requirements: *id008
@@ -150,9 +111,6 @@ dependencies:
150
111
  requirements:
151
112
  - - ">="
152
113
  - !ruby/object:Gem::Version
153
- hash: 3
154
- segments:
155
- - 0
156
114
  version: "0"
157
115
  type: :development
158
116
  version_requirements: *id009
@@ -164,9 +122,6 @@ dependencies:
164
122
  requirements:
165
123
  - - ">="
166
124
  - !ruby/object:Gem::Version
167
- hash: 3
168
- segments:
169
- - 0
170
125
  version: "0"
171
126
  type: :development
172
127
  version_requirements: *id010
@@ -182,17 +137,28 @@ files:
182
137
  - lib/sqs.rb
183
138
  - lib/sqs_attributes.rb
184
139
  - lib/sqs_message.rb
140
+ - lib/sqs_permission.rb
185
141
  - lib/sqs_queue.rb
142
+ - lib/sqs_send_message_response.rb
143
+ - lib/sqs_utilities.rb
144
+ - spec/fixtures/add_permission.xml
145
+ - spec/fixtures/change_message_visibility.xml
146
+ - spec/fixtures/create_queue.xml
186
147
  - spec/fixtures/delete_message.xml
148
+ - spec/fixtures/delete_queue.xml
187
149
  - spec/fixtures/error_response.xml
188
150
  - spec/fixtures/list_queues.xml
189
151
  - spec/fixtures/queue_attributes.xml
190
152
  - spec/fixtures/receive_message.xml
153
+ - spec/fixtures/remove_permission.xml
154
+ - spec/fixtures/send_message.xml
155
+ - spec/fixtures/set_queue_attributes.xml
191
156
  - spec/spec_helper.rb
192
157
  - spec/sqs_attributes_spec.rb
193
158
  - spec/sqs_message_spec.rb
194
159
  - spec/sqs_queue_spec.rb
195
160
  - spec/sqs_spec.rb
161
+ - spec/sqs_utilities_spec.rb
196
162
  has_rdoc: true
197
163
  homepage: https://github.com/edgecase/sqs_async
198
164
  licenses: []
@@ -207,34 +173,36 @@ required_ruby_version: !ruby/object:Gem::Requirement
207
173
  requirements:
208
174
  - - ">="
209
175
  - !ruby/object:Gem::Version
210
- hash: 3
211
- segments:
212
- - 0
213
176
  version: "0"
214
177
  required_rubygems_version: !ruby/object:Gem::Requirement
215
178
  none: false
216
179
  requirements:
217
180
  - - ">="
218
181
  - !ruby/object:Gem::Version
219
- hash: 3
220
- segments:
221
- - 0
222
182
  version: "0"
223
183
  requirements: []
224
184
 
225
185
  rubyforge_project: sqs_async
226
- rubygems_version: 1.5.0
186
+ rubygems_version: 1.6.2
227
187
  signing_key:
228
188
  specification_version: 3
229
189
  summary: Non-Blocking SQS library.
230
190
  test_files:
191
+ - spec/fixtures/add_permission.xml
192
+ - spec/fixtures/change_message_visibility.xml
193
+ - spec/fixtures/create_queue.xml
231
194
  - spec/fixtures/delete_message.xml
195
+ - spec/fixtures/delete_queue.xml
232
196
  - spec/fixtures/error_response.xml
233
197
  - spec/fixtures/list_queues.xml
234
198
  - spec/fixtures/queue_attributes.xml
235
199
  - spec/fixtures/receive_message.xml
200
+ - spec/fixtures/remove_permission.xml
201
+ - spec/fixtures/send_message.xml
202
+ - spec/fixtures/set_queue_attributes.xml
236
203
  - spec/spec_helper.rb
237
204
  - spec/sqs_attributes_spec.rb
238
205
  - spec/sqs_message_spec.rb
239
206
  - spec/sqs_queue_spec.rb
240
207
  - spec/sqs_spec.rb
208
+ - spec/sqs_utilities_spec.rb