aws 1.11.2 → 1.11.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/right_aws.rb CHANGED
@@ -53,7 +53,7 @@ module RightAws #:nodoc:
53
53
  module VERSION #:nodoc:
54
54
  MAJOR = 1
55
55
  MINOR = 11
56
- TINY = 2
56
+ TINY = 3
57
57
 
58
58
  STRING = [MAJOR, MINOR, TINY].join('.')
59
59
  end
@@ -33,56 +33,56 @@ module RightAws
33
33
  # = RightAws::ActiveSdb -- RightScale SDB interface (alpha release)
34
34
  # The RightAws::ActiveSdb class provides a complete interface to Amazon's Simple
35
35
  # Database Service.
36
- #
36
+ #
37
37
  # ActiveSdb is in alpha and does not load by default with the rest of RightAws. You must use an additional require statement to load the ActiveSdb class. For example:
38
- #
38
+ #
39
39
  # require 'right_aws'
40
40
  # require 'sdb/active_sdb'
41
- #
42
- # Additionally, the ActiveSdb class requires the 'uuidtools' gem; this gem is not normally required by RightAws and is not installed as a
41
+ #
42
+ # Additionally, the ActiveSdb class requires the 'uuidtools' gem; this gem is not normally required by RightAws and is not installed as a
43
43
  # dependency of RightAws.
44
44
  #
45
45
  # Simple ActiveSdb usage example:
46
46
  #
47
- # class Client < RightAws::ActiveSdb::Base
47
+ # class Client < RightAws::ActiveSdb::Base
48
48
  # end
49
- #
49
+ #
50
50
  # # connect to SDB
51
51
  # RightAws::ActiveSdb.establish_connection
52
- #
52
+ #
53
53
  # # create domain
54
54
  # Client.create_domain
55
- #
55
+ #
56
56
  # # create initial DB
57
57
  # Client.create 'name' => 'Bush', 'country' => 'USA', 'gender' => 'male', 'expiration' => '2009', 'post' => 'president'
58
- # Client.create 'name' => 'Putin', 'country' => 'Russia', 'gender' => 'male', 'expiration' => '2008', 'post' => 'president'
58
+ # Client.create 'name' => 'Putin', 'country' => 'Russia', 'gender' => 'male', 'expiration' => '2008', 'post' => 'president'
59
59
  # Client.create 'name' => 'Medvedev', 'country' => 'Russia', 'gender' => 'male', 'expiration' => '2012', 'post' => 'president'
60
60
  # Client.create 'name' => 'Mary', 'country' => 'USA', 'gender' => 'female', 'hobby' => ['patchwork', 'bundle jumping']
61
61
  # Client.create 'name' => 'Mary', 'country' => 'Russia', 'gender' => 'female', 'hobby' => ['flowers', 'cats', 'cooking']
62
62
  # sandy_id = Client.create('name' => 'Sandy', 'country' => 'Russia', 'gender' => 'female', 'hobby' => ['flowers', 'cats', 'cooking']).id
63
- #
63
+ #
64
64
  # # find all Bushes in USA
65
65
  # Client.find(:all, :conditions => ["['name'=?] intersection ['country'=?]",'Bush','USA']).each do |client|
66
66
  # client.reload
67
67
  # puts client.attributes.inspect
68
68
  # end
69
- #
69
+ #
70
70
  # # find all Maries through the world
71
71
  # Client.find_all_by_name_and_gender('Mary','female').each do |mary|
72
72
  # mary.reload
73
73
  # puts "#{mary[:name]}, gender: #{mary[:gender]}, hobbies: #{mary[:hobby].join(',')}"
74
74
  # end
75
- #
75
+ #
76
76
  # # find new russian president
77
77
  # medvedev = Client.find_by_post_and_country_and_expiration('president','Russia','2012')
78
78
  # if medvedev
79
79
  # medvedev.reload
80
80
  # medvedev.save_attributes('age' => '42', 'hobby' => 'Gazprom')
81
81
  # end
82
- #
82
+ #
83
83
  # # retire old president
84
84
  # Client.find_by_name('Putin').delete
85
- #
85
+ #
86
86
  # # Sandy disappointed in 'cooking' and decided to hide her 'gender' and 'country' ()
87
87
  # sandy = Client.find(sandy_id)
88
88
  # sandy.reload
@@ -93,7 +93,7 @@ module RightAws
93
93
  # Client.delete_domain
94
94
  #
95
95
  class ActiveSdb
96
-
96
+
97
97
  module ActiveSdbConnect
98
98
  def connection
99
99
  @connection || raise(ActiveSdbError.new('Connection to SDB is not established'))
@@ -124,9 +124,9 @@ module RightAws
124
124
  @connection.close_connection unless @connection.nil?
125
125
  end
126
126
  end
127
-
127
+
128
128
  class ActiveSdbError < RuntimeError ; end
129
-
129
+
130
130
  class << self
131
131
  include ActiveSdbConnect
132
132
 
@@ -138,49 +138,49 @@ module RightAws
138
138
  connection.list_domains[:domains]
139
139
  end
140
140
 
141
- # Create new domain.
141
+ # Create new domain.
142
142
  # Raises no errors if the domain already exists.
143
- #
143
+ #
144
144
  # RightAws::ActiveSdb.create_domain('alpha') #=> {:request_id=>"6fc652a0-0000-41d5-91f4-3ed390a3d3b2", :box_usage=>"0.0055590278"}
145
145
  #
146
146
  def create_domain(domain_name)
147
147
  connection.create_domain(domain_name)
148
148
  end
149
149
 
150
- # Remove domain from SDB.
150
+ # Remove domain from SDB.
151
151
  # Raises no errors if the domain does not exist.
152
- #
152
+ #
153
153
  # RightAws::ActiveSdb.create_domain('alpha') #=> {:request_id=>"6fc652a0-0000-41c6-91f4-3ed390a3d3b2", :box_usage=>"0.0055590001"}
154
154
  #
155
155
  def delete_domain(domain_name)
156
156
  connection.delete_domain(domain_name)
157
157
  end
158
158
  end
159
-
159
+
160
160
  class Base
161
-
161
+
162
162
  class << self
163
163
  include ActiveSdbConnect
164
-
164
+
165
165
  # next_token value returned by last find: is useful to continue finding
166
166
  attr_accessor :next_token
167
-
167
+
168
168
  # Returns a RightAws::SdbInterface object
169
169
  #
170
170
  # class A < RightAws::ActiveSdb::Base
171
171
  # end
172
- #
172
+ #
173
173
  # class B < RightAws::ActiveSdb::Base
174
174
  # end
175
- #
175
+ #
176
176
  # class C < RightAws::ActiveSdb::Base
177
177
  # end
178
178
  #
179
179
  # RightAws::ActiveSdb.establish_connection 'key_id_1', 'secret_key_1'
180
- #
180
+ #
181
181
  # C.establish_connection 'key_id_2', 'secret_key_2'
182
182
  #
183
- # # A and B uses the default connection, C - uses its own
183
+ # # A and B uses the default connection, C - uses its own
184
184
  # puts A.connection #=> #<RightAws::SdbInterface:0xb76d6d7c>
185
185
  # puts B.connection #=> #<RightAws::SdbInterface:0xb76d6d7c>
186
186
  # puts C.connection #=> #<RightAws::SdbInterface:0xb76d6ca0>
@@ -197,7 +197,7 @@ module RightAws
197
197
  # class Client < RightAws::ActiveSdb::Base
198
198
  # end
199
199
  # puts Client.domain #=> 'client'
200
- #
200
+ #
201
201
  # # if 'ActiveSupport' is loaded then class name being tableized
202
202
  # require 'activesupport'
203
203
  # class Client < RightAws::ActiveSdb::Base
@@ -222,7 +222,7 @@ module RightAws
222
222
  end
223
223
 
224
224
  # Change the default domain name to user defined.
225
- #
225
+ #
226
226
  # class Client < RightAws::ActiveSdb::Base
227
227
  # set_domain_name :foreign_clients
228
228
  # end
@@ -233,7 +233,7 @@ module RightAws
233
233
 
234
234
  # Create domain at SDB.
235
235
  # Raises no errors if the domain already exists.
236
- #
236
+ #
237
237
  # class Client < RightAws::ActiveSdb::Base
238
238
  # end
239
239
  # Client.create_domain #=> {:request_id=>"6fc652a0-0000-41d5-91f4-3ed390a3d3b2", :box_usage=>"0.0055590278"}
@@ -244,7 +244,7 @@ module RightAws
244
244
 
245
245
  # Remove domain from SDB.
246
246
  # Raises no errors if the domain does not exist.
247
- #
247
+ #
248
248
  # class Client < RightAws::ActiveSdb::Base
249
249
  # end
250
250
  # Client.delete_domain #=> {:request_id=>"e14d90d3-0000-4898-9995-0de28cdda270", :box_usage=>"0.0055590278"}
@@ -252,7 +252,7 @@ module RightAws
252
252
  def delete_domain
253
253
  connection.delete_domain(domain)
254
254
  end
255
-
255
+
256
256
  #
257
257
  # See select(), original find with QUERY syntax is deprecated so now find and select are synonyms.
258
258
  #
@@ -314,7 +314,7 @@ module RightAws
314
314
  #
315
315
  # Sort oder:
316
316
  # If :order=>'attribute' option is specified then result response (ordered by 'attribute') will contain only items where attribute is defined (is not null).
317
- #
317
+ #
318
318
  # Client.select(:all) # returns all records
319
319
  # Client.select(:all, :order => 'gender') # returns all records ordered by gender where gender attribute exists
320
320
  # Client.select(:all, :order => 'name desc') # returns all records ordered by name in desc order where name attribute exists
@@ -323,6 +323,7 @@ module RightAws
323
323
  #
324
324
  def select(*args)
325
325
  options = args.last.is_a?(Hash) ? args.pop : {}
326
+ # puts 'first=' + args.first.to_s
326
327
  case args.first
327
328
  when :all then sql_select(options)
328
329
  when :first then sql_select(options.merge(:limit => 1)).first
@@ -448,7 +449,7 @@ module RightAws
448
449
  # request items
449
450
  query_result = self.connection.query(domain, query_expression, options[:max_number_of_items], @next_token)
450
451
  @next_token = query_result[:next_token]
451
- items = query_result[:items].map do |name|
452
+ items = query_result[:items].map do |name|
452
453
  new_item = self.new('id' => name)
453
454
  new_item.mark_as_old
454
455
  reload_if_exists(record) if options[:auto_load]
@@ -511,7 +512,7 @@ module RightAws
511
512
  end
512
513
  end
513
514
 
514
- # find_by helpers
515
+ # find_by helpers
515
516
  def find_all_by_(format_str, args, options) # :nodoc:
516
517
  fields = format_str.to_s.sub(/^find_(all_)?by_/,'').split('_and_')
517
518
  conditions = fields.map { |field| "['#{field}'=?]" }.join(' intersection ')
@@ -567,12 +568,12 @@ module RightAws
567
568
  end
568
569
 
569
570
  end
570
-
571
+
571
572
  public
572
573
 
573
574
  # instance attributes
574
- attr_accessor :attributes
575
-
575
+ attr_accessor :attributes
576
+
576
577
  # item name
577
578
  attr_accessor :id
578
579
 
@@ -583,7 +584,7 @@ module RightAws
583
584
  # puts item.inspect #=> #<Client:0xb77a2698 @new_record=true, @attributes={"name"=>["Jon"], "toys"=>["girls", "beer", "pub"]}>
584
585
  # item.save #=> {"name"=>["Jon"], "id"=>"c03edb7e-e45c-11dc-bede-001bfc466dd7", "toys"=>["girls", "beer", "pub"]}
585
586
  # puts item.inspect #=> #<Client:0xb77a2698 @new_record=false, @attributes={"name"=>["Jon"], "id"=>"c03edb7e-e45c-11dc-bede-001bfc466dd7", "toys"=>["girls", "beer", "pub"]}>
586
- #
587
+ #
587
588
  def initialize(attrs={})
588
589
  @attributes = uniq_values(attrs)
589
590
  @new_record = true
@@ -592,20 +593,20 @@ module RightAws
592
593
  # Create and save new Item instance.
593
594
  # +Attributes+ is a hash: { attribute1 => values1, ..., attributeN => valuesN }.
594
595
  #
595
- # item = Client.create('name' => 'Cat', 'toys' => ['Jons socks', 'mice', 'clew'])
596
+ # item = Client.create('name' => 'Cat', 'toys' => ['Jons socks', 'mice', 'clew'])
596
597
  # puts item.inspect #=> #<Client:0xb77a0a78 @new_record=false, @attributes={"name"=>["Cat"], "id"=>"2937601a-e45d-11dc-a75f-001bfc466dd7", "toys"=>["Jons socks", "mice", "clew"]}>
597
- #
598
+ #
598
599
  def self.create(attributes={})
599
600
  item = self.new(attributes)
600
601
  item.save
601
602
  item
602
603
  end
603
-
604
+
604
605
  # Returns an item id. Same as: item['id'] or item.attributes['id']
605
606
  def id
606
607
  @attributes['id']
607
608
  end
608
-
609
+
609
610
  # Sets an item id.
610
611
  def id=(id)
611
612
  @attributes['id'] = id.to_s
@@ -618,7 +619,7 @@ module RightAws
618
619
  def attributes
619
620
  @attributes.dup
620
621
  end
621
-
622
+
622
623
  # Allows one to set all the attributes at once by passing in a hash with keys matching the attribute names.
623
624
  # if '+id+' attribute is not set in new attributes has then it being derived from old attributes.
624
625
  #
@@ -637,7 +638,7 @@ module RightAws
637
638
  self.attributes
638
639
  end
639
640
 
640
- def connection
641
+ def connection
641
642
  self.class.connection
642
643
  end
643
644
 
@@ -645,9 +646,9 @@ module RightAws
645
646
  def domain
646
647
  self.class.domain
647
648
  end
648
-
649
+
649
650
  # Returns the values of the attribute identified by +attribute+.
650
- #
651
+ #
651
652
  # puts item['Cat'].inspect #=> ["Jons socks", "clew", "mice"]
652
653
  #
653
654
  def [](attribute)
@@ -655,7 +656,7 @@ module RightAws
655
656
  end
656
657
 
657
658
  # Updates the attribute identified by +attribute+ with the specified +values+.
658
- #
659
+ #
659
660
  # puts item['Cat'].inspect #=> ["Jons socks", "clew", "mice"]
660
661
  # item['Cat'] = ["Whiskas", "chicken"]
661
662
  # puts item['Cat'].inspect #=> ["Whiskas", "chicken"]
@@ -666,10 +667,10 @@ module RightAws
666
667
  end
667
668
 
668
669
  # Reload attributes from SDB. Replaces in-memory attributes.
669
- #
670
+ #
670
671
  # item = Client.find_by_name('Cat') #=> #<Client:0xb77d0d40 @attributes={"id"=>"2937601a-e45d-11dc-a75f-001bfc466dd7"}, @new_record=false>
671
672
  # item.reload #=> #<Client:0xb77d0d40 @attributes={"id"=>"2937601a-e45d-11dc-a75f-001bfc466dd7", "name"=>["Cat"], "toys"=>["Jons socks", "clew", "mice"]}, @new_record=false>
672
- #
673
+ #
673
674
  def reload
674
675
  raise_on_id_absence
675
676
  old_id = id
@@ -686,10 +687,10 @@ module RightAws
686
687
  # Reload a set of attributes from SDB. Adds the loaded list to in-memory data.
687
688
  # +attrs_list+ is an array or comma separated list of attributes names.
688
689
  # Returns a hash of loaded attributes.
689
- #
690
+ #
690
691
  # This is not the best method to get a bunch of attributes because
691
692
  # a web service call is being performed for every attribute.
692
- #
693
+ #
693
694
  # item = Client.find_by_name('Cat')
694
695
  # item.reload_attributes('toys', 'name') #=> {"name"=>["Cat"], "toys"=>["Jons socks", "clew", "mice"]}
695
696
  #
@@ -713,7 +714,7 @@ module RightAws
713
714
 
714
715
  # Stores in-memory attributes to SDB.
715
716
  # Adds the attributes values to already stored at SDB.
716
- # Returns a hash of stored attributes.
717
+ # Returns a hash of stored attributes.
717
718
  #
718
719
  # sandy = Client.new(:name => 'Sandy') #=> #<Client:0xb775a7a8 @attributes={"name"=>["Sandy"]}, @new_record=true>
719
720
  # sandy['toys'] = 'boys'
@@ -763,8 +764,8 @@ module RightAws
763
764
 
764
765
  # Store in-memory attributes to SDB.
765
766
  # Replaces the attributes values already stored at SDB by in-memory data.
766
- # Returns a hash of stored attributes.
767
- #
767
+ # Returns a hash of stored attributes.
768
+ #
768
769
  # sandy = Client.new(:name => 'Sandy') #=> #<Client:0xb775a7a8 @attributes={"name"=>["Sandy"]}, @new_record=true>
769
770
  # sandy['toys'] = 'boys'
770
771
  # sandy.put
@@ -777,13 +778,21 @@ module RightAws
777
778
  #
778
779
  # compare to +put+ method
779
780
  def save
780
- @attributes = uniq_values(@attributes)
781
- prepare_for_update
781
+ pre_save2
782
782
  connection.put_attributes(domain, id, @attributes, :replace)
783
- mark_as_old
783
+ apres_save2
784
784
  @attributes
785
785
  end
786
786
 
787
+ def pre_save2
788
+ @attributes = uniq_values(@attributes)
789
+ prepare_for_update
790
+ end
791
+
792
+ def apres_save2
793
+ mark_as_old
794
+ end
795
+
787
796
  # Replaces the attributes at SDB by the given values.
788
797
  # +Attrs+ is a hash: { attribute1 => values1, ..., attributeN => valuesN }.
789
798
  # The other in-memory attributes are not being saved.
@@ -808,7 +817,7 @@ module RightAws
808
817
  # Remove specified values from corresponding attributes.
809
818
  # +attrs+ is a hash: { attribute1 => values1, ..., attributeN => valuesN }.
810
819
  #
811
- # sandy = Client.find_by_name 'Sandy'
820
+ # sandy = Client.find_by_name 'Sandy'
812
821
  # sandy.reload
813
822
  # puts sandy.inspect #=> #<Client:0xb77b48fc @new_record=false, @attributes={"name"=>["Sandy"], "id"=>"b2832ce2-e461-11dc-b13c-001bfc466dd7", "toys"=>["boys", "kids", "patchwork"]}>
814
823
  # puts sandy.delete_values('toys' => 'patchwork') #=> { 'toys' => ['patchwork'] }
@@ -836,8 +845,8 @@ module RightAws
836
845
  # Removes specified attributes from the item.
837
846
  # +attrs_list+ is an array or comma separated list of attributes names.
838
847
  # Returns the list of deleted attributes.
839
- #
840
- # sandy = Client.find_by_name 'Sandy'
848
+ #
849
+ # sandy = Client.find_by_name 'Sandy'
841
850
  # sandy.reload
842
851
  # puts sandy.inspect #=> #<Client:0xb7761d28 @new_record=false, @attributes={"name"=>["Sandy"], "id"=>"b2832ce2-e461-11dc-b13c-001bfc466dd7", "toys"=>["boys", "kids", "patchwork"}>
843
852
  # puts sandy.delete_attributes('toys') #=> ['toys']
@@ -855,14 +864,14 @@ module RightAws
855
864
  end
856
865
 
857
866
  # Delete the Item entirely from SDB.
858
- #
859
- # sandy = Client.find_by_name 'Sandy'
867
+ #
868
+ # sandy = Client.find_by_name 'Sandy'
860
869
  # sandy.reload
861
870
  # sandy.inspect #=> #<Client:0xb7761d28 @new_record=false, @attributes={"name"=>["Sandy"], "id"=>"b2832ce2-e461-11dc-b13c-001bfc466dd7", "toys"=>["boys", "kids", "patchwork"}>
862
871
  # puts sandy.delete
863
- # sandy.reload
872
+ # sandy.reload
864
873
  # puts sandy.inspect #=> #<Client:0xb7761d28 @attributes={}, @new_record=false>
865
- #
874
+ #
866
875
  def delete
867
876
  raise_on_id_absence
868
877
  connection.delete_attributes(domain, id)
@@ -873,7 +882,7 @@ module RightAws
873
882
  @id
874
883
  end
875
884
 
876
- # Returns true if this object hasn‘t been saved yet.
885
+ # Returns true if this object hasn‘t been saved yet.
877
886
  def new_record?
878
887
  @new_record
879
888
  end
@@ -882,16 +891,16 @@ module RightAws
882
891
  @new_record = false
883
892
  end
884
893
 
885
- private
886
-
894
+ private
895
+
887
896
  def raise_on_id_absence
888
897
  raise ActiveSdbError.new('Unknown record id') unless id
889
898
  end
890
-
899
+
891
900
  def prepare_for_update
892
901
  @attributes['id'] = self.class.generate_id if @attributes['id'].blank?
893
902
  end
894
-
903
+
895
904
  def uniq_values(attributes=nil) # :nodoc:
896
905
  attrs = {}
897
906
  attributes.each do |attribute, values|
@@ -49,13 +49,13 @@ module RightAws
49
49
  # :protocol => 'https' # Amazon service protocol: 'http' or 'https'(default)
50
50
  # :signature_version => '0' # The signature version : '0' or '1'(default)
51
51
  # :multi_thread => true|false # Multi-threaded (connection per each thread): true or false(default)
52
- # :logger => Logger Object # Logger instance: logs to STDOUT if omitted
52
+ # :logger => Logger Object # Logger instance: logs to STDOUT if omitted
53
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')
54
- #
54
+ #
55
55
  # Example:
56
- #
56
+ #
57
57
  # sdb = RightAws::SdbInterface.new('1E3GDYEOGFJPIT7XXXXXX','hgTHt68JY07JKUY08ftHYtERkjgtfERn57XXXXXX', {:multi_thread => true, :logger => Logger.new('/tmp/x.log')}) #=> #<RightSdb:0xa6b8c27c>
58
- #
58
+ #
59
59
  # see: http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/
60
60
  #
61
61
  def initialize(aws_access_key_id=nil, aws_secret_access_key=nil, params={})
@@ -136,24 +136,24 @@ module RightAws
136
136
 
137
137
  # Prepare attributes for putting.
138
138
  # (used by put_attributes)
139
- def pack_attributes(attributes, replace = false) #:nodoc:
139
+ def pack_attributes(attributes, replace = false, key_prefix = "") #:nodoc:
140
140
  result = {}
141
141
  if attributes
142
142
  idx = 0
143
143
  skip_values = attributes.is_a?(Array)
144
144
  attributes.each do |attribute, values|
145
145
  # set replacement attribute
146
- result["Attribute.#{idx}.Replace"] = 'true' if replace
146
+ result["#{key_prefix}Attribute.#{idx}.Replace"] = 'true' if replace
147
147
  # pack Name/Value
148
148
  unless values.nil?
149
149
  Array(values).each do |value|
150
- result["Attribute.#{idx}.Name"] = attribute
151
- result["Attribute.#{idx}.Value"] = ruby_to_sdb(value) unless skip_values
150
+ result["#{key_prefix}Attribute.#{idx}.Name"] = attribute
151
+ result["#{key_prefix}Attribute.#{idx}.Value"] = ruby_to_sdb(value) unless skip_values
152
152
  idx += 1
153
153
  end
154
154
  else
155
- result["Attribute.#{idx}.Name"] = attribute
156
- result["Attribute.#{idx}.Value"] = ruby_to_sdb(nil) unless skip_values
155
+ result["#{key_prefix}Attribute.#{idx}.Name"] = attribute
156
+ result["#{key_prefix}Attribute.#{idx}.Value"] = ruby_to_sdb(nil) unless skip_values
157
157
  idx += 1
158
158
  end
159
159
  end
@@ -161,9 +161,10 @@ module RightAws
161
161
  result
162
162
  end
163
163
 
164
- # Use this helper to manually escape the fields in the query expressions.
164
+
165
+ # Use this helper to manually escape the fields in the query expressions.
165
166
  # To escape the single quotes and backslashes and to wrap the string into the single quotes.
166
- #
167
+ #
167
168
  # see: http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/SDB_API.html
168
169
  #
169
170
  def escape(value)
@@ -220,23 +221,23 @@ module RightAws
220
221
  end
221
222
 
222
223
  # Retrieve a list of SDB domains from Amazon.
223
- #
224
+ #
224
225
  # Returns a hash:
225
226
  # { :domains => [domain1, ..., domainN],
226
227
  # :next_token => string || nil,
227
228
  # :box_usage => string,
228
229
  # :request_id => string }
229
- #
230
+ #
230
231
  # Example:
231
- #
232
+ #
232
233
  # sdb = RightAws::SdbInterface.new
233
234
  # sdb.list_domains #=> { :box_usage => "0.0000071759",
234
235
  # :request_id => "976709f9-0111-2345-92cb-9ce90acd0982",
235
236
  # :domains => ["toys", "dolls"]}
236
- #
237
+ #
237
238
  # If a block is given, this method yields to it. If the block returns true, list_domains will continue looping the request. If the block returns false,
238
239
  # list_domains will end.
239
- #
240
+ #
240
241
  # sdb.list_domains(10) do |result| # list by 10 domains per iteration
241
242
  # puts result.inspect
242
243
  # true
@@ -253,7 +254,7 @@ module RightAws
253
254
  return result unless block_given?
254
255
  # loop if block if given
255
256
  begin
256
- # the block must return true if it wanna continue
257
+ # the block must return true if it wanna continue
257
258
  break unless yield(result) && result[:next_token]
258
259
  # make new request
259
260
  request_params['NextToken'] = result[:next_token]
@@ -265,12 +266,12 @@ module RightAws
265
266
  end
266
267
 
267
268
  # Create new SDB domain at Amazon.
268
- #
269
+ #
269
270
  # Returns a hash: { :box_usage, :request_id } on success or an exception on error.
270
271
  # (Amazon raises no errors if the domain already exists).
271
- #
272
+ #
272
273
  # Example:
273
- #
274
+ #
274
275
  # sdb = RightAws::SdbInterface.new
275
276
  # sdb.create_domain('toys') # => { :box_usage => "0.0000071759",
276
277
  # :request_id => "976709f9-0111-2345-92cb-9ce90acd0982" }
@@ -285,12 +286,12 @@ module RightAws
285
286
  end
286
287
 
287
288
  # Delete SDB domain at Amazon.
288
- #
289
+ #
289
290
  # Returns a hash: { :box_usage, :request_id } on success or an exception on error.
290
291
  # (Amazon raises no errors if the domain does not exist).
291
- #
292
+ #
292
293
  # Example:
293
- #
294
+ #
294
295
  # sdb = RightAws::SdbInterface.new
295
296
  # sdb.delete_domain('toys') # => { :box_usage => "0.0000071759",
296
297
  # :request_id => "976709f9-0111-2345-92cb-9ce90acd0982" }
@@ -306,7 +307,7 @@ module RightAws
306
307
  end
307
308
 
308
309
  # Add/Replace item attributes.
309
- #
310
+ #
310
311
  # Params:
311
312
  # domain_name = DomainName
312
313
  # item_name = ItemName
@@ -316,28 +317,28 @@ module RightAws
316
317
  # 'nameZ' => [valueZ1,..., valueZN]
317
318
  # }
318
319
  # replace = :replace | any other value to skip replacement
319
- #
320
- # Returns a hash: { :box_usage, :request_id } on success or an exception on error.
320
+ #
321
+ # Returns a hash: { :box_usage, :request_id } on success or an exception on error.
321
322
  # (Amazon raises no errors if the attribute was not overridden, as when the :replace param is unset).
322
- #
323
+ #
323
324
  # Example:
324
- #
325
+ #
325
326
  # sdb = RightAws::SdbInterface.new
326
327
  # sdb.create_domain 'family'
327
- #
328
+ #
328
329
  # attributes = {}
329
330
  # # create attributes for Jon and Silvia
330
331
  # attributes['Jon'] = %w{ car beer }
331
- # attributes['Silvia'] = %w{ beetle rolling_pin kids }
332
+ # attributes['Silvia'] = %w{ beetle rolling_pin kids }
332
333
  # sdb.put_attributes 'family', 'toys', attributes #=> ok
333
334
  # # now: Jon=>[car, beer], Silvia=>[beetle, rolling_pin, kids]
334
- #
335
+ #
335
336
  # # add attributes to Jon
336
337
  # attributes.delete('Silvia')
337
338
  # attributes['Jon'] = %w{ girls pub }
338
339
  # sdb.put_attributes 'family', 'toys', attributes #=> ok
339
340
  # # now: Jon=>[car, beer, girls, pub], Silvia=>[beetle, rolling_pin, kids]
340
- #
341
+ #
341
342
  # # replace attributes for Jon and add to a cat (the cat had no attributes before)
342
343
  # attributes['Jon'] = %w{ vacuum_cleaner hammer spade }
343
344
  # attributes['cat'] = %w{ mouse clew Jons_socks }
@@ -355,15 +356,32 @@ module RightAws
355
356
  on_exception
356
357
  end
357
358
 
359
+ #
360
+ # items is an array of RightAws::SdbInterface::Item.new(o.id, o.attributes, true)
361
+ def batch_put_attributes(domain_name, items)
362
+ params = { 'DomainName' => domain_name }
363
+ i = 0
364
+ items.each do |item|
365
+ prefix = "Item." + i.to_s + "."
366
+ params[prefix + "ItemName"] = item.item_name
367
+ params.merge!(pack_attributes(item.attributes, item.replace, prefix))
368
+ i += 1
369
+ end
370
+ link = generate_request("BatchPutAttributes", params)
371
+ request_info( link, QSdbSimpleParser.new )
372
+ rescue Exception
373
+ on_exception
374
+ end
375
+
358
376
  # Retrieve SDB item's attribute(s).
359
- #
377
+ #
360
378
  # Returns a hash:
361
379
  # { :box_usage => string,
362
380
  # :request_id => string,
363
381
  # :attributes => { 'nameA' => [valueA1,..., valueAN],
364
382
  # ... ,
365
383
  # 'nameZ' => [valueZ1,..., valueZN] } }
366
- #
384
+ #
367
385
  # Example:
368
386
  # # request all attributes
369
387
  # sdb.get_attributes('family', 'toys') # => { :attributes => {"cat" => ["clew", "Jons_socks", "mouse"] },
@@ -371,7 +389,7 @@ module RightAws
371
389
  # "Jon" => ["vacuum_cleaner", "hammer", "spade"]},
372
390
  # :box_usage => "0.0000093222",
373
391
  # :request_id => "81273d21-000-1111-b3f9-512d91d29ac8" }
374
- #
392
+ #
375
393
  # # request cat's attributes only
376
394
  # sdb.get_attributes('family', 'toys', 'cat') # => { :attributes => {"cat" => ["clew", "Jons_socks", "mouse"] },
377
395
  # :box_usage => "0.0000093222",
@@ -400,13 +418,13 @@ module RightAws
400
418
  #
401
419
  # # delete the all the values from attributes (i.e. delete the attributes)
402
420
  # sdb.delete_attributes 'family', 'toys', { 'Jon' => [], 'cat' => [] }
403
- # # or
421
+ # # or
404
422
  # sdb.delete_attributes 'family', 'toys', [ 'Jon', 'cat' ]
405
423
  #
406
424
  # # delete all the attributes from item 'toys' (i.e. delete the item)
407
425
  # sdb.delete_attributes 'family', 'toys'
408
- #
409
- # see http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/SDB_API_DeleteAttributes.html
426
+ #
427
+ # see http://docs.amazonwebservices.com/AmazonSimpleDB/2007-11-07/DeveloperGuide/SDB_API_DeleteAttributes.html
410
428
  #
411
429
  def delete_attributes(domain_name, item_name, attributes = nil)
412
430
  params = { 'DomainName' => domain_name,
@@ -421,29 +439,29 @@ module RightAws
421
439
  # QUERY:
422
440
 
423
441
  # Perform a query on SDB.
424
- #
442
+ #
425
443
  # Returns a hash:
426
444
  # { :box_usage => string,
427
445
  # :request_id => string,
428
446
  # :next_token => string,
429
447
  # :items => [ItemName1,..., ItemNameN] }
430
- #
448
+ #
431
449
  # Example:
432
- #
450
+ #
433
451
  # query = "['cat' = 'clew']"
434
452
  # sdb.query('family', query) #=> hash of data
435
453
  # sdb.query('family', query, 10) #=> hash of data with max of 10 items
436
- #
454
+ #
437
455
  # If a block is given, query will iteratively yield results to it as long as the block continues to return true.
438
- #
439
- # # List 10 items per iteration. Don't
456
+ #
457
+ # # List 10 items per iteration. Don't
440
458
  # # forget to escape single quotes and backslashes and wrap all the items in single quotes.
441
459
  # query = "['cat'='clew'] union ['dog'='Jon\\'s boot']"
442
460
  # sdb.query('family', query, 10) do |result|
443
461
  # puts result.inspect
444
462
  # true
445
463
  # end
446
- #
464
+ #
447
465
  # # Same query using automatic escaping...to use the auto escape, pass the query and its params as an array:
448
466
  # query = [ "['cat'=?] union ['dog'=?]", "clew", "Jon's boot" ]
449
467
  # sdb.query('family', query)
@@ -468,7 +486,7 @@ module RightAws
468
486
  return result unless block_given?
469
487
  # loop if block if given
470
488
  begin
471
- # the block must return true if it wanna continue
489
+ # the block must return true if it wanna continue
472
490
  break unless yield(result) && result[:next_token]
473
491
  # make new request
474
492
  request_params['NextToken'] = result[:next_token]
@@ -610,6 +628,15 @@ module RightAws
610
628
  on_exception
611
629
  end
612
630
 
631
+ class Item
632
+ attr_accessor :item_name, :attributes, :replace
633
+ def initialize(item_name, attributes, replace = false)
634
+ @item_name = item_name
635
+ @attributes = attributes
636
+ @replace = replace
637
+ end
638
+ end
639
+
613
640
  #-----------------------------------------------------------------
614
641
  # PARSERS:
615
642
  #-----------------------------------------------------------------
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.11.2
4
+ version: 1.11.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Travis Reeder
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-19 00:00:00 -07:00
12
+ date: 2009-05-21 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency