Created
May 18, 2016 18:34
-
-
Save anoldguy/90c2b355450921769354652d95122a5c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Notification | |
require 'aws-sdk' | |
require 'httparty' | |
attr_accessor :message_info, :verifier, :message_body | |
def initialize(message_body, logger) | |
@message_info = JSON.parse(message_body) | |
@message_body = message_body | |
@verifier = ::Aws::SNS::MessageVerifier.new | |
@logger = logger | |
end | |
def confirm! | |
response = ::HTTParty.get(self.SubscribeURL) | |
response.code == 200 | |
end | |
def valid? | |
@valid ||= @verifier.authentic?(@message_body) | |
end | |
def message | |
@message ||= case JSON.parse(@message_info["Message"])["notificationType"] | |
when "TYPE_A" | |
CustomANotification.new(@message_info["Message"]) | |
when "Delivery" | |
CustomBNotification.new(@message_info["Message"]) | |
else | |
"" | |
end | |
end | |
def method_missing(method_sym, *arguments, &block) | |
@message_info.try(:[], method_sym.to_s) | |
end | |
def process | |
case self.Type | |
when "SubscriptionConfirmation" | |
@logger.debug "Subscription Request - #{self.SubscribeURL}" | |
self.confirm! | |
when "Notification" | |
self.message.process | |
else | |
@logger.debug "Unknown type: #{self.Type}" | |
end | |
end | |
end | |
class CustomANotification | |
def initialize(message) | |
@info = JSON.parse(message) | |
@logger = Rails.configuration.aws_logger | |
end | |
def method_missing(method_sym, *arguments, &block) | |
@info.try(:[], method_sym.to_s) | |
end | |
def process | |
# Custom logic goes here (send a notification?) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment