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.amazonwebservices.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.

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.



42
43
44
45
46
47
48
49
50
# File 'lib/aws/sns/message.rb', line 42

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.



39
40
41
# File 'lib/aws/sns/message.rb', line 39

def origin
  @origin
end

#rawObject (readonly)

Returns the value of attribute raw.



38
39
40
# File 'lib/aws/sns/message.rb', line 38

def raw
  @raw
end

Instance Method Details

#[](key) ⇒ String

Returns the value of the SNS' field.

Parameters:

  • indexer (String)

    into raw SNS JSON message

Returns:

  • (String)

    the value of the SNS' field



54
55
56
# File 'lib/aws/sns/message.rb', line 54

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



64
65
66
67
68
69
70
71
72
# File 'lib/aws/sns/message.rb', line 64

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



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

def message
  @raw['Message']
end

#message_idObject



87
88
89
# File 'lib/aws/sns/message.rb', line 87

def message_id
  @raw['MessageId']
end

#parse_from(json) ⇒ Object



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

def parse_from json
  JSON.parse json
end

#signatureObject



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

def signature
  @raw['Signature']
end

#signature_versionObject



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

def signature_version
  @raw['SignatureVersion']
end

#signing_cert_urlObject



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

def signing_cert_url
  @raw['SigningCertURL']
end

#subjectObject



95
96
97
# File 'lib/aws/sns/message.rb', line 95

def subject
  @raw['Subject']
end

#timestampObject



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

def timestamp
  @raw['Timestamp']
end

#topic_arnObject



91
92
93
# File 'lib/aws/sns/message.rb', line 91

def topic_arn
  @raw['TopicArn']
end

#typeObject

@return[Symbol] the message type



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

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



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

def unsubscribe_url
  @raw['UnsubscribeURL']
end