Class: RailwayIpc::IncomingMessage
- Inherits:
-
Object
- Object
- RailwayIpc::IncomingMessage
- Defined in:
- lib/railway_ipc/incoming_message.rb
Defined Under Namespace
Classes: InvalidMessage, ParserError
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#parsed_payload ⇒ Object
readonly
Returns the value of attribute parsed_payload.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #correlation_id ⇒ Object
- #decoded ⇒ Object
-
#initialize(payload) ⇒ IncomingMessage
constructor
A new instance of IncomingMessage.
- #stringify_errors ⇒ Object
- #user_uuid ⇒ Object
- #uuid ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(payload) ⇒ IncomingMessage
Returns a new instance of IncomingMessage.
7 8 9 10 11 12 13 14 |
# File 'lib/railway_ipc/incoming_message.rb', line 7 def initialize(payload) @parsed_payload = JSON.parse(payload) @type = parsed_payload['type'] @payload = payload @errors = {} rescue JSON::ParserError => e raise RailwayIpc::IncomingMessage::ParserError.new(e) end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
5 6 7 |
# File 'lib/railway_ipc/incoming_message.rb', line 5 def errors @errors end |
#parsed_payload ⇒ Object (readonly)
Returns the value of attribute parsed_payload.
5 6 7 |
# File 'lib/railway_ipc/incoming_message.rb', line 5 def parsed_payload @parsed_payload end |
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
5 6 7 |
# File 'lib/railway_ipc/incoming_message.rb', line 5 def payload @payload end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
5 6 7 |
# File 'lib/railway_ipc/incoming_message.rb', line 5 def type @type end |
Instance Method Details
#correlation_id ⇒ Object
24 25 26 |
# File 'lib/railway_ipc/incoming_message.rb', line 24 def correlation_id decoded.correlation_id end |
#decoded ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/railway_ipc/incoming_message.rb', line 34 def decoded @decoded ||= begin protobuf_msg = Base64.decode64(parsed_payload['encoded_message']) decoder = Kernel.const_get(type) decoder.decode(protobuf_msg) rescue Google::Protobuf::ParseError => e raise RailwayIpc::IncomingMessage::ParserError.new(e) rescue NameError RailwayIpc::Messages::Unknown.decode(protobuf_msg) end end |
#stringify_errors ⇒ Object
47 48 49 |
# File 'lib/railway_ipc/incoming_message.rb', line 47 def stringify_errors errors.values.join(', ') end |
#user_uuid ⇒ Object
20 21 22 |
# File 'lib/railway_ipc/incoming_message.rb', line 20 def user_uuid decoded.user_uuid end |
#uuid ⇒ Object
16 17 18 |
# File 'lib/railway_ipc/incoming_message.rb', line 16 def uuid decoded.uuid end |
#valid? ⇒ Boolean
28 29 30 31 32 |
# File 'lib/railway_ipc/incoming_message.rb', line 28 def valid? errors[:uuid] = 'uuid is required' unless uuid.present? errors[:correlation_id] = 'correlation_id is required' unless correlation_id.present? errors.none? end |