Class: Cloudenvoy::Message
- Inherits:
-
Object
- Object
- Cloudenvoy::Message
- Defined in:
- lib/cloudenvoy/message.rb
Overview
Represents a Pub/Sub message
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
-
#metadata ⇒ Object
Returns the value of attribute metadata.
-
#payload ⇒ Object
Returns the value of attribute payload.
-
#sub_uri ⇒ Object
Returns the value of attribute sub_uri.
-
#topic ⇒ String
Return the message topic.
Class Method Summary collapse
-
.from_descriptor(input_payload) ⇒ Cloudenvoy::Message
Return an instantiated message from a Pub/Sub webhook payload.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Equality operator.
-
#initialize(id: nil, payload: nil, metadata: nil, topic: nil, sub_uri: nil) ⇒ Message
constructor
Constructor.
-
#subscriber ⇒ Subscriber
Return the instantiated Subscriber designated to process this message.
-
#to_h ⇒ Hash
Return a hash description of the message.
Constructor Details
#initialize(id: nil, payload: nil, metadata: nil, topic: nil, sub_uri: nil) ⇒ Message
Constructor
37 38 39 40 41 42 43 |
# File 'lib/cloudenvoy/message.rb', line 37 def initialize(id: nil, payload: nil, metadata: nil, topic: nil, sub_uri: nil) @id = id @payload = payload @topic = topic @metadata = || {} @sub_uri = sub_uri end |
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
7 8 9 |
# File 'lib/cloudenvoy/message.rb', line 7 def id @id end |
#metadata ⇒ Object
Returns the value of attribute metadata.
7 8 9 |
# File 'lib/cloudenvoy/message.rb', line 7 def @metadata end |
#payload ⇒ Object
Returns the value of attribute payload.
7 8 9 |
# File 'lib/cloudenvoy/message.rb', line 7 def payload @payload end |
#sub_uri ⇒ Object
Returns the value of attribute sub_uri.
7 8 9 |
# File 'lib/cloudenvoy/message.rb', line 7 def sub_uri @sub_uri end |
#topic ⇒ String
Return the message topic.
50 51 52 53 54 55 |
# File 'lib/cloudenvoy/message.rb', line 50 def topic return @topic if @topic return nil unless sub_uri Subscriber.parse_sub_uri(sub_uri)[1] end |
Class Method Details
.from_descriptor(input_payload) ⇒ Cloudenvoy::Message
Return an instantiated message from a Pub/Sub webhook payload.
the message to process.
18 19 20 21 22 23 24 25 26 |
# File 'lib/cloudenvoy/message.rb', line 18 def self.from_descriptor(input_payload) # Build new message new( id: input_payload.dig('message', 'message_id'), payload: JSON.parse(Base64.decode64(input_payload.dig('message', 'data'))), metadata: input_payload.dig('message', 'attributes'), sub_uri: input_payload['subscription'] ) end |
Instance Method Details
#==(other) ⇒ Boolean
Equality operator.
92 93 94 |
# File 'lib/cloudenvoy/message.rb', line 92 def ==(other) other.is_a?(self.class) && other.id == id end |
#subscriber ⇒ Subscriber
Return the instantiated Subscriber designated to process this message.
62 63 64 65 66 67 68 |
# File 'lib/cloudenvoy/message.rb', line 62 def subscriber @subscriber ||= begin return nil unless sub_uri && (klass = Subscriber.from_sub_uri(sub_uri)) klass.new(message: self) end end |
#to_h ⇒ Hash
Return a hash description of the message.
75 76 77 78 79 80 81 82 83 |
# File 'lib/cloudenvoy/message.rb', line 75 def to_h { id: id, payload: payload.dup, metadata: .dup, topic: topic, sub_uri: sub_uri }.compact end |