class Aws::Rails::ActionMailbox::SnsNotification

@api private

Public Class Methods

new(request_body) click to toggle source
# File lib/aws/rails/action_mailbox/sns_notification.rb, line 14
def initialize(request_body)
  @request_body = request_body
end

Public Instance Methods

message_content() click to toggle source
# File lib/aws/rails/action_mailbox/sns_notification.rb, line 34
def message_content
  raise MessageContentError, 'Incoming emails must have notificationType `Received`' unless receipt?

  if content_in_s3?
    s3_content
  else
    return message[:content] unless destination

    "X-Original-To: #{destination}\n#{message[:content]}"
  end
end
subscription_confirmed?() click to toggle source
# File lib/aws/rails/action_mailbox/sns_notification.rb, line 18
def subscription_confirmed?
  (200..299).cover?(confirmation_response.code.to_i)
end
topic() click to toggle source
# File lib/aws/rails/action_mailbox/sns_notification.rb, line 26
def topic
  notification.fetch(:TopicArn)
end
type() click to toggle source
# File lib/aws/rails/action_mailbox/sns_notification.rb, line 30
def type
  notification.fetch(:Type)
end
verified?() click to toggle source
# File lib/aws/rails/action_mailbox/sns_notification.rb, line 22
def verified?
  SnsMessageVerifier.verifier.authentic?(@request_body)
end

Private Instance Methods

action() click to toggle source
# File lib/aws/rails/action_mailbox/sns_notification.rb, line 71
def action
  return unless message[:receipt]

  message.fetch(:receipt).fetch(:action)
end
bucket() click to toggle source
# File lib/aws/rails/action_mailbox/sns_notification.rb, line 77
def bucket
  action.fetch(:bucketName)
end
confirmation_response() click to toggle source
# File lib/aws/rails/action_mailbox/sns_notification.rb, line 93
def confirmation_response
  @confirmation_response ||= Net::HTTP.get_response(URI(notification[:SubscribeURL]))
end
content_in_s3?() click to toggle source
# File lib/aws/rails/action_mailbox/sns_notification.rb, line 85
def content_in_s3?
  action&.fetch(:type) == 'S3'
end
destination() click to toggle source
# File lib/aws/rails/action_mailbox/sns_notification.rb, line 67
def destination
  message.dig(:mail, :destination)&.first
end
key() click to toggle source
# File lib/aws/rails/action_mailbox/sns_notification.rb, line 81
def key
  action.fetch(:objectKey)
end
message() click to toggle source
# File lib/aws/rails/action_mailbox/sns_notification.rb, line 63
def message
  @message ||= JSON.parse(notification[:Message], symbolize_names: true)
end
notification() click to toggle source
# File lib/aws/rails/action_mailbox/sns_notification.rb, line 48
def notification
  @notification ||= JSON.parse(@request_body, symbolize_names: true)
rescue JSON::ParserError => e
  Rails.logger.warn("Unable to parse SNS notification: #{e}")
  nil
end
receipt?() click to toggle source
# File lib/aws/rails/action_mailbox/sns_notification.rb, line 89
def receipt?
  message.fetch(:notificationType) == 'Received'
end
s3_content() click to toggle source
# File lib/aws/rails/action_mailbox/sns_notification.rb, line 55
def s3_content
  S3Client
    .client
    .get_object(key: key, bucket: bucket)
    .body
    .string
end