Class: AWS::SNS::Topic
- Inherits:
-
Object
- Object
- AWS::SNS::Topic
- Includes:
- HasDeliveryPolicy
- Defined in:
- lib/aws/sns/topic.rb
Instance Attribute Summary (collapse)
-
- (String) arn
readonly
The topic ARN.
Instance Method Summary (collapse)
-
- (Subscription) confirm_subscription(token, options = {})
Verifies an endpoint owner's intent to receive messages by validating the token sent to the endpoint by an earlier Subscribe action.
-
- (nil) delete
Deletes the topic.
-
- (nil, String<JSON>) delivery_policy_json
The delivery policy JSON string.
-
- (String) display_name
Returns the human-readable name used in the "From" field for notifications to email and email-json endpoints.
-
- (String) display_name=(display_name)
Returns the display_name as passed.
-
- (String<JSON>) effective_delivery_policy_json
The effective delivery policy JSON string.
-
- (Boolean) eql?(other)
(also: #==)
Returns true if compared to another Topic with the same ARN.
-
- (Topic) initialize(arn, options = {})
constructor
A new instance of Topic.
-
- (String) name
The topic name.
-
- (Integer) num_subscriptions_confirmed
Returns number of confirmed topic subscriptions.
-
- (Integer) num_subscriptions_deleted
Returns number of deleted topic subscriptions.
-
- (Integer) num_subscriptions_pending
Returns number of pending topic subscriptions.
-
- (String) owner
The topic owner's ID.
-
- (Policy) policy
The topic's Policy.
-
- (nil) policy=(policy)
Sets the topic's policy.
-
- (String) publish(default_message, options = {})
Publishes a message to this SNS topic.
-
- (Subscription?) subscribe(endpoint, options = {})
Causes the given
endpointto receive messages published to this topic. -
- (TopicSubscriptionCollection) subscriptions
Returns a collection that represents all of the subscriptions for this topic.
-
- (Hash) to_h
Returns a hash of attributes about this topic, including:.
Methods included from HasDeliveryPolicy
#delivery_policy, #delivery_policy=, #effective_delivery_policy
Constructor Details
- (Topic) initialize(arn, options = {})
A new instance of Topic
25 26 27 28 |
# File 'lib/aws/sns/topic.rb', line 25 def initialize arn, = {} @arn = arn super end |
Instance Attribute Details
- (String) arn (readonly)
The topic ARN.
31 32 33 |
# File 'lib/aws/sns/topic.rb', line 31 def arn @arn end |
Instance Method Details
- (Subscription) confirm_subscription(token, options = {})
Verifies an endpoint owner's intent to receive messages by validating the token sent to the endpoint by an earlier Subscribe action. If the token is valid, the action creates a new subscription.
135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/aws/sns/topic.rb', line 135 def confirm_subscription token, = {} [:authenticate_on_unsubscribe] = 'true' if [:authenticate_on_unsubscribe] confirm_opts = .merge(:token => token, :topic_arn => arn) resp = client.confirm_subscription(confirm_opts) Subscription.new( resp[:subscription_arn], :topic_arn => arn, :config => config) end |
- (nil) delete
Deletes the topic.
285 286 287 288 |
# File 'lib/aws/sns/topic.rb', line 285 def delete client.delete_topic(:topic_arn => arn) nil end |
- (nil, String<JSON>) delivery_policy_json
The delivery policy JSON string.
209 210 211 |
# File 'lib/aws/sns/topic.rb', line 209 def delivery_policy_json to_h[:delivery_policy_json] end |
- (String) display_name
Returns the human-readable name used in the "From" field for notifications to email and email-json endpoints. If you have not set the display name the topic #name will be used/returned instead.
159 160 161 |
# File 'lib/aws/sns/topic.rb', line 159 def display_name to_h[:display_name] end |
- (String) display_name=(display_name)
Returns the display_name as passed.
167 168 169 170 |
# File 'lib/aws/sns/topic.rb', line 167 def display_name= display_name set_attribute('DisplayName', display_name) display_name end |
- (String<JSON>) effective_delivery_policy_json
The effective delivery policy JSON string. into account system defaults.
215 216 217 |
# File 'lib/aws/sns/topic.rb', line 215 def effective_delivery_policy_json to_h[:effective_delivery_policy_json] end |
- (Boolean) eql?(other) Also known as: ==
Returns true if compared to another AWS::SNS::Topic with the same ARN.
320 321 322 |
# File 'lib/aws/sns/topic.rb', line 320 def eql? other other.kind_of?(Topic) and other.arn == arn end |
- (String) name
The topic name.
If you have not set a display name (see #display_name=) then this is used as the "From" field for notifications to email and email-json endpoints.
39 40 41 |
# File 'lib/aws/sns/topic.rb', line 39 def name arn.split(/:/)[-1] end |
- (Integer) num_subscriptions_confirmed
Returns number of confirmed topic subscriptions.
178 179 180 |
# File 'lib/aws/sns/topic.rb', line 178 def num_subscriptions_confirmed to_h[:num_subscriptions_confirmed] end |
- (Integer) num_subscriptions_deleted
Returns number of deleted topic subscriptions.
188 189 190 |
# File 'lib/aws/sns/topic.rb', line 188 def num_subscriptions_deleted to_h[:num_subscriptions_deleted] end |
- (Integer) num_subscriptions_pending
Returns number of pending topic subscriptions.
183 184 185 |
# File 'lib/aws/sns/topic.rb', line 183 def num_subscriptions_pending to_h[:num_subscriptions_pending] end |
- (String) owner
The topic owner's ID.
173 174 175 |
# File 'lib/aws/sns/topic.rb', line 173 def owner to_h[:owner] end |
- (Policy) policy
The topic's Policy.
193 194 195 |
# File 'lib/aws/sns/topic.rb', line 193 def policy to_h[:policy] end |
- (nil) policy=(policy)
Sets the topic's policy.
202 203 204 205 206 |
# File 'lib/aws/sns/topic.rb', line 202 def policy= policy policy_json = policy.is_a?(String) ? policy : policy.to_json set_attribute('Policy', policy_json) nil end |
- (String) publish(default_message, options = {})
Publishes a message to this SNS topic.
topic.publish('a short message')
You can pass a subject that is used when sending the message to email endpoints:
topic.publish('message', :subject => 'SNS message subject')
If you would like to pass a different message to various protocols (endpoint types) you can pass those as options:
topic.publish('default message',
:http => "message sent to http endpoints",
:https => "message sent to https endpoints",
:email => "message sent to email endpoints")
The full list of acceptable protocols are listed below. The default message is sent to endpoints who's protocol was not listed.
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/aws/sns/topic.rb', line 261 def publish , = {} = { :default => } [:http, :https, :email, :email_json, :sqs].each do |protocol| if [protocol] [protocol.to_s.gsub(/_/, '-')] = [protocol] end end publish_opts = {} publish_opts[:message] = .to_json publish_opts[:message_structure] = 'json' publish_opts[:subject] = [:subject] if [:subject] publish_opts[:topic_arn] = arn response = client.publish(publish_opts) response[:message_id] end |
- (Subscription?) subscribe(endpoint, options = {})
Causes the given endpoint to receive messages published to this
topic.
Subscribing to SQS Queues
If you subscribe to an SQS queue (with a AWS::SQS::Queue object} then a policy will be added/updated to the queue that will permit this topic to send it messages. Some important notes:
If you subscribe with a queue by ARN then you must change the policy yourself.
If you do not want the policy modified then pass
:update_policyas false or just pass the queue's arntopic.subscribe(queue.arn) topic.subscribe(queue, :update_policy => false)
108 109 110 111 112 113 114 115 116 |
# File 'lib/aws/sns/topic.rb', line 108 def subscribe endpoint, = {} subscribe_opts = endpoint_opts(endpoint, ).merge(:topic_arn => arn) resp = client.subscribe(subscribe_opts) if arn = resp[:subscription_arn] and arn =~ /^arn:/ Subscription.new(arn, :config => config) else nil end end |
- (TopicSubscriptionCollection) subscriptions
Returns a collection that represents all of the subscriptions for this topic.
151 152 153 |
# File 'lib/aws/sns/topic.rb', line 151 def subscriptions TopicSubscriptionCollection.new(self) end |
- (Hash) to_h
Returns a hash of attributes about this topic, including:
:arn:name:owner:display_name:policy:num_subscriptions_confirmed:num_subscriptions_pending:num_subscriptions_deleted
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/aws/sns/topic.rb', line 302 def to_h attributes = client.get_topic_attributes(:topic_arn => arn).attributes { :arn => arn, :name => name, :owner => attributes['Owner'], :display_name => attributes['DisplayName'] || name, :policy => parse_policy(attributes['Policy']), :num_subscriptions_confirmed => attributes['SubscriptionsConfirmed'].to_i, :num_subscriptions_pending => attributes['SubscriptionsPending'].to_i, :num_subscriptions_deleted => attributes['SubscriptionsDeleted'].to_i, :delivery_policy_json => attributes['DeliveryPolicy'], :effective_delivery_policy_json => attributes['EffectiveDeliveryPolicy'], } end |