You are viewing documentation for version 1 of the AWS SDK for Ruby. Version 2 documentation can be found here.

Class: AWS::SNS::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/aws/sns/message.rb

Overview

Represents a single SNS message.

See also http://docs.aws.amazon.com/sns/latest/gsg/json-formats.html

= Originators Originators are sources of SNS messages. FromAutoScaling is one. Message can be extended by originators if their #applicable? method returns true when passed the raw message. Originator modules must implement applicable? sns module function. If an originator is applicable, it should set the @origin accessor to denote itself.

Constant Summary

SIGNABLE_KEYS =
[
  'Message',
  'MessageId',
  'Subject',
  'SubscribeURL',
  'Timestamp',
  'Token',
  'TopicArn',
  'Type',
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sns) ⇒ Message

Returns Constructs a new AWS::SNS::Message from the raw SNS, sets origin



55
56
57
58
59
60
61
62
63
# File 'lib/aws/sns/message.rb', line 55

def initialize sns
  if sns.is_a? String
    @raw = parse_from sns
  else
    @raw = sns
  end
  @origin = :sns
  self.extend FromAutoScaling if FromAutoScaling.applicable? @raw
end

Instance Attribute Details

#originObject

Returns the value of attribute origin



52
53
54
# File 'lib/aws/sns/message.rb', line 52

def origin
  @origin
end

#rawObject (readonly)

Returns the value of attribute raw



51
52
53
# File 'lib/aws/sns/message.rb', line 51

def raw
  @raw
end

Instance Method Details

#[](key) ⇒ String

Returns the value of the SNS' field

Parameters:

  • key (String)

    Indexer into raw SNS JSON message.

Returns:

  • (String)

    the value of the SNS' field



67
68
69
# File 'lib/aws/sns/message.rb', line 67

def [] key
  @raw[key]
end

#authentic?Boolean

Returns:

  • (Boolean)

    true when the AWS::SNS::Message is authentic: SigningCert is hosted at amazonaws.com, on https correctly cryptographically signed by sender nothing went wrong during authenticating the AWS::SNS::Message



77
78
79
80
81
82
83
84
85
# File 'lib/aws/sns/message.rb', line 77

def authentic?
  begin
    decoded_from_base64 = decode signature
    public_key = get_public_key_from signing_cert_url
    public_key.verify OpenSSL::Digest::SHA1.new, decoded_from_base64, canonical_string
  rescue MessageWasNotAuthenticError
    false
  end
end

#messageObject



112
113
114
# File 'lib/aws/sns/message.rb', line 112

def message
  @raw['Message']
end

#message_idObject



100
101
102
# File 'lib/aws/sns/message.rb', line 100

def message_id
  @raw['MessageId']
end

#parse_from(json) ⇒ Object



144
145
146
# File 'lib/aws/sns/message.rb', line 144

def parse_from json
  JSON.parse json
end

#signatureObject



120
121
122
# File 'lib/aws/sns/message.rb', line 120

def signature
  @raw['Signature']
end

#signature_versionObject



124
125
126
# File 'lib/aws/sns/message.rb', line 124

def signature_version
  @raw['SignatureVersion']
end

#signing_cert_urlObject



128
129
130
# File 'lib/aws/sns/message.rb', line 128

def signing_cert_url
  @raw['SigningCertURL']
end

#subjectObject



108
109
110
# File 'lib/aws/sns/message.rb', line 108

def subject
  @raw['Subject']
end

#subscribe_urlObject



132
133
134
# File 'lib/aws/sns/message.rb', line 132

def subscribe_url
  @raw['SubscribeURL']
end

#timestampObject



116
117
118
# File 'lib/aws/sns/message.rb', line 116

def timestamp
  @raw['Timestamp']
end

#tokenObject



136
137
138
# File 'lib/aws/sns/message.rb', line 136

def token
  @raw['Token']
end

#topic_arnObject



104
105
106
# File 'lib/aws/sns/message.rb', line 104

def topic_arn
  @raw['TopicArn']
end

#typeObject

@return[Symbol] the message type



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/aws/sns/message.rb', line 88

def type
  case when @raw['Type'] =~ /SubscriptionConfirmation/i
    then :SubscriptionConfirmation
  when @raw['Type'] =~ /Notification/i
    then :Notification
  when @raw['Type'] =~ /UnsubscribeConfirmation/i
    then :UnsubscribeConfirmation
  else
    :unknown
  end
end

#unsubscribe_urlObject



140
141
142
# File 'lib/aws/sns/message.rb', line 140

def unsubscribe_url
  @raw['UnsubscribeURL']
end