class Aws::Rails::ActionMailbox::SnsNotification

@api private

def action

def action
  return unless message[:receipt]
  message.fetch(:receipt).fetch(:action)
end

def bucket

def bucket
  action.fetch(:bucketName)
end

def confirmation_response

def confirmation_response
  @confirmation_response ||= Net::HTTP.get_response(URI(notification[:SubscribeURL]))
end

def content_in_s3?

def content_in_s3?
  action&.fetch(:type) == 'S3'
end

def destination

def destination
  message.dig(:mail, :destination)&.first
end

def initialize(request_body)

def initialize(request_body)
  @request_body = request_body
end

def key

def key
  action.fetch(:objectKey)
end

def message

def message
  @message ||= JSON.parse(notification[:Message], symbolize_names: true)
end

def message_content

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

def notification

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

def receipt?

def receipt?
  message.fetch(:notificationType) == 'Received'
end

def s3_content

def s3_content
  S3Client
    .client
    .get_object(key: key, bucket: bucket)
    .body
    .string
end

def subscription_confirmed?

def subscription_confirmed?
  (200..299).cover?(confirmation_response.code.to_i)
end

def topic

def topic
  notification.fetch(:TopicArn)
end

def type

def type
  notification.fetch(:Type)
end

def verified?

def verified?
  SnsMessageVerifier.verifier.authentic?(@request_body)
end