Skip to content

Instantly share code, notes, and snippets.

@mmassaki
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save mmassaki/ce065fb7ad42b1ffda5b to your computer and use it in GitHub Desktop.

Select an option

Save mmassaki/ce065fb7ad42b1ffda5b to your computer and use it in GitHub Desktop.
Publish message to AWS SNS
require 'aws-sdk'
require 'json'
# from: http://stackoverflow.com/a/11482430
class String
# colorization
def colorize(color_code)
"\e[#{color_code}m#{self}\e[0m"
end
def red
colorize(31)
end
def green
colorize(32)
end
def yellow
colorize(33)
end
def pink
colorize(35)
end
end
if ARGV.size != 2
puts "usage: ruby #{$0} <aws_access_key> <aws_secret_key>"
exit 127
end
access_key = ARGV[0]
secret_key = ARGV[1]
ARGV.clear
finish = false
while !finish
puts "Publishing new message"
print "Target ARN: "
target_arn = gets.chomp
print "Text message: "
text = gets.chomp
payload = {
aps: {
alert: text,
sound: 'default',
}
}
payload_json = payload.to_json
message = {
default: text,
APNS: payload_json
}
sns = AWS::SNS.new(
access_key_id: access_key,
secret_access_key: secret_key
)
chars_exceeded = payload_json.size - 256
if chars_exceeded > 0
puts
puts "ERROR: Your message is too big. It's exceeding the maximum allowed"\
" by #{chars_exceeded} character(s)".red
puts "Message not published."
else
puts
puts "==================================="
puts "Target ARN: #{target_arn}"
puts "Text message: #{text}"
puts "==================================="
puts
puts "Publish the message? Type 'yes' to confirm"
confirm = gets.chomp
if confirm == 'yes'
res = sns.client.publish(
target_arn: target_arn,
message: message.to_json,
message_structure: 'json',
)
puts "Message sent."
puts res.data.to_yaml
else
puts "Message not published."
end
end
puts
puts "Publish another message? (y/n)"
another = gets.chomp.downcase
if another == 'y' or another == 'yes'
puts
else
finish = true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment