Skip to content

Commit a793af5

Browse files
committed
Clean up SNS examples (Ruby v3 AWS SDK)
1 parent b04b0de commit a793af5

9 files changed

+259
-164
lines changed

ruby/example_code/sns/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Amazon Cloudwatch JavaScript SDK v3 code examples
2+
Amazon Cloudwatch enables you to collect, access, and correlate this data on a single platform from across all your AWS resources, applications, and services.
3+
4+
## Code examples
5+
This is a workspace where you can find the following AWS SDK for JavaScript version 3 (v3) Amazon Cloudwatch examples:
6+
7+
- [Create a subscription](./sns-ruby-example-create-subscription.rb)
8+
- [Create a topic](./sns-ruby-example-create-topic.rb)
9+
- [Enable a resource](./sns-ruby-example-enable-resource.rb)
10+
- [Send a message](./sns-ruby-example-send-message.rb)
11+
- [List subscriptions](./sns-ruby-example-show-subscriptions.rb)
12+
- [List topics](./sns-ruby-example-show-topics.rb)
13+
14+
15+
## Getting started
16+
17+
1. Clone the [AWS Code Samples repo](https://github.com/awsdocs/aws-doc-sdk-examples) to your local environment.
18+
See [the Github documentation](https://docs.github.com/en/github/creating-cloning-and-archiving-repositories/cloning-a-repository) for
19+
instructions.
20+
21+
22+
## Resources
23+
- [AWS SDK for Ruby repo](https://github.com/aws/aws-sdk-ruby) .
24+
- [AWS SDK for Ruby Developer Guide](https://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/welcome.html)
25+
- [AWS SDK for Ruby v3 API Reference Guide](https://docs.aws.amazon.com/sdk-for-ruby/v3/api/)
26+
Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,47 @@
1-
# snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.]
2-
# snippet-sourceauthor:[Doug-AWS]
3-
# snippet-sourcedescription:[Subscribes a user to an SNS topic.]
4-
# snippet-keyword:[Amazon Simple Notification Service]
5-
# snippet-keyword:[topic method]
6-
# snippet-keyword:[Topic.subscribe method]
7-
# snippet-keyword:[Ruby]
8-
# snippet-sourcesyntax:[ruby]
9-
# snippet-service:[sns]
10-
# snippet-keyword:[Code Sample]
11-
# snippet-sourcetype:[full-example]
12-
# snippet-sourcedate:[2018-03-16]
13-
# Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
14-
#
15-
# This file is licensed under the Apache License, Version 2.0 (the "License").
16-
# You may not use this file except in compliance with the License. A copy of the
17-
# License is located at
18-
#
19-
# http://aws.amazon.com/apache2.0/
20-
#
21-
# This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
22-
# OF ANY KIND, either express or implied. See the License for the specific
23-
# language governing permissions and limitations under the License.
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
243

4+
# Purpose:
5+
# sns-ruby-example-create-subscription.rb demonstrates how to create an Amazon Simple Notification Services (SNS) subscription using
6+
# the AWS SDK for JavaScript (v3).
7+
8+
# Inputs:
9+
# - REGION
10+
# - SNS_TOPIC_ARN
11+
# - EMAIL_ADDRESS
12+
13+
# snippet-start:[sns.Ruby.createSubscription]
2514
require 'aws-sdk-sns' # v2: require 'aws-sdk'
2615

27-
sns = Aws::SNS::Resource.new(region: 'us-west-2')
16+
def subscription_created?(sns_client, topic_arn, protocol, endpoint)
17+
18+
sns_client.subscribe(topic_arn: topic_arn, protocol: protocol, endpoint: endpoint)
19+
20+
rescue StandardError => e
21+
puts "Error while creating the subscription: #{e.message}"
22+
end
23+
# snippet-end:[sns.Ruby.createSubscription]
24+
#
25+
# Full example call:
26+
def run_me
27+
28+
protocol = 'email'
29+
endpoint = '[email protected]'
30+
topic_arn = 'arn:aws:sns:eu-west-1:164794437551:brmurrubytopic'
31+
region = 'eu-west-1'
32+
33+
sns_client = Aws::SNS::Client.new(region: region)
34+
35+
puts "Creating the subscription."
2836

29-
topic = sns.topic('arn:aws:sns:us-west-2:123456789:MyGroovyTopic')
37+
if subscription_created?(sns_client, topic_arn, protocol, endpoint)
38+
puts 'The subscriptions was created.'
39+
else
40+
puts 'The subscription was not created. Stopping program.'
41+
exit 1
42+
end
43+
end
3044

31-
sub = topic.subscribe({
32-
protocol: 'email',
33-
endpoint: '[email protected]'
34-
})
45+
run_me if $PROGRAM_NAME == __FILE__
3546

36-
puts sub.arn
47+
# snippet-end:[sns.Ruby.createSubscription]
Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,40 @@
1-
# snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.]
2-
# snippet-sourceauthor:[Doug-AWS]
3-
# snippet-sourcedescription:[Creates an SNS topic.]
4-
# snippet-keyword:[Amazon Simple Notification Service]
5-
# snippet-keyword:[create_topic method]
6-
# snippet-keyword:[Ruby]
7-
# snippet-sourcesyntax:[ruby]
8-
# snippet-service:[sns]
9-
# snippet-keyword:[Code Sample]
10-
# snippet-sourcetype:[full-example]
11-
# snippet-sourcedate:[2018-03-16]
12-
# Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
13-
#
14-
# This file is licensed under the Apache License, Version 2.0 (the "License").
15-
# You may not use this file except in compliance with the License. A copy of the
16-
# License is located at
17-
#
18-
# http://aws.amazon.com/apache2.0/
19-
#
20-
# This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
21-
# OF ANY KIND, either express or implied. See the License for the specific
22-
# language governing permissions and limitations under the License.
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
233

4+
# Purpose:
5+
# sns-ruby-example-create-topic.rb demonstrates how to create an Amazon Simple Notification Services (SNS) topic using
6+
# the AWS SDK for JavaScript (v3).
7+
8+
# Inputs:
9+
# - REGION
10+
# - TOPIC_NAME
11+
12+
# snippet-start:[sns.Ruby.createTopic]
2413
require 'aws-sdk-sns' # v2: require 'aws-sdk'
2514

26-
sns = Aws::SNS::Resource.new(region: 'us-west-2')
15+
def topic_created?(sns_client, topic_name)
16+
17+
sns_client.create_topic(name: topic_name)
18+
rescue StandardError => e
19+
puts "Error while creating the topic named '#{topic_name}': #{e.message}"
20+
end
21+
# snippet-end:[sns.Ruby.createTopic]
22+
# Full example call:
23+
def run_me
24+
topic_name = 'doc-example-topic'
25+
region = 'eu-west-1'
26+
27+
sns_client = Aws::SNS::Client.new(region: region)
28+
29+
puts "Creating the topic '#{topic_name}'..."
30+
31+
if topic_created?(sns_client, topic_name)
32+
puts 'The topic was created.'
33+
else
34+
puts 'The topic was not created. Stopping program.'
35+
exit 1
36+
end
37+
end
2738

28-
topic = sns.create_topic(name: 'MyGroovyTopic')
29-
puts topic.arn
39+
run_me if $PROGRAM_NAME == __FILE__
40+
# snippet-end:[sns.Ruby.createTopic]
Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
1-
# snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.]
2-
# snippet-sourceauthor:[Doug-AWS]
3-
# snippet-sourcedescription:[Enables a resource to publish to an SNS topic.]
4-
# snippet-keyword:[Amazon Simple Notification Service]
5-
# snippet-keyword:[Resource.topic method]
6-
# snippet-keyword:[Topic.set_attributes method]
7-
# snippet-keyword:[Ruby]
8-
# snippet-sourcesyntax:[ruby]
9-
# snippet-service:[sns]
10-
# snippet-keyword:[Code Sample]
11-
# snippet-sourcetype:[full-example]
12-
# snippet-sourcedate:[2018-03-16]
13-
# Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
14-
#
15-
# This file is licensed under the Apache License, Version 2.0 (the "License").
16-
# You may not use this file except in compliance with the License. A copy of the
17-
# License is located at
18-
#
19-
# http://aws.amazon.com/apache2.0/
20-
#
21-
# This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
22-
# OF ANY KIND, either express or implied. See the License for the specific
23-
# language governing permissions and limitations under the License.
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
243

4+
# Purpose:
5+
# sns-ruby-example-enable-resource.rb demonstrates how to enable an Amazon Simple Notification Services (SNS) resource using
6+
# the AWS SDK for JavaScript (v3).
7+
8+
# Inputs:
9+
# - MY_TOPIC_ARN
10+
# - MY_RESOURCE_ARN
11+
# - REGION
12+
# - POLICY_NAME
13+
14+
# snippet-start:[sns.Ruby.enableResource]
2515
require 'aws-sdk-sns' # v2: require 'aws-sdk'
2616

2717
policy = '{
@@ -34,21 +24,22 @@
3424
"AWS":"*"
3525
},
3626
"Action":["SNS:Publish"],
37-
"Resource":"' + my-topic-arn + '",
27+
"Resource":"' + MY_TOPIC_ARN + '",
3828
"Condition":{
3929
"ArnEquals":{
40-
"AWS:SourceArn":"' + my-resource-arn + '"}
30+
"AWS:SourceArn":"' + MY_RESOURCE_ARN + '"}
4131
}
4232
}]
4333
}'
4434

45-
sns = Aws::SNS::Resource.new(region: 'us-west-2')
35+
sns = Aws::SNS::Resource.new(region: 'REGION')
4636

4737
# Get topic by ARN
48-
topic = sns.topic(my-topic-arn)
38+
topic = sns.topic()
4939

5040
# Add policy to topic
5141
topic.set_attributes({
52-
attribute_name: "Policy",
42+
attribute_name: "POLICY_NAME",
5343
attribute_value: policy
5444
})
45+
# snippet-end:[sns.Ruby.enableResource]
Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,42 @@
1-
# Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2-
#
3-
# This file is licensed under the Apache License, Version 2.0 (the "License").
4-
# You may not use this file except in compliance with the License. A copy of the
5-
# License is located at
6-
# snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.]
7-
# snippet-sourceauthor:[Doug-AWS]
8-
# snippet-sourcedescription:[Publishes a message to an SNS topic.]
9-
# snippet-keyword:[Amazon Simple Notification Service]
10-
# snippet-keyword:[Resource.topic method]
11-
# snippet-keyword:[Topic.publish method]
12-
# snippet-keyword:[Ruby]
13-
# snippet-sourcesyntax:[ruby]
14-
# snippet-service:[sns]
15-
# snippet-keyword:[Code Sample]
16-
# snippet-sourcetype:[full-example]
17-
# snippet-sourcedate:[2018-03-16]
18-
#
19-
# http://aws.amazon.com/apache2.0/
20-
#
21-
# This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
22-
# OF ANY KIND, either express or implied. See the License for the specific
23-
# language governing permissions and limitations under the License.
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
243

4+
# Purpose:
5+
# sns-ruby-example-send-message.rb demonstrates how to send a message using the Amazon Simple Notification Services (SNS) topic using
6+
# the AWS SDK for JavaScript (v3).
7+
8+
# Inputs:
9+
# - REGION
10+
# - SNS_TOPIC_ARN
11+
# - MESSAGE
12+
13+
# snippet-start:[sns.Ruby.sendMessage]
2514
require 'aws-sdk-sns' # v2: require 'aws-sdk'
2615

27-
sns = Aws::SNS::Resource.new(region: 'us-west-2')
16+
def message_sent?(sns_client, topic_arn, message)
17+
18+
sns_client.publish(topic_arn: topic_arn, message: message)
19+
rescue StandardError => e
20+
puts "Error while sending the message: #{e.message}"
21+
end
22+
# snippet-end:[sns.Ruby.sendMessage]
23+
def run_me
24+
25+
topic_arn = 'SNS_TOPIC_ARN'
26+
region = 'REGION'
27+
message = 'MESSAGE' # The text of the message to send.
28+
29+
sns_client = Aws::SNS::Client.new(region: region)
30+
31+
puts "Message sending."
2832

29-
topic = sns.topic('arn:aws:sns:us-west-2:123456789:MyGroovyTopic')
33+
if message_sent?(sns_client, topic_arn, message)
34+
puts 'The message was sent.'
35+
else
36+
puts 'The message was not sent. Stopping program.'
37+
exit 1
38+
end
39+
end
3040

31-
topic.publish({
32-
message: 'Hello!'
33-
})
41+
run_me if $PROGRAM_NAME == __FILE__
42+
# snippet-start:[sns.Ruby.sendMessage]
Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
1-
# snippet-comment:[These are tags for the AWS doc team's sample catalog. Do not remove.]
2-
# snippet-sourceauthor:[Doug-AWS]
3-
# snippet-sourcedescription:[Lists the endpoints for your SNS subscriptions.]
4-
# snippet-keyword:[Amazon Simple Notification Service]
5-
# snippet-keyword:[Resource.topic method]
6-
# snippet-keyword:[Topic.subscriptions method]
7-
# snippet-keyword:[Ruby]
8-
# snippet-sourcesyntax:[ruby]
9-
# snippet-service:[sns]
10-
# snippet-keyword:[Code Sample]
11-
# snippet-sourcetype:[full-example]
12-
# snippet-sourcedate:[2018-03-16]
13-
# Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
14-
#
15-
# This file is licensed under the Apache License, Version 2.0 (the "License").
16-
# You may not use this file except in compliance with the License. A copy of the
17-
# License is located at
18-
#
19-
# http://aws.amazon.com/apache2.0/
20-
#
21-
# This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
22-
# OF ANY KIND, either express or implied. See the License for the specific
23-
# language governing permissions and limitations under the License.
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
243

4+
# Purpose:
5+
# sns-ruby-example-show-subscriptions.rb demonstrates how to list subscriptions to the Amazon Simple Notification Services (SNS) topic using
6+
# the AWS SDK for JavaScript (v3).
7+
8+
# Inputs:
9+
# - REGION
10+
# - SNS_TOPIC
11+
12+
# snippet-start:[sns.Ruby.showSubscription]
2513
require 'aws-sdk-sns' # v2: require 'aws-sdk'
2614

27-
sns = Aws::SNS::Resource.new(region: 'us-west-2')
15+
def show_subscriptions?(sns_client, topic_arn)
16+
topic = sns_client.topic(topic_arn)
17+
topic.subscriptions.each do |s|
18+
puts s.attributes['Endpoint']
19+
end
20+
21+
rescue StandardError => e
22+
puts "Error while sending the message: #{e.message}"
23+
end
24+
# snippet-end:[sns.Ruby.showSubscription]
25+
def run_me
2826

29-
topic = sns.topic('arn:aws:sns:us-west-2:123456789:MyGroovyTopic')
27+
topic_arn = 'arn:aws:sns:eu-west-1:164794437551:brmurrubytopic'
28+
region = 'eu-west-1'
3029

31-
topic.subscriptions.each do |s|
32-
puts s.attributes['Endpoint']
30+
sns_client = Aws::SNS::Resource.new(region: region)
31+
32+
puts "Listing subscriptions to the topic."
33+
34+
if show_subscriptions?(sns_client, topic_arn)
35+
else
36+
puts 'There was an error. Stopping program.'
37+
exit 1
38+
end
3339
end
40+
41+
run_me if $PROGRAM_NAME == __FILE__

0 commit comments

Comments
 (0)