aws-sdk 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (205) hide show
  1. data/.yardopts +6 -0
  2. data/LICENSE.txt +171 -0
  3. data/NOTICE.txt +2 -0
  4. data/README.rdoc +189 -0
  5. data/lib/aws-sdk.rb +14 -0
  6. data/lib/aws.rb +63 -0
  7. data/lib/aws/api_config.rb +45 -0
  8. data/lib/aws/api_config/.document +0 -0
  9. data/lib/aws/api_config/EC2-2011-02-28.yml +2314 -0
  10. data/lib/aws/api_config/SNS-2010-03-31.yml +171 -0
  11. data/lib/aws/api_config/SQS-2009-02-01.yml +161 -0
  12. data/lib/aws/api_config/SimpleDB-2009-04-15.yml +278 -0
  13. data/lib/aws/api_config/SimpleEmailService-2010-12-01.yml +147 -0
  14. data/lib/aws/api_config_transform.rb +32 -0
  15. data/lib/aws/async_handle.rb +90 -0
  16. data/lib/aws/authorize_v2.rb +37 -0
  17. data/lib/aws/authorize_v3.rb +37 -0
  18. data/lib/aws/base_client.rb +524 -0
  19. data/lib/aws/cacheable.rb +92 -0
  20. data/lib/aws/common.rb +228 -0
  21. data/lib/aws/configurable.rb +36 -0
  22. data/lib/aws/configuration.rb +272 -0
  23. data/lib/aws/configured_client_methods.rb +81 -0
  24. data/lib/aws/configured_grammars.rb +65 -0
  25. data/lib/aws/configured_option_grammars.rb +46 -0
  26. data/lib/aws/configured_xml_grammars.rb +47 -0
  27. data/lib/aws/default_signer.rb +38 -0
  28. data/lib/aws/ec2.rb +321 -0
  29. data/lib/aws/ec2/attachment.rb +149 -0
  30. data/lib/aws/ec2/attachment_collection.rb +57 -0
  31. data/lib/aws/ec2/availability_zone.rb +80 -0
  32. data/lib/aws/ec2/availability_zone_collection.rb +47 -0
  33. data/lib/aws/ec2/block_device_mappings.rb +53 -0
  34. data/lib/aws/ec2/client.rb +54 -0
  35. data/lib/aws/ec2/client/xml.rb +127 -0
  36. data/lib/aws/ec2/collection.rb +39 -0
  37. data/lib/aws/ec2/config_transform.rb +63 -0
  38. data/lib/aws/ec2/elastic_ip.rb +107 -0
  39. data/lib/aws/ec2/elastic_ip_collection.rb +85 -0
  40. data/lib/aws/ec2/errors.rb +29 -0
  41. data/lib/aws/ec2/filtered_collection.rb +65 -0
  42. data/lib/aws/ec2/has_permissions.rb +46 -0
  43. data/lib/aws/ec2/image.rb +245 -0
  44. data/lib/aws/ec2/image_collection.rb +235 -0
  45. data/lib/aws/ec2/instance.rb +515 -0
  46. data/lib/aws/ec2/instance_collection.rb +276 -0
  47. data/lib/aws/ec2/key_pair.rb +86 -0
  48. data/lib/aws/ec2/key_pair_collection.rb +102 -0
  49. data/lib/aws/ec2/permission_collection.rb +177 -0
  50. data/lib/aws/ec2/region.rb +81 -0
  51. data/lib/aws/ec2/region_collection.rb +55 -0
  52. data/lib/aws/ec2/request.rb +27 -0
  53. data/lib/aws/ec2/reserved_instances.rb +50 -0
  54. data/lib/aws/ec2/reserved_instances_collection.rb +44 -0
  55. data/lib/aws/ec2/reserved_instances_offering.rb +55 -0
  56. data/lib/aws/ec2/reserved_instances_offering_collection.rb +43 -0
  57. data/lib/aws/ec2/resource.rb +340 -0
  58. data/lib/aws/ec2/resource_tag_collection.rb +218 -0
  59. data/lib/aws/ec2/security_group.rb +246 -0
  60. data/lib/aws/ec2/security_group/ip_permission.rb +70 -0
  61. data/lib/aws/ec2/security_group/ip_permission_collection.rb +59 -0
  62. data/lib/aws/ec2/security_group_collection.rb +132 -0
  63. data/lib/aws/ec2/snapshot.rb +138 -0
  64. data/lib/aws/ec2/snapshot_collection.rb +90 -0
  65. data/lib/aws/ec2/tag.rb +88 -0
  66. data/lib/aws/ec2/tag_collection.rb +114 -0
  67. data/lib/aws/ec2/tagged_collection.rb +48 -0
  68. data/lib/aws/ec2/tagged_item.rb +87 -0
  69. data/lib/aws/ec2/volume.rb +190 -0
  70. data/lib/aws/ec2/volume_collection.rb +95 -0
  71. data/lib/aws/errors.rb +129 -0
  72. data/lib/aws/http/builtin_handler.rb +69 -0
  73. data/lib/aws/http/curb_handler.rb +123 -0
  74. data/lib/aws/http/handler.rb +77 -0
  75. data/lib/aws/http/httparty_handler.rb +61 -0
  76. data/lib/aws/http/request.rb +136 -0
  77. data/lib/aws/http/request_param.rb +63 -0
  78. data/lib/aws/http/response.rb +75 -0
  79. data/lib/aws/ignore_result_element.rb +38 -0
  80. data/lib/aws/indifferent_hash.rb +86 -0
  81. data/lib/aws/inflection.rb +46 -0
  82. data/lib/aws/lazy_error_classes.rb +64 -0
  83. data/lib/aws/meta_utils.rb +43 -0
  84. data/lib/aws/model.rb +57 -0
  85. data/lib/aws/naming.rb +32 -0
  86. data/lib/aws/option_grammar.rb +544 -0
  87. data/lib/aws/policy.rb +912 -0
  88. data/lib/aws/rails.rb +209 -0
  89. data/lib/aws/record.rb +79 -0
  90. data/lib/aws/record/attribute.rb +94 -0
  91. data/lib/aws/record/attribute_macros.rb +288 -0
  92. data/lib/aws/record/attributes/boolean.rb +49 -0
  93. data/lib/aws/record/attributes/datetime.rb +86 -0
  94. data/lib/aws/record/attributes/float.rb +48 -0
  95. data/lib/aws/record/attributes/integer.rb +68 -0
  96. data/lib/aws/record/attributes/sortable_float.rb +60 -0
  97. data/lib/aws/record/attributes/sortable_integer.rb +95 -0
  98. data/lib/aws/record/attributes/string.rb +69 -0
  99. data/lib/aws/record/base.rb +728 -0
  100. data/lib/aws/record/conversion.rb +38 -0
  101. data/lib/aws/record/dirty_tracking.rb +286 -0
  102. data/lib/aws/record/errors.rb +153 -0
  103. data/lib/aws/record/exceptions.rb +48 -0
  104. data/lib/aws/record/finder_methods.rb +262 -0
  105. data/lib/aws/record/naming.rb +31 -0
  106. data/lib/aws/record/scope.rb +157 -0
  107. data/lib/aws/record/validations.rb +653 -0
  108. data/lib/aws/record/validator.rb +237 -0
  109. data/lib/aws/record/validators/acceptance.rb +51 -0
  110. data/lib/aws/record/validators/block.rb +38 -0
  111. data/lib/aws/record/validators/confirmation.rb +43 -0
  112. data/lib/aws/record/validators/count.rb +108 -0
  113. data/lib/aws/record/validators/exclusion.rb +43 -0
  114. data/lib/aws/record/validators/format.rb +57 -0
  115. data/lib/aws/record/validators/inclusion.rb +56 -0
  116. data/lib/aws/record/validators/length.rb +107 -0
  117. data/lib/aws/record/validators/numericality.rb +138 -0
  118. data/lib/aws/record/validators/presence.rb +45 -0
  119. data/lib/aws/resource_cache.rb +39 -0
  120. data/lib/aws/response.rb +113 -0
  121. data/lib/aws/response_cache.rb +50 -0
  122. data/lib/aws/s3.rb +109 -0
  123. data/lib/aws/s3/access_control_list.rb +252 -0
  124. data/lib/aws/s3/acl_object.rb +266 -0
  125. data/lib/aws/s3/bucket.rb +320 -0
  126. data/lib/aws/s3/bucket_collection.rb +122 -0
  127. data/lib/aws/s3/bucket_version_collection.rb +85 -0
  128. data/lib/aws/s3/client.rb +999 -0
  129. data/lib/aws/s3/client/xml.rb +190 -0
  130. data/lib/aws/s3/data_options.rb +99 -0
  131. data/lib/aws/s3/errors.rb +43 -0
  132. data/lib/aws/s3/multipart_upload.rb +318 -0
  133. data/lib/aws/s3/multipart_upload_collection.rb +78 -0
  134. data/lib/aws/s3/object_collection.rb +159 -0
  135. data/lib/aws/s3/object_metadata.rb +67 -0
  136. data/lib/aws/s3/object_upload_collection.rb +83 -0
  137. data/lib/aws/s3/object_version.rb +141 -0
  138. data/lib/aws/s3/object_version_collection.rb +78 -0
  139. data/lib/aws/s3/paginated_collection.rb +94 -0
  140. data/lib/aws/s3/policy.rb +76 -0
  141. data/lib/aws/s3/prefix_and_delimiter_collection.rb +56 -0
  142. data/lib/aws/s3/prefixed_collection.rb +84 -0
  143. data/lib/aws/s3/presigned_post.rb +504 -0
  144. data/lib/aws/s3/request.rb +198 -0
  145. data/lib/aws/s3/s3_object.rb +794 -0
  146. data/lib/aws/s3/tree.rb +116 -0
  147. data/lib/aws/s3/tree/branch_node.rb +71 -0
  148. data/lib/aws/s3/tree/child_collection.rb +108 -0
  149. data/lib/aws/s3/tree/leaf_node.rb +99 -0
  150. data/lib/aws/s3/tree/node.rb +22 -0
  151. data/lib/aws/s3/tree/parent.rb +90 -0
  152. data/lib/aws/s3/uploaded_part.rb +82 -0
  153. data/lib/aws/s3/uploaded_part_collection.rb +86 -0
  154. data/lib/aws/service_interface.rb +60 -0
  155. data/lib/aws/simple_db.rb +202 -0
  156. data/lib/aws/simple_db/attribute.rb +159 -0
  157. data/lib/aws/simple_db/attribute_collection.rb +227 -0
  158. data/lib/aws/simple_db/client.rb +52 -0
  159. data/lib/aws/simple_db/client/options.rb +34 -0
  160. data/lib/aws/simple_db/client/xml.rb +68 -0
  161. data/lib/aws/simple_db/consistent_read_option.rb +42 -0
  162. data/lib/aws/simple_db/delete_attributes.rb +64 -0
  163. data/lib/aws/simple_db/domain.rb +118 -0
  164. data/lib/aws/simple_db/domain_collection.rb +116 -0
  165. data/lib/aws/simple_db/domain_metadata.rb +112 -0
  166. data/lib/aws/simple_db/errors.rb +46 -0
  167. data/lib/aws/simple_db/expect_condition_option.rb +45 -0
  168. data/lib/aws/simple_db/item.rb +84 -0
  169. data/lib/aws/simple_db/item_collection.rb +594 -0
  170. data/lib/aws/simple_db/item_data.rb +70 -0
  171. data/lib/aws/simple_db/put_attributes.rb +62 -0
  172. data/lib/aws/simple_db/request.rb +27 -0
  173. data/lib/aws/simple_email_service.rb +373 -0
  174. data/lib/aws/simple_email_service/client.rb +39 -0
  175. data/lib/aws/simple_email_service/client/options.rb +24 -0
  176. data/lib/aws/simple_email_service/client/xml.rb +38 -0
  177. data/lib/aws/simple_email_service/email_address_collection.rb +66 -0
  178. data/lib/aws/simple_email_service/errors.rb +29 -0
  179. data/lib/aws/simple_email_service/quotas.rb +64 -0
  180. data/lib/aws/simple_email_service/request.rb +27 -0
  181. data/lib/aws/sns.rb +69 -0
  182. data/lib/aws/sns/client.rb +37 -0
  183. data/lib/aws/sns/client/options.rb +24 -0
  184. data/lib/aws/sns/client/xml.rb +38 -0
  185. data/lib/aws/sns/errors.rb +29 -0
  186. data/lib/aws/sns/policy.rb +49 -0
  187. data/lib/aws/sns/request.rb +27 -0
  188. data/lib/aws/sns/subscription.rb +100 -0
  189. data/lib/aws/sns/subscription_collection.rb +84 -0
  190. data/lib/aws/sns/topic.rb +384 -0
  191. data/lib/aws/sns/topic_collection.rb +70 -0
  192. data/lib/aws/sns/topic_subscription_collection.rb +58 -0
  193. data/lib/aws/sqs.rb +70 -0
  194. data/lib/aws/sqs/client.rb +38 -0
  195. data/lib/aws/sqs/client/xml.rb +36 -0
  196. data/lib/aws/sqs/errors.rb +33 -0
  197. data/lib/aws/sqs/policy.rb +50 -0
  198. data/lib/aws/sqs/queue.rb +507 -0
  199. data/lib/aws/sqs/queue_collection.rb +105 -0
  200. data/lib/aws/sqs/received_message.rb +184 -0
  201. data/lib/aws/sqs/received_sns_message.rb +112 -0
  202. data/lib/aws/sqs/request.rb +44 -0
  203. data/lib/aws/xml_grammar.rb +923 -0
  204. data/rails/init.rb +15 -0
  205. metadata +298 -0
@@ -0,0 +1,82 @@
1
+ # Copyright 2011 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License"). You
4
+ # may not use this file except in compliance with the License. A copy of
5
+ # the License is located at
6
+ #
7
+ # http://aws.amazon.com/apache2.0/
8
+ #
9
+ # or in the "license" file accompanying this file. This file is
10
+ # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
+ # ANY KIND, either express or implied. See the License for the specific
12
+ # language governing permissions and limitations under the License.
13
+
14
+ require 'aws/model'
15
+
16
+ module AWS
17
+ class S3
18
+
19
+ # Represents a part of a multipart upload that has been uploaded
20
+ # to S3.
21
+ #
22
+ # @example Get the total size of the uploaded parts
23
+ # upload.parts.inject(0) { |sum, part| sum + part.size }
24
+ class UploadedPart
25
+
26
+ include Model
27
+
28
+ # @return [MultipartUpload] The upload to which this belongs.
29
+ attr_reader :upload
30
+
31
+ # @return [Integer] The part number.
32
+ attr_reader :part_number
33
+
34
+ # @private
35
+ def initialize(upload, part_number, opts = {})
36
+ @upload = upload
37
+ @part_number = part_number
38
+ super
39
+ end
40
+
41
+ def ==(other)
42
+ other.kind_of?(UploadedPart) and
43
+ other.upload == upload and
44
+ other.part_number == part_number
45
+ end
46
+ alias_method :eql?, :==
47
+
48
+ # @return [Integer] The size of the part as it currently
49
+ # exists in S3.
50
+ def size
51
+ get_attribute(:size)
52
+ end
53
+
54
+ # @return [DateTime] The time at which the part was last
55
+ # modified.
56
+ def last_modified
57
+ get_attribute(:last_modified)
58
+ end
59
+
60
+ # @return [String] The ETag of the part.
61
+ def etag
62
+ get_attribute(:etag)
63
+ end
64
+
65
+ # @private
66
+ private
67
+ def get_attribute(name)
68
+ (resp = client.list_parts(:bucket_name => upload.object.bucket.name,
69
+ :key => upload.object.key,
70
+ :upload_id => upload.id,
71
+ :part_number_marker => part_number-1,
72
+ :max_parts => 1) and
73
+ part = resp.parts.first and
74
+ part.part_number == part_number and
75
+ part.send(name)) or
76
+ raise "part 3 of upload abc123 does not exist"
77
+ end
78
+
79
+ end
80
+
81
+ end
82
+ end
@@ -0,0 +1,86 @@
1
+ # Copyright 2011 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License"). You
4
+ # may not use this file except in compliance with the License. A copy of
5
+ # the License is located at
6
+ #
7
+ # http://aws.amazon.com/apache2.0/
8
+ #
9
+ # or in the "license" file accompanying this file. This file is
10
+ # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
+ # ANY KIND, either express or implied. See the License for the specific
12
+ # language governing permissions and limitations under the License.
13
+
14
+ require 'aws/model'
15
+ require 'aws/s3/paginated_collection'
16
+ require 'aws/s3/uploaded_part'
17
+
18
+ module AWS
19
+ class S3
20
+
21
+ # Represents the collection of parts that have been uploaded for
22
+ # a given multipart upload. You can get an instance of this
23
+ # class by calling {MultipartUpload#parts}.
24
+ #
25
+ # @example Get the total size of the uploaded parts
26
+ # upload.parts.inject(0) { |sum, part| sum + part.size }
27
+ class UploadedPartCollection
28
+
29
+ include Enumerable
30
+ include Model
31
+ include PaginatedCollection
32
+
33
+ # @return [MultipartUpload] The upload to which the parts belong.
34
+ attr_reader :upload
35
+
36
+ # @private
37
+ def initialize(upload, opts = {})
38
+ @upload = upload
39
+ super
40
+ end
41
+
42
+ # @return [UploadedPart] An object representing the part with
43
+ # the given part number.
44
+ #
45
+ # @param [Integer] number The part number.
46
+ def [](number)
47
+ UploadedPart.new(upload, number)
48
+ end
49
+
50
+ # @private
51
+ protected
52
+ def each_member_in_page(page, &block)
53
+ page.parts.each do |part_info|
54
+ part = UploadedPart.new(upload, part_info.part_number)
55
+ yield(part)
56
+ end
57
+ end
58
+
59
+ # @private
60
+ protected
61
+ def list_options(options)
62
+ opts = super
63
+ opts.merge!(:bucket_name => upload.object.bucket.name,
64
+ :key => upload.object.key,
65
+ :upload_id => upload.id)
66
+ opts
67
+ end
68
+
69
+ # @private
70
+ protected
71
+ def limit_param; :max_parts; end
72
+
73
+ # @private
74
+ protected
75
+ def list_request(options)
76
+ client.list_parts(options)
77
+ end
78
+
79
+ # @private
80
+ protected
81
+ def pagination_markers; [:part_number_marker]; end
82
+
83
+ end
84
+
85
+ end
86
+ end
@@ -0,0 +1,60 @@
1
+ # Copyright 2011 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License"). You
4
+ # may not use this file except in compliance with the License. A copy of
5
+ # the License is located at
6
+ #
7
+ # http://aws.amazon.com/apache2.0/
8
+ #
9
+ # or in the "license" file accompanying this file. This file is
10
+ # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
+ # ANY KIND, either express or implied. See the License for the specific
12
+ # language governing permissions and limitations under the License.
13
+
14
+ require 'aws/errors'
15
+
16
+ module AWS
17
+
18
+ # @private
19
+ module ServiceInterface
20
+
21
+ def self.included base
22
+
23
+ base.send(:attr_reader, :config)
24
+ base.send(:attr_reader, :client)
25
+
26
+ base.module_eval('module Errors; end')
27
+
28
+ unless base::Errors.include?(AWS::Errors)
29
+ base::Errors.module_eval { include AWS::Errors }
30
+ end
31
+
32
+ end
33
+
34
+ # Returns a new interface object for this service. You can override
35
+ # any of the global configuration parameters by passing them in as
36
+ # hash options. They are merged with AWS.config or merged
37
+ # with the provided +:config+ object.
38
+ #
39
+ # @ec2 = AWS::EC2.new(:max_retries => 2)
40
+ #
41
+ # @see AWS::Cofiguration
42
+ #
43
+ # @param [Hash] options
44
+ # @option options [Configuration] :config An AWS::Configuration
45
+ # object to initialize this service interface object with. Defaults
46
+ # to AWS.config when not provided.
47
+ def initialize options = {}
48
+ @config = options[:config]
49
+ @config ||= AWS.config
50
+ @config = @config.with(options)
51
+ @client = config.send(Inflection.ruby_name(self.class.to_s) + '_client')
52
+ end
53
+
54
+ # @private
55
+ def inspect
56
+ "<#{self.class}>"
57
+ end
58
+
59
+ end
60
+ end
@@ -0,0 +1,202 @@
1
+ # Copyright 2011 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License"). You
4
+ # may not use this file except in compliance with the License. A copy of
5
+ # the License is located at
6
+ #
7
+ # http://aws.amazon.com/apache2.0/
8
+ #
9
+ # or in the "license" file accompanying this file. This file is
10
+ # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
+ # ANY KIND, either express or implied. See the License for the specific
12
+ # language governing permissions and limitations under the License.
13
+
14
+ require 'aws/common'
15
+ require 'aws/service_interface'
16
+ require 'aws/s3/errors'
17
+ require 'aws/simple_db/client'
18
+ require 'aws/simple_db/domain_collection'
19
+
20
+ module AWS
21
+
22
+ # This class is the starting point for working with Amazon SimpleDB.
23
+ #
24
+ # To use Amazon SimpleDB you must first
25
+ # {sign up here}[http://aws.amazon.com/simpledb/].
26
+ #
27
+ # For more information about Amazon SimpleDB:
28
+ #
29
+ # * {Amazon SimpleDB}[http://aws.amazon.com/simpledb/]
30
+ # * {Amazon SimpleDB Documentation}[http://aws.amazon.com/documentation/simpledb/]
31
+ #
32
+ # = Credentials
33
+ #
34
+ # You can setup default credentials for all AWS services via
35
+ # AWS.config:
36
+ #
37
+ # AWS.config(
38
+ # :access_key_id => 'YOUR_ACCESS_KEY_ID',
39
+ # :secret_access_key => 'YOUR_SECRET_ACCESS_KEY')
40
+ #
41
+ # Or you can set them directly on the SimpleDB interface:
42
+ #
43
+ # sdb = AWS::SimpleDB.new(
44
+ # :access_key_id => 'YOUR_ACCESS_KEY_ID',
45
+ # :secret_access_key => 'YOUR_SECRET_ACCESS_KEY')
46
+ #
47
+ # = Understanding the SimpleDB Interface
48
+ #
49
+ # SimpleDB stores data in a hierarchy of:
50
+ #
51
+ # Domains > Items > Attributes
52
+ #
53
+ # These are modeled with the following classes:
54
+ #
55
+ # * {DomainCollection}
56
+ # * {Domain}
57
+ # * {ItemCollection}
58
+ # * {Item}
59
+ # * {AttributeCollection}
60
+ # * {Attribute}
61
+ #
62
+ # The collection classes listed above make it easy to enumerate,
63
+ # the objects they represent. They also make it easy to perform
64
+ # bulk operations on all objects in that collection.
65
+ #
66
+ # = Domains
67
+ #
68
+ # Domains are like database tables. A domain must exist before you can
69
+ # write to it. To create a domain:
70
+ #
71
+ # sdb = SimpleDB.new
72
+ # domain = sdb.domains.create('mydomain')
73
+ #
74
+ # For more information about working with domains see {DomainCollection}
75
+ # and {Domain}.
76
+ #
77
+ # = Items & Attributes
78
+ #
79
+ # Items exist in SimpleDB when they have attributes. You can delete an
80
+ # item by removing all of its attributes. You create an item by adding
81
+ # an attribute to it.
82
+ #
83
+ # The following example illustrates how you can reference an item that
84
+ # does not exist yet:
85
+ #
86
+ # sdb = SimpleDB.new
87
+ #
88
+ # # this domain is empty, it has no items
89
+ # domain = sdb.domains.create('newdomain')
90
+ # domain.items.collect(&:name)
91
+ # #=> []
92
+ #
93
+ # # this item doesn't exist yet, so it has no attributes
94
+ # item = domain.items['newitem']
95
+ # item.attributes.collect(&:name)
96
+ # #=> []
97
+ #
98
+ # # the item has no attributes
99
+ # tags = item.attributes['tags']
100
+ # tags.values
101
+ # #=> []
102
+ #
103
+ # To create the item in SimpleDB you just need to add an attribute.
104
+ #
105
+ # tags.add %w(first new)
106
+ #
107
+ # domain.items.collect(&:name)
108
+ # #=> ['newitem']
109
+ #
110
+ # item.attributes.collect(&:name)
111
+ # #=> ['tags']
112
+ #
113
+ # tags.values
114
+ # #=> ['first', 'new']
115
+ #
116
+ # For more information about working with items and attributes, see:
117
+ #
118
+ # * {ItemCollection}
119
+ # * {Item}
120
+ # * {AttributeCollection}
121
+ # * {Attribute}
122
+ #
123
+ # = Lazy Execution
124
+ #
125
+ # Requests are not made until necessary. This means you can drill down
126
+ # all the way to an attribute, by name, without making any requets
127
+ # to SimpleDB.
128
+ #
129
+ # # makes no request to SimpleDB
130
+ # sdb = SimpleDB.new
131
+ # colors = sdb.domains['mydomain'].items['car'].attributes['colors']
132
+ #
133
+ # # one request to get the values for 'colors'
134
+ # puts colors.values
135
+ #
136
+ # # one request to add blue and green
137
+ # colors.add 'blue', 'green'
138
+ #
139
+ # # one request to delete the colors attribute
140
+ # colors.delete
141
+ #
142
+ class SimpleDB
143
+
144
+ include ServiceInterface
145
+
146
+ # Returns a collection object that represents the domains in your
147
+ # account.
148
+ #
149
+ # @return [DomainCollection] Returns a collection representing all your
150
+ # domains.
151
+ def domains
152
+ DomainCollection.new(:config => config)
153
+ end
154
+
155
+ # Call this method with a block. Code executed inside the block
156
+ # make consistent reads until the block ends.
157
+ #
158
+ # AWS::SimpleDB.consistent_reads do
159
+ # # ...
160
+ # end
161
+ #
162
+ # === Other Modes
163
+ #
164
+ # You can also use this same function to disable consistent reads insie
165
+ # a block. This is useful if you have consistent reads enabled by
166
+ # default:
167
+ #
168
+ # AWS::SimpleDB.consistent_reads(false) do
169
+ # # ...
170
+ # end
171
+ #
172
+ # @param [Boolean] state (true) When true, all SimpleDB read operations
173
+ # will be consistent reads inside the block. When false, all
174
+ # reads operations will not be consistent reads. The previous state
175
+ # will be restored after the block executes.
176
+ # @return Returns the final block value.
177
+ def self.consistent_reads state = true, &block
178
+ begin
179
+ prev_state = Thread.current['_simple_db_consistent_reads_']
180
+ Thread.current['_simple_db_consistent_reads_'] = state
181
+ yield
182
+ ensure
183
+ Thread.current['_simple_db_consistent_reads_'] = prev_state
184
+ end
185
+ end
186
+
187
+ # @return [Boolean] Returns true if we are inside an AWS::SimpleDB
188
+ # #consistent_reads method block.
189
+ # @private
190
+ def self.in_consistent_reads_block?
191
+ !Thread.current['_simple_db_consistent_reads_'].nil?
192
+ end
193
+
194
+ # @return [Boolean] Returns true if the consistent_reads block has
195
+ # a true state, false otherwise.
196
+ # @private
197
+ def self.consistent_reads_state
198
+ Thread.current['_simple_db_consistent_reads_']
199
+ end
200
+
201
+ end
202
+ end
@@ -0,0 +1,159 @@
1
+ # Copyright 2011 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License"). You
4
+ # may not use this file except in compliance with the License. A copy of
5
+ # the License is located at
6
+ #
7
+ # http://aws.amazon.com/apache2.0/
8
+ #
9
+ # or in the "license" file accompanying this file. This file is
10
+ # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
+ # ANY KIND, either express or implied. See the License for the specific
12
+ # language governing permissions and limitations under the License.
13
+
14
+ require 'aws/model'
15
+ require 'aws/simple_db/consistent_read_option'
16
+ require 'aws/simple_db/put_attributes'
17
+ require 'aws/simple_db/delete_attributes'
18
+
19
+ module AWS
20
+ class SimpleDB
21
+
22
+ # Represents a single named item attribute in SimpleDB.
23
+ class Attribute
24
+
25
+ include Model
26
+ include Enumerable
27
+ include ConsistentReadOption
28
+ include PutAttributes
29
+ include DeleteAttributes
30
+
31
+ # @private
32
+ def initialize item, name, options = {}
33
+ @item = item
34
+ @name = name
35
+ super
36
+ end
37
+
38
+ # @return [Item] The item this attribute belongs to.
39
+ attr_reader :item
40
+
41
+ # @return [String] The name of this attribute.
42
+ attr_reader :name
43
+
44
+ # Sets all values for this attribute, replacing current values.
45
+ #
46
+ # @example Setting a list of values
47
+ # attributes['colors'].set 'red', 'blue', 'green'
48
+ #
49
+ # @example Setting an array of values
50
+ # attributes['colors'].set ['red', 'blue']
51
+ #
52
+ # @param *values An array or list of attribute values to set.
53
+ # @return [nil]
54
+ def set *values
55
+ put(values, true)
56
+ nil
57
+ end
58
+
59
+ # Appends values to this attribute. Duplicate values are ignored
60
+ # by SimpleDB.
61
+ #
62
+ # @example Adding a list of values
63
+ #
64
+ # attributes['colors'].add 'red', 'blue', 'green'
65
+ #
66
+ # @example Adding an array of values
67
+ #
68
+ # attributes['colors'].add ['red', 'blue']
69
+ #
70
+ # @param *values An array or list of attribute values to add.
71
+ # @return [nil]
72
+ def add *values
73
+ put(values, false)
74
+ nil
75
+ end
76
+ alias_method :<<, :add
77
+
78
+ # Deletes this attribute or specific values from this attribute.
79
+ #
80
+ # @example Delete the attribute and all of its values
81
+ #
82
+ # item.attributes['color'].delete
83
+ #
84
+ # @example Delete specific attribute values
85
+ #
86
+ # item.attributes['color'].delete('red', 'blue')
87
+ #
88
+ # @param values One ore more values to remove from this attribute.
89
+ # If values is empty, then all attribute values are deleted
90
+ # (which deletes this attribute).
91
+ # @return [nil]
92
+ def delete *values
93
+ expect_opts = values.pop if values.last.kind_of?(Hash)
94
+
95
+ if values.empty?
96
+ delete_named_attributes(name, expect_opts || {})
97
+ else
98
+ delete_attribute_values(Hash[[[name, values]]].
99
+ merge(expect_opts || {}))
100
+ end
101
+ nil
102
+ end
103
+
104
+ # Yields once for each value on this attribute.
105
+ #
106
+ # @yield [attribute_value] Yields once for each domain in the account.
107
+ # @yieldparam [String] attribute_value
108
+ # @param [Hash] options
109
+ # @option options [Boolean] :consistent_read (false) A consistent read
110
+ # returns values that reflects all writes that received a successful
111
+ # response prior to the read.
112
+ # @return [nil]
113
+ def each options = {}, &block
114
+
115
+ resp = client.get_attributes(
116
+ :domain_name => item.domain.name,
117
+ :item_name => item.name,
118
+ :attribute_names => [name],
119
+ :consistent_read => consistent_read(options))
120
+
121
+ resp.attributes.each do |attribute|
122
+ yield(attribute.value)
123
+ end
124
+
125
+ nil
126
+
127
+ end
128
+
129
+ # Returns all values for this attribute as an array of strings.
130
+ #
131
+ # @example
132
+ # item.attributes['ratings'].values
133
+ # #=> ['5', '3', '4']
134
+ #
135
+ # @param [Hash] options
136
+ # @option options [Boolean] :consistent_read (false) A consistent read
137
+ # returns values that reflects all writes that received a successful
138
+ # response prior to the read.
139
+ # @return [Array<String>] An array of attribute values
140
+ def values options = {}
141
+ values = []
142
+ self.each(options) do |value|
143
+ values << value
144
+ end
145
+ values
146
+ end
147
+
148
+ # @private
149
+ protected
150
+ def put values, replace
151
+ expect_opts = values.pop if values.last.kind_of?(Hash)
152
+ do_put(attribute_hashes(Hash[[[name, values]]],
153
+ replace),
154
+ expect_opts || {})
155
+ end
156
+
157
+ end
158
+ end
159
+ end