Class: AWS::SNS::Topic
- Inherits:
-
Object
- Object
- AWS::SNS::Topic
- Defined in:
- lib/aws/sns/topic.rb
Instance Attribute Summary collapse
-
#arn ⇒ String
readonly
The topic ARN.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Returns true if compared to another Topic with the same ARN.
-
#confirm_subscription(token, opts = {}) ⇒ Subscription
Verifies an endpoint owner’s intent to receive messages by validating the token sent to the endpoint by an earlier Subscribe action.
-
#delete ⇒ nil
Deletes the topic.
-
#display_name ⇒ String
Returns the human-readable name used in the “From” field for notifications to email and email-json endpoints.
-
#display_name=(display_name) ⇒ String
Returns the display_name as passed.
-
#initialize(arn, options = {}) ⇒ Topic
constructor
A new instance of Topic.
-
#name ⇒ String
The topic name.
-
#num_subscriptions_confirmed ⇒ Integer
Returns number of confirmed topic subscriptions.
-
#num_subscriptions_deleted ⇒ Integer
Returns number of deleted topic subscriptions.
-
#num_subscriptions_pending ⇒ Integer
Returns number of pending topic subscriptions.
-
#owner ⇒ String
The topic owner’s ID.
-
#policy ⇒ Policy
The topic’s Policy.
-
#policy=(policy) ⇒ nil
Sets the topic’s policy.
-
#publish(default_message, options = {}) ⇒ String
Publishes a message to this SNS topic.
-
#subscribe(endpoint, opts = {}) ⇒ Subscription?
Causes the given
endpoint
to receive messages published to this topic. -
#subscriptions ⇒ TopicSubscriptionCollection
Returns a collection that represents all of the subscriptions for this topic.
-
#to_h ⇒ Hash
Returns a hash of attributes about this topic, including:.
Constructor Details
#initialize(arn, options = {}) ⇒ Topic
Returns a new instance of Topic.
24 25 26 27 |
# File 'lib/aws/sns/topic.rb', line 24 def initialize arn, = {} @arn = arn super end |
Instance Attribute Details
#arn ⇒ String (readonly)
Returns The topic ARN.
30 31 32 |
# File 'lib/aws/sns/topic.rb', line 30 def arn @arn end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Returns true if compared to another AWS::SNS::Topic with the same ARN.
305 306 307 |
# File 'lib/aws/sns/topic.rb', line 305 def ==(other) other.kind_of?(Topic) and other.arn == arn end |
#confirm_subscription(token, opts = {}) ⇒ Subscription
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.
138 139 140 141 142 143 144 145 |
# File 'lib/aws/sns/topic.rb', line 138 def confirm_subscription(token, opts = {}) confirm_opts = opts.merge(:token => token, :topic_arn => arn) resp = client.confirm_subscription(confirm_opts) Subscription.new( resp.subscription_arn, :topic => self, :config => config) end |
#delete ⇒ nil
Deletes the topic.
272 273 274 275 |
# File 'lib/aws/sns/topic.rb', line 272 def delete client.delete_topic(:topic_arn => arn) nil end |
#display_name ⇒ String
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.
157 158 159 |
# File 'lib/aws/sns/topic.rb', line 157 def display_name to_h[:display_name] end |
#display_name=(display_name) ⇒ String
Returns the display_name as passed.
165 166 167 168 |
# File 'lib/aws/sns/topic.rb', line 165 def display_name= display_name set_attribute('DisplayName', display_name) display_name end |
#name ⇒ String
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.
38 39 40 |
# File 'lib/aws/sns/topic.rb', line 38 def name arn.split(/:/)[-1] end |
#num_subscriptions_confirmed ⇒ Integer
Returns number of confirmed topic subscriptions.
176 177 178 |
# File 'lib/aws/sns/topic.rb', line 176 def num_subscriptions_confirmed to_h[:num_subscriptions_confirmed] end |
#num_subscriptions_deleted ⇒ Integer
Returns number of deleted topic subscriptions.
186 187 188 |
# File 'lib/aws/sns/topic.rb', line 186 def num_subscriptions_deleted to_h[:num_subscriptions_deleted] end |
#num_subscriptions_pending ⇒ Integer
Returns number of pending topic subscriptions.
181 182 183 |
# File 'lib/aws/sns/topic.rb', line 181 def num_subscriptions_pending to_h[:num_subscriptions_pending] end |
#owner ⇒ String
Returns The topic owner’s ID.
171 172 173 |
# File 'lib/aws/sns/topic.rb', line 171 def owner to_h[:owner] end |
#policy ⇒ Policy
Returns The topic’s Policy.
191 192 193 |
# File 'lib/aws/sns/topic.rb', line 191 def policy to_h[:policy] end |
#policy=(policy) ⇒ nil
Sets the topic’s policy.
200 201 202 203 204 |
# File 'lib/aws/sns/topic.rb', line 200 def policy= policy policy_json = policy.is_a?(String) ? policy : policy.to_json set_attribute('Policy', policy_json) nil end |
#publish(default_message, options = {}) ⇒ String
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.
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/aws/sns/topic.rb', line 248 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. end |
#subscribe(endpoint, opts = {}) ⇒ Subscription?
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_policy
as false or just pass the queue’s arntopic.subscribe(queue.arn) topic.subscribe(queue, :update_policy => false)
107 108 109 110 111 112 113 114 115 |
# File 'lib/aws/sns/topic.rb', line 107 def subscribe endpoint, opts = {} subscribe_opts = endpoint_opts(endpoint, opts).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 |
#subscriptions ⇒ TopicSubscriptionCollection
Returns a collection that represents all of the subscriptions for this topic.
149 150 151 |
# File 'lib/aws/sns/topic.rb', line 149 def subscriptions TopicSubscriptionCollection.new(self) end |
#to_h ⇒ Hash
Returns a hash of attributes about this topic, including:
-
:arn
-
:name
-
:owner
-
:display_name
-
:policy
-
:num_subscriptions_confirmed
-
:num_subscriptions_pending
-
:num_subscriptions_deleted
289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/aws/sns/topic.rb', line 289 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, } end |