kerryb-right_aws 1.7.6 → 1.10.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -33,11 +33,14 @@ module RightAws
33
33
  DEFAULT_PORT = 443
34
34
  DEFAULT_PROTOCOL = 'https'
35
35
  API_VERSION = '2007-11-07'
36
+ DEFAULT_NIL_REPRESENTATION = 'nil'
36
37
 
37
38
  @@bench = AwsBenchmarkingBlock.new
38
39
  def self.bench_xml; @@bench.xml; end
39
40
  def self.bench_sdb; @@bench.service; end
40
41
 
42
+ attr_reader :last_query_expression
43
+
41
44
  # Creates new RightSdb instance.
42
45
  #
43
46
  # Params:
@@ -46,7 +49,8 @@ module RightAws
46
49
  # :protocol => 'https' # Amazon service protocol: 'http' or 'https'(default)
47
50
  # :signature_version => '0' # The signature version : '0' or '1'(default)
48
51
  # :multi_thread => true|false # Multi-threaded (connection per each thread): true or false(default)
49
- # :logger => Logger Object} # Logger instance: logs to STDOUT if omitted }
52
+ # :logger => Logger Object # Logger instance: logs to STDOUT if omitted
53
+ # :nil_representation => 'mynil'} # interpret Ruby nil as this string value; i.e. use this string in SDB to represent Ruby nils (default is the string 'nil')
50
54
  #
51
55
  # Example:
52
56
  #
@@ -55,6 +59,8 @@ module RightAws
55
59
  # see: http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/
56
60
  #
57
61
  def initialize(aws_access_key_id=nil, aws_secret_access_key=nil, params={})
62
+ @nil_rep = params[:nil_representation] ? params[:nil_representation] : DEFAULT_NIL_REPRESENTATION
63
+ params.delete(:nil_representation)
58
64
  init({ :name => 'SDB',
59
65
  :default_host => ENV['SDB_URL'] ? URI.parse(ENV['SDB_URL']).host : DEFAULT_HOST,
60
66
  :default_port => ENV['SDB_URL'] ? URI.parse(ENV['SDB_URL']).port : DEFAULT_PORT,
@@ -69,38 +75,35 @@ module RightAws
69
75
  #-----------------------------------------------------------------
70
76
  def generate_request(action, params={}) #:nodoc:
71
77
  # remove empty params from request
72
- params.delete_if {|key,value| value.blank? }
73
- params_string = params.to_a.collect{|key,val| key + "=#{CGI::escape(val.to_s)}" }.join("&")
78
+ params.delete_if {|key,value| value.nil? }
79
+ #params_string = params.to_a.collect{|key,val| key + "=#{CGI::escape(val.to_s)}" }.join("&")
74
80
  # prepare service data
75
- service_hash = {"Action" => action,
76
- "AWSAccessKeyId" => @aws_access_key_id,
77
- "Version" => API_VERSION,
78
- "Timestamp" => Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S.000Z"),
79
- "SignatureVersion" => signature_version }
81
+ service = '/'
82
+ service_hash = {"Action" => action,
83
+ "AWSAccessKeyId" => @aws_access_key_id,
84
+ "Version" => API_VERSION }
80
85
  service_hash.update(params)
81
- # prepare string to sight
82
- string_to_sign = case signature_version
83
- when '0' : service_hash["Action"] + service_hash["Timestamp"]
84
- when '1' : service_hash.sort{|a,b| (a[0].to_s.downcase)<=>(b[0].to_s.downcase)}.to_s
85
- end
86
- service_hash.update('Signature' => AwsUtils::sign(@aws_secret_access_key, string_to_sign))
87
- service_string = service_hash.to_a.collect{|key,val| key + "=#{CGI::escape(val.to_s)}" }.join("&")
86
+ service_params = signed_service_params(@aws_secret_access_key, service_hash, :get, @params[:server], service)
88
87
  #
89
88
  # use POST method if the length of the query string is too large
90
89
  # see http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/MakingRESTRequests.html
91
- if (service_string + params_string).size > 2000
92
- request = Net::HTTP::Post.new("/?#{service_string}")
93
- request.body = params_string
90
+ if service_params.size > 2000
91
+ if signature_version == '2'
92
+ # resign the request because HTTP verb is included into signature
93
+ service_params = signed_service_params(@aws_secret_access_key, service_hash, :post, @params[:server], service)
94
+ end
95
+ request = Net::HTTP::Post.new(service)
96
+ request.body = service_params
97
+ request['Content-Type'] = 'application/x-www-form-urlencoded'
94
98
  else
95
- params_string = "&#{params_string}" unless params_string.blank?
96
- request = Net::HTTP::Get.new("/?#{service_string}#{params_string}")
99
+ request = Net::HTTP::Get.new("#{service}?#{service_params}")
97
100
  end
98
101
  # prepare output hash
99
102
  { :request => request,
100
103
  :server => @params[:server],
101
104
  :port => @params[:port],
102
105
  :protocol => @params[:protocol],
103
- :proxy => @params[:proxy] }
106
+ :proxy => @params[:proxy] }
104
107
  end
105
108
 
106
109
  # Sends request to Amazon and parses the response
@@ -117,18 +120,20 @@ module RightAws
117
120
  result = {}
118
121
  if attributes
119
122
  idx = 0
123
+ skip_values = attributes.is_a?(Array)
120
124
  attributes.each do |attribute, values|
121
125
  # set replacement attribute
122
126
  result["Attribute.#{idx}.Replace"] = 'true' if replace
123
127
  # pack Name/Value
124
- unless values.blank?
125
- values.to_a.each do |value|
128
+ unless values.nil?
129
+ Array(values).each do |value|
126
130
  result["Attribute.#{idx}.Name"] = attribute
127
- result["Attribute.#{idx}.Value"] = value
131
+ result["Attribute.#{idx}.Value"] = ruby_to_sdb(value) unless skip_values
128
132
  idx += 1
129
133
  end
130
134
  else
131
135
  result["Attribute.#{idx}.Name"] = attribute
136
+ result["Attribute.#{idx}.Value"] = ruby_to_sdb(nil) unless skip_values
132
137
  idx += 1
133
138
  end
134
139
  end
@@ -145,22 +150,55 @@ module RightAws
145
150
  %Q{'#{value.to_s.gsub(/(['\\])/){ "\\#{$1}" }}'} if value
146
151
  end
147
152
 
153
+ # Convert a Ruby language value to a SDB value by replacing Ruby nil with the user's chosen string representation of nil.
154
+ # Non-nil values are unaffected by this filter.
155
+ def ruby_to_sdb(value)
156
+ value.nil? ? @nil_rep : value
157
+ end
158
+
159
+ # Convert a SDB value to a Ruby language value by replacing the user's chosen string representation of nil with Ruby nil.
160
+ # Values are unaffected by this filter unless they match the nil representation exactly.
161
+ def sdb_to_ruby(value)
162
+ value.eql?(@nil_rep) ? nil : value
163
+ end
164
+
165
+ # Convert select and query_with_attributes responses to a Ruby language values by replacing the user's chosen string representation of nil with Ruby nil.
166
+ # (This method affects on a passed response value)
167
+ def select_response_to_ruby(response) #:nodoc:
168
+ response[:items].each_with_index do |item, idx|
169
+ item.each do |key, attributes|
170
+ attributes.each do |name, values|
171
+ values.collect! { |value| sdb_to_ruby(value) }
172
+ end
173
+ end
174
+ end
175
+ response
176
+ end
177
+
148
178
  # Create query expression from an array.
149
179
  # (similar to ActiveRecord::Base#find using :conditions => ['query', param1, .., paramN])
150
180
  #
151
181
  def query_expression_from_array(params) #:nodoc:
152
- unless params.blank?
153
- query = params.shift.to_s
154
- query.gsub(/(\\)?(\?)/) do
155
- if $1 # if escaped '\?' is found - replace it by '?' without backslash
156
- "?"
157
- else # well, if no backslash precedes '?' then replace it by next param from the list
158
- escape(params.shift)
159
- end
182
+ return '' if params.blank?
183
+ query = params.shift.to_s
184
+ query.gsub(/(\\)?(\?)/) do
185
+ if $1 # if escaped '\?' is found - replace it by '?' without backslash
186
+ "?"
187
+ else # well, if no backslash precedes '?' then replace it by next param from the list
188
+ escape(params.shift)
160
189
  end
161
190
  end
162
191
  end
163
-
192
+
193
+ def query_expression_from_hash(hash)
194
+ return '' if hash.blank?
195
+ expression = []
196
+ hash.each do |key, value|
197
+ expression << "#{key}=#{escape(value)}"
198
+ end
199
+ expression.join(' AND ')
200
+ end
201
+
164
202
  # Retrieve a list of SDB domains from Amazon.
165
203
  #
166
204
  # Returns a hash:
@@ -325,7 +363,11 @@ module RightAws
325
363
  link = generate_request("GetAttributes", 'DomainName' => domain_name,
326
364
  'ItemName' => item_name,
327
365
  'AttributeName' => attribute_name )
328
- request_info(link, QSdbGetAttributesParser.new)
366
+ res = request_info(link, QSdbGetAttributesParser.new)
367
+ res[:attributes].each_value do |values|
368
+ values.collect! { |e| sdb_to_ruby(e) }
369
+ end
370
+ res
329
371
  rescue Exception
330
372
  on_exception
331
373
  end
@@ -386,10 +428,15 @@ module RightAws
386
428
  # query = [ "['cat'=?] union ['dog'=?]", "clew", "Jon's boot" ]
387
429
  # sdb.query('family', query)
388
430
  #
431
+ # query = [ "['cat'=?] union ['dog'=?] sort 'cat' desc", "clew", "Jon's boot" ]
432
+ # sdb.query('family', query)
433
+ #
389
434
  # see: http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/SDB_API_Query.html
435
+ # http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/index.html?SortingData.html
390
436
  #
391
437
  def query(domain_name, query_expression = nil, max_number_of_items = nil, next_token = nil)
392
438
  query_expression = query_expression_from_array(query_expression) if query_expression.is_a?(Array)
439
+ @last_query_expression = query_expression
393
440
  #
394
441
  request_params = { 'DomainName' => domain_name,
395
442
  'QueryExpression' => query_expression,
@@ -412,6 +459,137 @@ module RightAws
412
459
  on_exception
413
460
  end
414
461
 
462
+ # Perform a query and fetch specified attributes.
463
+ # If attributes are not specified then fetches the whole list of attributes.
464
+ #
465
+ #
466
+ # Returns a hash:
467
+ # { :box_usage => string,
468
+ # :request_id => string,
469
+ # :next_token => string,
470
+ # :items => [ { ItemName1 => { attribute1 => value1, ... attributeM => valueM } },
471
+ # { ItemName2 => {...}}, ... ]
472
+ #
473
+ # Example:
474
+ #
475
+ # sdb.query_with_attributes(domain, ['hobby', 'country'], "['gender'='female'] intersection ['name' starts-with ''] sort 'name'") #=>
476
+ # { :request_id => "06057228-70d0-4487-89fb-fd9c028580d3",
477
+ # :items =>
478
+ # [ { "035f1ba8-dbd8-11dd-80bd-001bfc466dd7"=>
479
+ # { "hobby" => ["cooking", "flowers", "cats"],
480
+ # "country" => ["Russia"]}},
481
+ # { "0327614a-dbd8-11dd-80bd-001bfc466dd7"=>
482
+ # { "hobby" => ["patchwork", "bundle jumping"],
483
+ # "country" => ["USA"]}}, ... ],
484
+ # :box_usage=>"0.0000504786"}
485
+ #
486
+ # sdb.query_with_attributes(domain, [], "['gender'='female'] intersection ['name' starts-with ''] sort 'name'") #=>
487
+ # { :request_id => "75bb19db-a529-4f69-b86f-5e3800f79a45",
488
+ # :items =>
489
+ # [ { "035f1ba8-dbd8-11dd-80bd-001bfc466dd7"=>
490
+ # { "hobby" => ["cooking", "flowers", "cats"],
491
+ # "name" => ["Mary"],
492
+ # "country" => ["Russia"],
493
+ # "gender" => ["female"],
494
+ # "id" => ["035f1ba8-dbd8-11dd-80bd-001bfc466dd7"]}},
495
+ # { "0327614a-dbd8-11dd-80bd-001bfc466dd7"=>
496
+ # { "hobby" => ["patchwork", "bundle jumping"],
497
+ # "name" => ["Mary"],
498
+ # "country" => ["USA"],
499
+ # "gender" => ["female"],
500
+ # "id" => ["0327614a-dbd8-11dd-80bd-001bfc466dd7"]}}, ... ],
501
+ # :box_usage=>"0.0000506668"}
502
+ #
503
+ # see: http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/index.html?SDB_API_QueryWithAttributes.html
504
+ #
505
+ def query_with_attributes(domain_name, attributes=[], query_expression = nil, max_number_of_items = nil, next_token = nil)
506
+ attributes = attributes.to_a
507
+ query_expression = query_expression_from_array(query_expression) if query_expression.is_a?(Array)
508
+ @last_query_expression = query_expression
509
+ #
510
+ request_params = { 'DomainName' => domain_name,
511
+ 'QueryExpression' => query_expression,
512
+ 'MaxNumberOfItems' => max_number_of_items,
513
+ 'NextToken' => next_token }
514
+ attributes.each_with_index do |attribute, idx|
515
+ request_params["AttributeName.#{idx+1}"] = attribute
516
+ end
517
+ link = generate_request("QueryWithAttributes", request_params)
518
+ result = select_response_to_ruby(request_info( link, QSdbQueryWithAttributesParser.new ))
519
+ # return result if no block given
520
+ return result unless block_given?
521
+ # loop if block if given
522
+ begin
523
+ # the block must return true if it wanna continue
524
+ break unless yield(result) && result[:next_token]
525
+ # make new request
526
+ request_params['NextToken'] = result[:next_token]
527
+ link = generate_request("QueryWithAttributes", request_params)
528
+ result = select_response_to_ruby(request_info( link, QSdbQueryWithAttributesParser.new ))
529
+ end while true
530
+ rescue Exception
531
+ on_exception
532
+ end
533
+
534
+ # Perform SQL-like select and fetch attributes.
535
+ # Attribute values must be quoted with a single or double quote. If a quote appears within the attribute value, it must be escaped with the same quote symbol as shown in the following example.
536
+ # (Use array to pass select_expression params to avoid manual escaping).
537
+ #
538
+ # sdb.select(["select * from my_domain where gender=?", 'female']) #=>
539
+ # {:request_id =>"8241b843-0fb9-4d66-9100-effae12249ec",
540
+ # :items =>
541
+ # [ { "035f1ba8-dbd8-11dd-80bd-001bfc466dd7"=>
542
+ # {"hobby" => ["cooking", "flowers", "cats"],
543
+ # "name" => ["Mary"],
544
+ # "country" => ["Russia"],
545
+ # "gender" => ["female"],
546
+ # "id" => ["035f1ba8-dbd8-11dd-80bd-001bfc466dd7"]}},
547
+ # { "0327614a-dbd8-11dd-80bd-001bfc466dd7"=>
548
+ # {"hobby" => ["patchwork", "bundle jumping"],
549
+ # "name" => ["Mary"],
550
+ # "country" => ["USA"],
551
+ # "gender" => ["female"],
552
+ # "id" => ["0327614a-dbd8-11dd-80bd-001bfc466dd7"]}}, ... ]
553
+ # :box_usage =>"0.0000506197"}
554
+ #
555
+ # sdb.select('select country, name from my_domain') #=>
556
+ # {:request_id=>"b1600198-c317-413f-a8dc-4e7f864a940a",
557
+ # :items=>
558
+ # [ { "035f1ba8-dbd8-11dd-80bd-001bfc466dd7"=> {"name"=>["Mary"], "country"=>["Russia"]} },
559
+ # { "376d2e00-75b0-11dd-9557-001bfc466dd7"=> {"name"=>["Putin"], "country"=>["Russia"]} },
560
+ # { "0327614a-dbd8-11dd-80bd-001bfc466dd7"=> {"name"=>["Mary"], "country"=>["USA"]} },
561
+ # { "372ebbd4-75b0-11dd-9557-001bfc466dd7"=> {"name"=>["Bush"], "country"=>["USA"]} },
562
+ # { "37a4e552-75b0-11dd-9557-001bfc466dd7"=> {"name"=>["Medvedev"], "country"=>["Russia"]} },
563
+ # { "38278dfe-75b0-11dd-9557-001bfc466dd7"=> {"name"=>["Mary"], "country"=>["Russia"]} },
564
+ # { "37df6c36-75b0-11dd-9557-001bfc466dd7"=> {"name"=>["Mary"], "country"=>["USA"]} } ],
565
+ # :box_usage=>"0.0000777663"}
566
+ #
567
+ # see: http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/index.html?SDB_API_Select.html
568
+ # http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/index.html?UsingSelect.html
569
+ # http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/index.html?SDBLimits.html
570
+ #
571
+ def select(select_expression, next_token = nil)
572
+ select_expression = query_expression_from_array(select_expression) if select_expression.is_a?(Array)
573
+ @last_query_expression = select_expression
574
+ #
575
+ request_params = { 'SelectExpression' => select_expression,
576
+ 'NextToken' => next_token }
577
+ link = generate_request("Select", request_params)
578
+ result = select_response_to_ruby(request_info( link, QSdbSelectParser.new ))
579
+ return result unless block_given?
580
+ # loop if block if given
581
+ begin
582
+ # the block must return true if it wanna continue
583
+ break unless yield(result) && result[:next_token]
584
+ # make new request
585
+ request_params['NextToken'] = result[:next_token]
586
+ link = generate_request("Select", request_params)
587
+ result = select_response_to_ruby(request_info( link, QSdbSelectParser.new ))
588
+ end while true
589
+ rescue Exception
590
+ on_exception
591
+ end
592
+
415
593
  #-----------------------------------------------------------------
416
594
  # PARSERS:
417
595
  #-----------------------------------------------------------------
@@ -421,10 +599,10 @@ module RightAws
421
599
  end
422
600
  def tagend(name)
423
601
  case name
424
- when 'NextToken' : @result[:next_token] = @text
425
- when 'DomainName' : @result[:domains] << @text
426
- when 'BoxUsage' : @result[:box_usage] = @text
427
- when 'RequestId' : @result[:request_id] = @text
602
+ when 'NextToken' then @result[:next_token] = @text
603
+ when 'DomainName' then @result[:domains] << @text
604
+ when 'BoxUsage' then @result[:box_usage] = @text
605
+ when 'RequestId' then @result[:request_id] = @text
428
606
  end
429
607
  end
430
608
  end
@@ -435,8 +613,8 @@ module RightAws
435
613
  end
436
614
  def tagend(name)
437
615
  case name
438
- when 'BoxUsage' : @result[:box_usage] = @text
439
- when 'RequestId' : @result[:request_id] = @text
616
+ when 'BoxUsage' then @result[:box_usage] = @text
617
+ when 'RequestId' then @result[:request_id] = @text
440
618
  end
441
619
  end
442
620
  end
@@ -448,10 +626,10 @@ module RightAws
448
626
  end
449
627
  def tagend(name)
450
628
  case name
451
- when 'Name' : @last_attribute_name = @text
452
- when 'Value' : (@result[:attributes][@last_attribute_name] ||= []) << @text
453
- when 'BoxUsage' : @result[:box_usage] = @text
454
- when 'RequestId' : @result[:request_id] = @text
629
+ when 'Name' then @last_attribute_name = @text
630
+ when 'Value' then (@result[:attributes][@last_attribute_name] ||= []) << @text
631
+ when 'BoxUsage' then @result[:box_usage] = @text
632
+ when 'RequestId' then @result[:request_id] = @text
455
633
  end
456
634
  end
457
635
  end
@@ -462,10 +640,56 @@ module RightAws
462
640
  end
463
641
  def tagend(name)
464
642
  case name
465
- when 'ItemName' : @result[:items] << @text
466
- when 'BoxUsage' : @result[:box_usage] = @text
467
- when 'RequestId' : @result[:request_id] = @text
468
- when 'NextToken' : @result[:next_token] = @text
643
+ when 'ItemName' then @result[:items] << @text
644
+ when 'BoxUsage' then @result[:box_usage] = @text
645
+ when 'RequestId' then @result[:request_id] = @text
646
+ when 'NextToken' then @result[:next_token] = @text
647
+ end
648
+ end
649
+ end
650
+
651
+ class QSdbQueryWithAttributesParser < RightAWSParser #:nodoc:
652
+ def reset
653
+ @result = { :items => [] }
654
+ end
655
+ def tagend(name)
656
+ case name
657
+ when 'Name'
658
+ case @xmlpath
659
+ when 'QueryWithAttributesResponse/QueryWithAttributesResult/Item'
660
+ @item = @text
661
+ @result[:items] << { @item => {} }
662
+ when 'QueryWithAttributesResponse/QueryWithAttributesResult/Item/Attribute'
663
+ @attribute = @text
664
+ @result[:items].last[@item][@attribute] ||= []
665
+ end
666
+ when 'RequestId' then @result[:request_id] = @text
667
+ when 'BoxUsage' then @result[:box_usage] = @text
668
+ when 'NextToken' then @result[:next_token] = @text
669
+ when 'Value' then @result[:items].last[@item][@attribute] << @text
670
+ end
671
+ end
672
+ end
673
+
674
+ class QSdbSelectParser < RightAWSParser #:nodoc:
675
+ def reset
676
+ @result = { :items => [] }
677
+ end
678
+ def tagend(name)
679
+ case name
680
+ when 'Name'
681
+ case @xmlpath
682
+ when 'SelectResponse/SelectResult/Item'
683
+ @item = @text
684
+ @result[:items] << { @item => {} }
685
+ when 'SelectResponse/SelectResult/Item/Attribute'
686
+ @attribute = @text
687
+ @result[:items].last[@item][@attribute] ||= []
688
+ end
689
+ when 'RequestId' then @result[:request_id] = @text
690
+ when 'BoxUsage' then @result[:box_usage] = @text
691
+ when 'NextToken' then @result[:next_token] = @text
692
+ when 'Value' then @result[:items].last[@item][@attribute] << @text
469
693
  end
470
694
  end
471
695
  end
data/lib/sqs/right_sqs.rb CHANGED
@@ -54,6 +54,14 @@ module RightAws
54
54
  # ...
55
55
  # grantee2 = queue.grantees('another_cool_guy@email.address')
56
56
  # grantee2.revoke('SENDMESSAGE')
57
+ #
58
+ # Params is a hash:
59
+ #
60
+ # {:server => 'queue.amazonaws.com' # Amazon service host: 'queue.amazonaws.com' (default)
61
+ # :port => 443 # Amazon service port: 80 or 443 (default)
62
+ # :multi_thread => true|false # Multi-threaded (connection per each thread): true or false (default)
63
+ # :signature_version => '0' # The signature version : '0' or '1'(default)
64
+ # :logger => Logger Object} # Logger instance: logs to STDOUT if omitted }
57
65
  #
58
66
  class Sqs
59
67
  attr_reader :interface
@@ -55,6 +55,14 @@ module RightAws
55
55
  # ...
56
56
  #
57
57
  # NB: Second-generation SQS has eliminated the entire access grant mechanism present in Gen 1.
58
+ #
59
+ # Params is a hash:
60
+ #
61
+ # {:server => 'queue.amazonaws.com' # Amazon service host: 'queue.amazonaws.com' (default)
62
+ # :port => 443 # Amazon service port: 80 or 443 (default)
63
+ # :multi_thread => true|false # Multi-threaded (connection per each thread): true or false (default)
64
+ # :signature_version => '0' # The signature version : '0' or '1'(default)
65
+ # :logger => Logger Object} # Logger instance: logs to STDOUT if omitted }
58
66
  class SqsGen2
59
67
  attr_reader :interface
60
68
 
@@ -38,7 +38,6 @@ module RightAws
38
38
  class SqsGen2Interface < RightAwsBase
39
39
  include RightAwsBaseInterface
40
40
 
41
- SIGNATURE_VERSION = "1"
42
41
  API_VERSION = "2008-01-01"
43
42
  DEFAULT_HOST = "queue.amazonaws.com"
44
43
  DEFAULT_PORT = 443
@@ -99,48 +98,44 @@ module RightAws
99
98
  # fields required in all requests, and then the headers passed in as
100
99
  # params. We sort the header fields alphabetically and then generate the
101
100
  # signature before URL escaping the resulting query and sending it.
102
- queue_uri = param[:queue_url] ? URI(param[:queue_url]).path : '/'
101
+ service = param[:queue_url] ? URI(param[:queue_url]).path : '/'
103
102
  param.each{ |key, value| param.delete(key) if (value.nil? || key.is_a?(Symbol)) }
104
- request_hash = { "Action" => action,
103
+ service_hash = { "Action" => action,
105
104
  "Expires" => (Time.now + REQUEST_TTL).utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
106
105
  "AWSAccessKeyId" => @aws_access_key_id,
107
- "Version" => API_VERSION,
108
- "SignatureVersion" => SIGNATURE_VERSION }
109
- request_hash.update(param)
110
- request_data = request_hash.sort{|a,b| (a[0].to_s.downcase)<=>(b[0].to_s.downcase)}.to_s
111
- request_hash['Signature'] = AwsUtils::sign(@aws_secret_access_key, request_data)
112
- request_params = request_hash.to_a.collect{|key,val| key.to_s + "=" + val.to_s }.join("&")
113
- request = Net::HTTP::Get.new(AwsUtils.URLencode("#{queue_uri}?#{request_params}"))
106
+ "Version" => API_VERSION }
107
+ service_hash.update(param)
108
+ service_params = signed_service_params(@aws_secret_access_key, service_hash, :get, @params[:server], service)
109
+ request = Net::HTTP::Get.new("#{AwsUtils.URLencode(service)}?#{service_params}")
114
110
  # prepare output hash
115
111
  { :request => request,
116
112
  :server => @params[:server],
117
113
  :port => @params[:port],
118
114
  :protocol => @params[:protocol],
119
- :proxy => @params[:proxy] }
115
+ :proxy => @params[:proxy] }
120
116
  end
121
117
 
122
118
  def generate_post_request(action, param={}) # :nodoc:
123
- queue_uri = param[:queue_url] ? URI(param[:queue_url]).path : '/'
119
+ service = param[:queue_url] ? URI(param[:queue_url]).path : '/'
124
120
  message = param[:message] # extract message body if nesessary
125
121
  param.each{ |key, value| param.delete(key) if (value.nil? || key.is_a?(Symbol)) }
126
- request_hash = { "Action" => action,
122
+ service_hash = { "Action" => action,
127
123
  "Expires" => (Time.now + REQUEST_TTL).utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
128
124
  "AWSAccessKeyId" => @aws_access_key_id,
129
125
  "MessageBody" => message,
130
- "Version" => API_VERSION,
131
- "SignatureVersion" => SIGNATURE_VERSION }
132
- request_hash.update(param)
133
- request_data = request_hash.sort{|a,b| (a[0].to_s.downcase)<=>(b[0].to_s.downcase)}.to_s
134
- request_hash['Signature'] = AwsUtils::sign(@aws_secret_access_key, request_data)
135
- request_body = request_hash.to_a.collect{|key,val| CGI.escape(key.to_s) + "=" + CGI.escape(val.to_s) }.join("&")
136
- request = Net::HTTP::Post.new(AwsUtils.URLencode(queue_uri))
126
+ "Version" => API_VERSION }
127
+ service_hash.update(param)
128
+ #
129
+ service_params = signed_service_params(@aws_secret_access_key, service_hash, :post, @params[:server], service)
130
+ request = Net::HTTP::Post.new(AwsUtils::URLencode(service))
137
131
  request['Content-Type'] = 'application/x-www-form-urlencoded'
138
- request.body = request_body
132
+ request.body = service_params
139
133
  # prepare output hash
140
134
  { :request => request,
141
135
  :server => @params[:server],
142
136
  :port => @params[:port],
143
- :protocol => @params[:protocol] }
137
+ :protocol => @params[:protocol],
138
+ :proxy => @params[:proxy] }
144
139
  end
145
140
 
146
141
 
@@ -78,30 +78,23 @@ module RightAws
78
78
  def generate_request(action, params={}) # :nodoc:
79
79
  # Sometimes we need to use queue uri (delete queue etc)
80
80
  # In that case we will use Symbol key: 'param[:queue_url]'
81
- queue_uri = params[:queue_url] ? URI(params[:queue_url]).path : '/'
81
+ service = params[:queue_url] ? URI(params[:queue_url]).path : '/'
82
82
  # remove unset(=optional) and symbolyc keys
83
83
  params.each{ |key, value| params.delete(key) if (value.nil? || key.is_a?(Symbol)) }
84
84
  # prepare output hash
85
85
  service_hash = { "Action" => action,
86
86
  "Expires" => (Time.now + REQUEST_TTL).utc.strftime("%Y-%m-%dT%H:%M:%SZ"),
87
87
  "AWSAccessKeyId" => @aws_access_key_id,
88
- "Version" => API_VERSION,
89
- "SignatureVersion" => signature_version }
88
+ "Version" => API_VERSION }
90
89
  service_hash.update(params)
91
- # prepare string to sight
92
- string_to_sign = case signature_version
93
- when '0' : service_hash["Action"] + service_hash["Expires"]
94
- when '1' : service_hash.sort{|a,b| (a[0].to_s.downcase)<=>(b[0].to_s.downcase)}.to_s
95
- end
96
- service_hash['Signature'] = AwsUtils::sign(@aws_secret_access_key, string_to_sign)
97
- request_params = service_hash.to_a.collect{|key,val| key.to_s + "=" + CGI::escape(val.to_s) }.join("&")
98
- request = Net::HTTP::Get.new("#{queue_uri}?#{request_params}")
90
+ service_params = signed_service_params(@aws_secret_access_key, service_hash, :get, @params[:server], service)
91
+ request = Net::HTTP::Get.new("#{AwsUtils::URLencode(service)}?#{service_params}")
99
92
  # prepare output hash
100
93
  { :request => request,
101
94
  :server => @params[:server],
102
95
  :port => @params[:port],
103
96
  :protocol => @params[:protocol],
104
- :proxy => @params[:proxy] }
97
+ :proxy => @params[:proxy] }
105
98
  end
106
99
 
107
100
  # Generates a request hash for the REST API
@@ -129,7 +122,8 @@ module RightAws
129
122
  { :request => request,
130
123
  :server => @params[:server],
131
124
  :port => @params[:port],
132
- :protocol => @params[:protocol] }
125
+ :protocol => @params[:protocol],
126
+ :proxy => @params[:proxy] }
133
127
  end
134
128
 
135
129
 
@@ -0,0 +1,2 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../../lib/right_aws'