Class: Freeswitch::ESL::Protocol::Message
- Inherits:
-
Object
- Object
- Freeswitch::ESL::Protocol::Message
- Defined in:
- lib/freeswitch/esl/protocol/message.rb
Overview
Represents a raw ESL protocol message (headers + optional body).
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #content_type ⇒ Object
-
#error_message ⇒ Object
Extract the FreeSWITCH error text without the leading
-ERRmarker. -
#initialize(headers, body = nil) ⇒ Message
constructor
A new instance of Message.
- #reply_text ⇒ Object
-
#successful? ⇒ Boolean
Return true when the message payload represents a successful ESL reply.
Constructor Details
#initialize(headers, body = nil) ⇒ Message
Returns a new instance of Message.
10 11 12 13 |
# File 'lib/freeswitch/esl/protocol/message.rb', line 10 def initialize(headers, body = nil) @headers = headers.freeze @body = body&.freeze end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
8 9 10 |
# File 'lib/freeswitch/esl/protocol/message.rb', line 8 def body @body end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
8 9 10 |
# File 'lib/freeswitch/esl/protocol/message.rb', line 8 def headers @headers end |
Instance Method Details
#[](key) ⇒ Object
15 16 17 |
# File 'lib/freeswitch/esl/protocol/message.rb', line 15 def [](key) headers[key] end |
#content_type ⇒ Object
19 20 21 |
# File 'lib/freeswitch/esl/protocol/message.rb', line 19 def content_type headers["Content-Type"] end |
#error_message ⇒ Object
Extract the FreeSWITCH error text without the leading -ERR marker. Returns nil for successful replies or when no error payload is present.
40 41 42 43 |
# File 'lib/freeswitch/esl/protocol/message.rb', line 40 def text = content_type == "api/response" ? body : reply_text text&.start_with?("-ERR") ? text.sub(/\A-ERR\s*/, "").strip : nil end |
#reply_text ⇒ Object
23 24 25 |
# File 'lib/freeswitch/esl/protocol/message.rb', line 23 def reply_text headers["Reply-Text"] end |
#successful? ⇒ Boolean
Return true when the message payload represents a successful ESL reply. For api/response messages the success marker lives in the body; for command replies it lives in the Reply-Text header.
30 31 32 33 34 35 36 |
# File 'lib/freeswitch/esl/protocol/message.rb', line 30 def successful? if content_type == "api/response" body&.start_with?("+OK") || false else reply_text&.start_with?("+OK") || false end end |