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,29 @@
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/lazy_error_classes'
15
+ require 'aws/sns/client/xml'
16
+
17
+ module AWS
18
+ class SNS
19
+
20
+ # @private
21
+ module Errors
22
+
23
+ BASE_ERROR_GRAMMAR = Client::XML::BaseError
24
+
25
+ include LazyErrorClasses
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,49 @@
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/policy'
15
+
16
+ module AWS
17
+ class SNS
18
+
19
+ # @private
20
+ class Policy < AWS::Policy
21
+
22
+ class Statement < AWS::Policy::Statement
23
+
24
+ ACTION_MAPPING = {
25
+ :add_permission => 'sns:AddPermission',
26
+ :delete_topic => 'sns:DeleteTopic',
27
+ :get_topic_attributes => 'sns:GetTopicAttributes',
28
+ :list_subscriptions_by_topic => 'sns:ListSubscriptionsByTopic',
29
+ :publish => 'sns:Publish',
30
+ :receive => 'sns:Receive',
31
+ :remove_permission => 'sns:RemovePermission',
32
+ :set_topic_attributes => 'sns:SetTopicAttributes',
33
+ :subscribe => 'sns:Subscribe',
34
+ }
35
+
36
+ protected
37
+ def resource_arn resource
38
+ case resource
39
+ when Topic then resource.arn
40
+ #when Subscription then resource.arn
41
+ else super(resource)
42
+ end
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,27 @@
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/http/request'
15
+ require 'aws/authorize_v2'
16
+
17
+ module AWS
18
+ class SNS
19
+
20
+ # @private
21
+ class Request < AWS::Http::Request
22
+
23
+ include AuthorizeV2
24
+
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,100 @@
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/sns/topic_subscription_collection'
16
+ require 'aws/sns/subscription_collection'
17
+
18
+ module AWS
19
+ class SNS
20
+
21
+ # Represents a subscription of a single endpoint to an SNS topic.
22
+ # To create a subscription, use the {Topic#subscribe} method.
23
+ # Depending on the endpoint type, you may also need to use
24
+ # {Topic#confirm_subscription}.
25
+ class Subscription
26
+
27
+ include Model
28
+
29
+ # @private
30
+ def initialize(arn, opts = {})
31
+ @arn = arn
32
+ @topic = opts[:topic]
33
+ @endpoint = opts[:endpoint]
34
+ @protocol = opts[:protocol]
35
+ @owner_id = opts[:owner_id]
36
+ super
37
+ end
38
+
39
+ # @return [String] The ARN of the subscription.
40
+ attr_reader :arn
41
+
42
+ # @return [Topic] The topic to which the endpoint is subscribed.
43
+ attr_reader :topic
44
+
45
+ # @return [String] The endpoint. This can be an HTTP or HTTPS
46
+ # URL, an e-mail address, or a queue ARN.
47
+ attr_reader :endpoint
48
+
49
+ # @return [String] The protocol. Possible values:
50
+ #
51
+ # * +:http+
52
+ # * +:https+
53
+ # * +:email+
54
+ # * +:email_json+
55
+ # * +:sqs+
56
+ attr_reader :protocol
57
+
58
+ # @return [String] The AWS account ID of the subscription owner.
59
+ attr_reader :owner_id
60
+
61
+ # Deletes this subscription.
62
+ # @return [nil]
63
+ def unsubscribe
64
+ client.unsubscribe(:subscription_arn => arn)
65
+ nil
66
+ end
67
+
68
+ # @note This method requests the entire list of subscriptions
69
+ # for the topic (if known) or the account (if the topic is not
70
+ # known). It can be expensive if the number of subscriptions
71
+ # is high.
72
+ #
73
+ # @return [Boolean] Returns true if the subscription exists.
74
+ def exists?
75
+ collection =
76
+ if topic
77
+ TopicSubscriptionCollection.new(topic,
78
+ :config => config)
79
+ else
80
+ SubscriptionCollection.new(:config => config)
81
+ end
82
+ collection.include?(self)
83
+ end
84
+
85
+ # @private
86
+ def inspect
87
+ "<#{self.class}:#{arn}>"
88
+ end
89
+
90
+ # @return [Boolean] Returns true if the subscriptions have the same
91
+ # resource ARN.
92
+ def ==(other)
93
+ other.kind_of?(Subscription) and other.arn == arn
94
+ end
95
+ alias_method :eql?, :==
96
+
97
+ end
98
+
99
+ end
100
+ end
@@ -0,0 +1,84 @@
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/sns/subscription'
16
+ require 'aws/sns/topic'
17
+
18
+ module AWS
19
+ class SNS
20
+
21
+ # Represents the collection of all subscriptions for the AWS
22
+ # account. For example:
23
+ #
24
+ # # get the ARNs of all SQS queues with subscriptions to topics
25
+ # # owned by this account
26
+ # topic.subscriptions.
27
+ # select { |s| s.protocol == :sqs }.
28
+ # collect(&:endpoint)
29
+ #
30
+ class SubscriptionCollection
31
+
32
+ include Model
33
+ include Enumerable
34
+
35
+ # Yield each subscription belonging to this account.
36
+ # @yieldparam [Subscription] subscription Each of the
37
+ # subscriptions in the account.
38
+ # @return [nil]
39
+ def each
40
+ next_token = nil
41
+ begin
42
+ opts = request_opts
43
+ opts[:next_token] = next_token if next_token
44
+ resp = client.send(list_request, opts)
45
+ resp.subscriptions.each do |sub|
46
+ subscription = Subscription.new(sub.subscription_arn,
47
+ :endpoint => sub.endpoint,
48
+ :protocol => sub.protocol.tr('-','_').to_sym,
49
+ :owner_id => sub.owner,
50
+ :topic => Topic.new(sub.topic_arn, :config => config),
51
+ :config => config
52
+ )
53
+ yield(subscription)
54
+ end
55
+ next_token = resp.next_token
56
+ end until resp && next_token.nil?
57
+ nil
58
+ end
59
+
60
+ # Retrieves a subscription object by ARN. This method does not
61
+ # make any requests to the service.
62
+ #
63
+ # @param [String] arn The ARN of the subscription to retrieve.
64
+ # @return [Subscription] The subscription with the given ARN.
65
+ def [] arn
66
+ Subscription.new(arn, :config => config)
67
+ end
68
+
69
+ # @private
70
+ protected
71
+ def list_request
72
+ :list_subscriptions
73
+ end
74
+
75
+ # @private
76
+ protected
77
+ def request_opts
78
+ {}
79
+ end
80
+
81
+ end
82
+
83
+ end
84
+ end
@@ -0,0 +1,384 @@
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 'json'
15
+ require 'aws/model'
16
+ require 'aws/sns/policy'
17
+ require 'aws/sns/subscription'
18
+ require 'aws/sns/topic_subscription_collection'
19
+ require 'aws/sqs'
20
+
21
+ module AWS
22
+ class SNS
23
+
24
+ class Topic
25
+
26
+ include Model
27
+
28
+ # @param [String] arn The topic ARN.
29
+ def initialize arn, options = {}
30
+ @arn = arn
31
+ super
32
+ end
33
+
34
+ # @return [String] The topic ARN.
35
+ attr_reader :arn
36
+
37
+ # The topic name.
38
+ #
39
+ # If you have not set a display name (see {#display_name=}) then
40
+ # this is used as the "From" field for notifications to email and
41
+ # email-json endpoints.
42
+ # @return [String] Returns the toipc name.
43
+ def name
44
+ arn.split(/:/)[-1]
45
+ end
46
+
47
+ # Causes the given +endpoint+ to receive messages published to this
48
+ # topic.
49
+ #
50
+ # == Subscribing to SQS Queues
51
+ #
52
+ # If you subscribe to an SQS queue (with a {SQS::Queue} object}
53
+ # then a policy will be added/updated to the queue that will
54
+ # permit this topic to send it messages. Some important notes:
55
+ #
56
+ # * If you subscribe with a queue by ARN then you must change the
57
+ # policy yourself.
58
+ #
59
+ # * If you do not want the policy modified then pass +:update_policy+
60
+ # as false or just pass the queue's arn
61
+ #
62
+ # topic.subscribe(queue.arn)
63
+ # topic.subscribe(queue, :update_policy => false)
64
+ #
65
+ # @example Using a url string to set the endpoint (http and https)
66
+ #
67
+ # topic.subscribe('http://example.com/messages')
68
+ # topic.subscribe('https://example.com/messages')
69
+ #
70
+ # @example Using a uri object to set the endpoint (http and https)
71
+ #
72
+ # topic.subscribe(URI.parse('http://example.com/messages'))
73
+ # topic.subscribe(URI.parse('https://example.com/messages'))
74
+ #
75
+ # @example Email address as endpoint
76
+ #
77
+ # topic.subscribe('nobody@example.com')
78
+ #
79
+ # @example Email address as a JSON endpoint
80
+ #
81
+ # # send messages encoded as json object to the given email address
82
+ # topic.subscribe('nobody@example.com', :json => true)
83
+ #
84
+ # @example SQS Queue (by arn)
85
+ #
86
+ # # you must manage the queue policy yourself to allow the
87
+ # # the topic to send messages (policy action 'sqs:SendMessage')
88
+ # topic.subscribe('arn:aws:sqs:us-east-1:123456789123:AQueue')
89
+ #
90
+ # @example SQS Queue (by Queue object)
91
+ #
92
+ # # the queue policy will be added/updated to allow the topic
93
+ # # to send it messages
94
+ # topic.subscribe(AWS::SQS.new.queues.first)
95
+ #
96
+ # @param [mixed] endpoint The endpoint that should receive
97
+ # messages that are published to this topic. Valid values
98
+ # for +endpoint+ include:
99
+ # * URI object
100
+ # * http and https URI strings
101
+ # * email addresse
102
+ # * {SQS::Queue}
103
+ # * SQS queue ARN
104
+ # @param [Hash] options
105
+ # @option options [Boolean] :json (false)
106
+ # @return [Subscription,nil] Returns a subscription when possible.
107
+ # If the subscription requires confirmation first, then +nil+ is
108
+ # returned instead.
109
+ def subscribe(endpoint, opts = {})
110
+ subscribe_opts = endpoint_opts(endpoint, opts).merge(:topic_arn => arn)
111
+ resp = client.subscribe(subscribe_opts)
112
+ if arn = resp.subscription_arn and arn =~ /^arn:/
113
+ Subscription.new(arn, :config => config)
114
+ else
115
+ nil
116
+ end
117
+ end
118
+
119
+ # Verifies an endpoint owner's intent to receive messages by
120
+ # validating the token sent to the endpoint by an earlier
121
+ # Subscribe action. If the token is valid, the action creates a
122
+ # new subscription.
123
+ #
124
+ # @param [String] token Short-lived token sent to an endpoint
125
+ # during the {#subscribe} action.
126
+ #
127
+ # @param [Hash] options Additional options for confirming the
128
+ # subscription.
129
+ #
130
+ # @option :options [Boolean] :authenticate_on_unsubscribe
131
+ # Indicates that you want to disable unauthenticated
132
+ # unsubsciption of the subscription. If parameter is present
133
+ # in the request, the request has an AWS signature, and the
134
+ # value of this parameter is true, only the topic owner and
135
+ # the subscription owner will be permitted to unsubscribe the
136
+ # endpoint, and the Unsubscribe action will require AWS
137
+ # authentication.
138
+ #
139
+ # @return [Subscription] The newly created subscription.
140
+ def confirm_subscription(token, opts = {})
141
+ confirm_opts = opts.merge(:token => token, :topic_arn => arn)
142
+ resp = client.confirm_subscription(confirm_opts)
143
+ Subscription.new(
144
+ resp.subscription_arn,
145
+ :topic => self,
146
+ :config => config)
147
+ end
148
+
149
+ # @return [TopicSubscriptionCollection] Returns a collection that
150
+ # represents all of the subscriptions for this topic.
151
+ def subscriptions
152
+ TopicSubscriptionCollection.new(self)
153
+ end
154
+
155
+ # @return [String] Returns the human-readable name used in
156
+ # the "From" field for notifications to email and email-json
157
+ # endpoints. If you have not set the display name the topic
158
+ # {#name} will be used/returned instead.
159
+ def display_name
160
+ to_h[:display_name]
161
+ end
162
+
163
+ # @param [String] display_name Sets the human-readable name used in
164
+ # the "From" field for notifications to email and email-json
165
+ # endpoints.
166
+ # @return [String] Returns the display_name as passed.
167
+ def display_name= display_name
168
+ set_attribute('DisplayName', display_name)
169
+ display_name
170
+ end
171
+
172
+ # @return [String] The topic owner's ID.
173
+ def owner
174
+ to_h[:owner]
175
+ end
176
+
177
+ # @return [Integer] Returns number of confirmed topic subscriptions.
178
+ def num_subscriptions_confirmed
179
+ to_h[:num_subscriptions_confirmed]
180
+ end
181
+
182
+ # @return [Integer] Returns number of pending topic subscriptions.
183
+ def num_subscriptions_pending
184
+ to_h[:num_subscriptions_pending]
185
+ end
186
+
187
+ # @return [Integer] Returns number of deleted topic subscriptions.
188
+ def num_subscriptions_deleted
189
+ to_h[:num_subscriptions_deleted]
190
+ end
191
+
192
+ # @return [Policy] The topic's {Policy}.
193
+ def policy
194
+ to_h[:policy]
195
+ end
196
+
197
+ # Sets the topic's policy.
198
+ # @param [String,Policy] policy A JSON policy string, a {Policy} object
199
+ # or any other object that responds to #to_json with a valid
200
+ # policy.
201
+ # @return [nil]
202
+ def policy= policy
203
+ policy_json = policy.is_a?(String) ? policy : policy.to_json
204
+ set_attribute('Policy', policy_json)
205
+ nil
206
+ end
207
+
208
+ # Publishes a message to this SNS topic.
209
+ #
210
+ # topic.publish('a short message')
211
+ #
212
+ # You can pass a subject that is used when sending the message to
213
+ # email endpoints:
214
+ #
215
+ # topic.publish('message', :subject => 'SNS message subject')
216
+ #
217
+ # If you would like to pass a different message to various protocols
218
+ # (endpoint types) you can pass those as options:
219
+ #
220
+ # topic.publish('default message',
221
+ # :http => "message sent to http endpoints",
222
+ # :https => "message sent to https endpoints",
223
+ # :email => "message sent to email endpoints")
224
+ #
225
+ # The full list of acceptable protocols are listed below. The default
226
+ # message is sent to endpoints who's protocol was not listed.
227
+ #
228
+ # @param [String] default_message The message you want to send to the
229
+ # topic. Messages must be UTF-8 encoded strings at most 8 KB in size
230
+ # (8192 bytes, not 8192 characters).
231
+ # @param [Hash] Options
232
+ # @option options [String] :subject Used as the "Subject" line when
233
+ # the message is delivered to email endpoints. Will also be
234
+ # included in the standard JSON messages delivered to other endpoints.
235
+ # * must be ASCII text that begins with a letter, number or
236
+ # punctuation mark
237
+ # * must not include line breaks or control characters
238
+ # * and must be less than 100 characters long
239
+ # @option options [String] :http - Message to use when sending to an
240
+ # HTTP endpoint.
241
+ # @option options [String] :https - Message to use when sending to an
242
+ # HTTPS endpoint.
243
+ # @option options [String] :email - Message to use when sending to an
244
+ # email endpoint.
245
+ # @option options [String] :email_json - Message to use when sending
246
+ # to an email json endpoint.
247
+ # @option options [String] :sqs - Message to use when sending to an
248
+ # SQS endpoint.
249
+ # @return [String] Returns the ID of the message that was sent.
250
+ def publish default_message, options = {}
251
+
252
+ message = { :default => default_message }
253
+
254
+ [:http, :https, :email, :email_json, :sqs].each do |protocol|
255
+ if options[protocol]
256
+ message[protocol.to_s.gsub(/_/, '-')] = options[protocol]
257
+ end
258
+ end
259
+
260
+ publish_opts = {}
261
+ publish_opts[:message] = message.to_json
262
+ publish_opts[:message_structure] = 'json'
263
+ publish_opts[:subject] = options[:subject] if options[:subject]
264
+ publish_opts[:topic_arn] = arn
265
+
266
+ response = client.publish(publish_opts)
267
+
268
+ response.message_id
269
+
270
+ end
271
+
272
+ # Deletes the topic.
273
+ # @return [nil]
274
+ def delete
275
+ client.delete_topic(:topic_arn => arn)
276
+ nil
277
+ end
278
+
279
+ # @return [Hash] Returns a hash of attributes about this topic,
280
+ # including:
281
+ #
282
+ # * +:arn+
283
+ # * +:name+
284
+ # * +:owner+
285
+ # * +:display_name+
286
+ # * +:policy+
287
+ # * +:num_subscriptions_confirmed+
288
+ # * +:num_subscriptions_pending+
289
+ # * +:num_subscriptions_deleted+
290
+ #
291
+ def to_h
292
+ attributes = client.get_topic_attributes(:topic_arn => arn).attributes
293
+ {
294
+ :arn => arn,
295
+ :name => name,
296
+ :owner => attributes['Owner'],
297
+ :display_name => attributes['DisplayName'] || name,
298
+ :policy => parse_policy(attributes['Policy']),
299
+ :num_subscriptions_confirmed => attributes['SubscriptionsConfirmed'].to_i,
300
+ :num_subscriptions_pending => attributes['SubscriptionsPending'].to_i,
301
+ :num_subscriptions_deleted => attributes['SubscriptionsDeleted'].to_i,
302
+ }
303
+ end
304
+
305
+ # @return [Boolean] Returns true if compared to another {Topic}
306
+ # with the same ARN.
307
+ def ==(other)
308
+ other.kind_of?(Topic) and other.arn == arn
309
+ end
310
+ alias_method :eql?, :==
311
+
312
+ # @private
313
+ protected
314
+ def parse_policy policy_json
315
+ if policy_json
316
+ policy = SNS::Policy.from_json(policy_json)
317
+ policy.extend(PolicyProxy)
318
+ policy.topic = self
319
+ policy
320
+ else
321
+ nil
322
+ end
323
+ end
324
+
325
+ # @private
326
+ protected
327
+ def set_attribute name, value
328
+ client.send(:set_topic_attributes, {
329
+ :topic_arn => arn,
330
+ :attribute_name => name,
331
+ :attribute_value => value,
332
+ })
333
+ end
334
+
335
+ # @private
336
+ module PolicyProxy
337
+ attr_accessor :topic
338
+ def change
339
+ yield(self)
340
+ topic.policy = self
341
+ end
342
+ end
343
+
344
+ # @private
345
+ protected
346
+ def endpoint_opts(endpoint, opts = {})
347
+
348
+ case
349
+ when endpoint.is_a?(SQS::Queue)
350
+
351
+ # auto add a policy to the queue to allow the topic
352
+ # to send the queue messages
353
+ unless opts[:update_policy] == false
354
+ policy = endpoint.policy || SQS::Policy.new
355
+ policy.allow(
356
+ :principal => :any,
357
+ :actions => [:send_message],
358
+ :resources => [endpoint]
359
+ ).where(:source_arn).is(arn)
360
+ endpoint.policy = policy
361
+ end
362
+
363
+ { :protocol => 'sqs', :endpoint => endpoint.arn }
364
+
365
+ when endpoint =~ /^arn:/
366
+ raise ArgumentError, "expected a queue ARN" unless
367
+ endpoint =~ /^arn:aws:sqs:/
368
+ { :protocol => "sqs", :endpoint => endpoint }
369
+ when endpoint.kind_of?(URI)
370
+ { :protocol => endpoint.scheme,
371
+ :endpoint => endpoint.to_s }
372
+ when endpoint =~ /^(https?):/
373
+ { :protocol => $1, :endpoint => endpoint }
374
+ when endpoint.include?("@")
375
+ { :protocol => opts[:json] ? "email-json" : "email",
376
+ :endpoint => endpoint }
377
+ else
378
+ raise ArgumentError, "could not determine protocol for '#{endpoint}'"
379
+ end
380
+ end
381
+
382
+ end
383
+ end
384
+ end