Skip to content

Instantly share code, notes, and snippets.

@atinder
Forked from swistak/example_notification.json
Created October 3, 2018 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atinder/931681b1b4cb07bf32ee531197ae52fa to your computer and use it in GitHub Desktop.
Save atinder/931681b1b4cb07bf32ee531197ae52fa to your computer and use it in GitHub Desktop.
SNS in Rails
{
"Type" : "SubscriptionConfirmation",
"MessageId" : "0941b7b3-4797-44e5-8079-dbf8ef09793f",
"Token" : "2336412f37fb687f5d51e6e241d164b0533302ec696efc8f113cd2252ce8a925c1e44a3c4ca2984675da85b9bd8c05da85337beb1f24a9c659b22ed3a8cc682f3457c4ead28d392a40e5c35c0e9>
"TopicArn" : "arn:aws:sns:us-west-2:164053224164:email-list",
"Message" : "You have chosen to subscribe to the topic arn:aws:sns:us-west-2:164053224164:email-list.\nTo confirm the subscription, visit the SubscribeURL included in this message",
"SubscribeURL" : "https://sns.us-west-2.amazonaws.com/?Action=ConfirmSubscription&TopicArn=arn:aws:sns:us-west-2:164053224164:email-list&Token=REDACTED",
"Timestamp" : "2013-10-05T17:00:04.135Z",
"SignatureVersion" : "1",
"Signature" : "<REDACTED>"
"SigningCertURL" : "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-e372f8ca30337fdb084e8ac449342c77.pem"
}
# id :integer not null, primary key
# raw :text
# created_at :datetime not null
# updated_at :datetime not null
# type :string(255)
# message_id :string(255)
# topic_arn :string(255)
# message :text
# subscribe_url :text
# subscribed :boolean
# notification_type :string(255)
#
require 'uri'
require 'net/http'
# http://docs.aws.amazon.com/ses/latest/DeveloperGuide/notification-contents.html#complaint-object
# http://docs.aws.amazon.com/ses/latest/DeveloperGuide/notification-contents.html#bounce-object
# http://docs.aws.amazon.com/ses/latest/DeveloperGuide/notification-contents.html#mail-object
#
class Notification < ActiveRecord::Base
self.inheritance_column = nil
scope :subscriptions, where(type: "SubscriptionConfirmation")
scope :notifications, where(type: "Notification")
attr_accessible :raw
before_create :extract_data
def data
@data ||= JSON.parse(raw)
end
def message_data
@message_data ||= JSON.parse(message)
end
def extract_data
self.type ||= data["Type"]
self.message_id ||= data["MessageId"]
self.topic_arn ||= data["TopicArn"]
self.subscribe_url ||= data["SubscribeURL"]
self.message ||= data["Message"]
self
end
# Types of notifications
def confirmation?() type == "SubscriptionConfirmation" end
def notification?() type == "Notification" end
# Confirms subscription.
def confirm!
uri = URI.parse(subscribe_url)
res = ::Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https', :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |https|
https.request(Net::HTTP::Get.new(uri.request_uri))
end
self.subscribed = res.is_a?(Net::HTTPOK)
end
end
# routes.rb:
#
# resources :notifications, only: [:create]
#
class NotificationsController < ApplicationController
# POST /notifications
# POST /notifications.json
def create
@notification = Notification.new(raw: request.raw_post)
@notification.extract_data
@notification.process!
respond_to do |format|
if @notification.save
format.html { render text: "Notification received!", layout: false }
format.json { render json: @notification, status: :created, location: @notification }
else
format.html { render text: "Cannot process notification", status: :unprocessable_entity, layout: false }
format.json { render json: @notification.errors, status: :unprocessable_entity }
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment