Class: EventHub::Message
- Inherits:
-
Object
- Object
- EventHub::Message
- Includes:
- Helper
- Defined in:
- lib/eventhub/message.rb
Overview
Message class
Constant Summary collapse
- VERSION =
"1.0.0".freeze
- REQUIRED_HEADERS =
Headers that are required (value can be nil) in order to pass valid?
[ "message_id", "version", "created_at", "origin.module_id", "origin.type", "origin.site_id", "process.name", "process.step_position", "process.execution_id", "status.retried_count", "status.code", "status.message" ].freeze
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#header ⇒ Object
Returns the value of attribute header.
-
#raw ⇒ Object
Returns the value of attribute raw.
-
#routing_key ⇒ Object
Returns the value of attribute routing_key.
-
#vhost ⇒ Object
Returns the value of attribute vhost.
Class Method Summary collapse
Instance Method Summary collapse
- #append_to_execution_history(processor_name) ⇒ Object
-
#copy(status_code = STATUS_SUCCESS) ⇒ Object
copies the message and set’s provided status code (default: success), actual stamp, and a new message id.
- #initial? ⇒ Boolean
-
#initialize(header = nil, body = nil, raw = nil) ⇒ Message
constructor
A new instance of Message.
- #invalid? ⇒ Boolean
- #retry? ⇒ Boolean
- #retry_pending? ⇒ Boolean
- #schedule? ⇒ Boolean
- #schedule_pending? ⇒ Boolean
- #schedule_retry? ⇒ Boolean
- #success? ⇒ Boolean
- #to_json ⇒ Object
- #to_s ⇒ Object
- #valid? ⇒ Boolean
Methods included from Helper
#create_bunny_connection, #get_name_from_class, #now_stamp, #stringify_keys
Constructor Details
#initialize(header = nil, body = nil, raw = nil) ⇒ Message
Returns a new instance of Message.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/eventhub/message.rb', line 59 def initialize(header = nil, body = nil, raw = nil) @header = header || {} @body = body || {} @raw = raw # set message defaults, that we have required headers @header.set("message_id", SecureRandom.uuid, false) @header.set("version", VERSION, false) @header.set("created_at", now_stamp, false) @header.set("origin.module_id", "undefined", false) @header.set("origin.type", "undefined", false) @header.set("origin.site_id", "undefined", false) @header.set("process.name", "undefined", false) @header.set("process.execution_id", SecureRandom.uuid, false) @header.set("process.step_position", 0, false) @header.set("status.retried_count", 0, false) @header.set("status.code", STATUS_INITIAL, false) @header.set("status.message", "", false) end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
25 26 27 |
# File 'lib/eventhub/message.rb', line 25 def body @body end |
#header ⇒ Object
Returns the value of attribute header.
25 26 27 |
# File 'lib/eventhub/message.rb', line 25 def header @header end |
#raw ⇒ Object
Returns the value of attribute raw.
25 26 27 |
# File 'lib/eventhub/message.rb', line 25 def raw @raw end |
#routing_key ⇒ Object
Returns the value of attribute routing_key.
25 26 27 |
# File 'lib/eventhub/message.rb', line 25 def routing_key @routing_key end |
#vhost ⇒ Object
Returns the value of attribute vhost.
25 26 27 |
# File 'lib/eventhub/message.rb', line 25 def vhost @vhost end |
Class Method Details
.from_json(raw) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/eventhub/message.rb', line 40 def self.from_json(raw) data = JSON.parse(raw) Message.new(data.get("header"), data.get("body"), raw) rescue => e Message.new( { "status" => { "code" => STATUS_INVALID, "message" => "JSON parse error: #{e}" } }, { "original_message_base64_encoded" => Base64.encode64(raw) }, raw ) end |
.translate_status_code(code) ⇒ Object
153 154 155 |
# File 'lib/eventhub/message.rb', line 153 def self.translate_status_code(code) STATUS_CODE_TRANSLATION[code] end |
Instance Method Details
#append_to_execution_history(processor_name) ⇒ Object
146 147 148 149 150 151 |
# File 'lib/eventhub/message.rb', line 146 def append_to_execution_history(processor_name) header.set("execution_history", []) unless header.get("execution_history") header.get("execution_history") << {"processor" => processor_name, "timestamp" => now_stamp} end |
#copy(status_code = STATUS_SUCCESS) ⇒ Object
copies the message and set’s provided status code (default: success), actual stamp, and a new message id
134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/eventhub/message.rb', line 134 def copy(status_code = STATUS_SUCCESS) # use Marshal dump and load to make a deep object copy copied_header = Marshal.load(Marshal.dump(header)) copied_body = Marshal.load(Marshal.dump(body)) copied_header.set("message_id", SecureRandom.uuid) copied_header.set("created_at", now_stamp) copied_header.set("status.code", status_code) Message.new(copied_header, copied_body) end |
#initial? ⇒ Boolean
98 99 100 |
# File 'lib/eventhub/message.rb', line 98 def initial? status_code == STATUS_INITIAL end |
#invalid? ⇒ Boolean
106 107 108 |
# File 'lib/eventhub/message.rb', line 106 def invalid? status_code == STATUS_INVALID end |
#retry? ⇒ Boolean
94 95 96 |
# File 'lib/eventhub/message.rb', line 94 def retry? status_code == STATUS_RETRY end |
#retry_pending? ⇒ Boolean
102 103 104 |
# File 'lib/eventhub/message.rb', line 102 def retry_pending? status_code == STATUS_RETRY_PENDING end |
#schedule? ⇒ Boolean
110 111 112 |
# File 'lib/eventhub/message.rb', line 110 def schedule? status_code == STATUS_SCHEDULE end |
#schedule_pending? ⇒ Boolean
118 119 120 |
# File 'lib/eventhub/message.rb', line 118 def schedule_pending? status_code == STATUS_SCHEDULE_PENDING end |
#schedule_retry? ⇒ Boolean
114 115 116 |
# File 'lib/eventhub/message.rb', line 114 def schedule_retry? status_code == STATUS_SCHEDULE_RETRY end |
#success? ⇒ Boolean
90 91 92 |
# File 'lib/eventhub/message.rb', line 90 def success? status_code == STATUS_SUCCESS end |
#to_json ⇒ Object
122 123 124 |
# File 'lib/eventhub/message.rb', line 122 def to_json {"header" => header, "body" => body}.to_json end |
#to_s ⇒ Object
126 127 128 129 130 |
# File 'lib/eventhub/message.rb', line 126 def to_s "Msg: process " \ "[#{process_name}, #{process_step_position}, #{process_execution_id}]" \ ", status [#{status_code},#{},#{status_retried_count}]" end |
#valid? ⇒ Boolean
82 83 84 85 86 87 88 |
# File 'lib/eventhub/message.rb', line 82 def valid? # check for existence and defined value REQUIRED_HEADERS.all? do |key| @header.all_keys_with_path.include?(key) && !send(key.tr(".", "_").to_sym).nil? end end |