Class: Freeswitch::ESL::Protocol::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/freeswitch/esl/protocol/message.rb

Overview

Represents a raw ESL protocol message (headers + optional body).

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bodyObject (readonly)

Returns the value of attribute body.



8
9
10
# File 'lib/freeswitch/esl/protocol/message.rb', line 8

def body
  @body
end

#headersObject (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_typeObject



19
20
21
# File 'lib/freeswitch/esl/protocol/message.rb', line 19

def content_type
  headers["Content-Type"]
end

#error_messageObject



35
36
37
38
# File 'lib/freeswitch/esl/protocol/message.rb', line 35

def error_message
  text = content_type == "api/response" ? body : reply_text
  text&.start_with?("-ERR") ? text.sub(/\A-ERR\s*/, "").strip : nil
end

#reply_textObject



23
24
25
# File 'lib/freeswitch/esl/protocol/message.rb', line 23

def reply_text
  headers["Reply-Text"]
end

#successful?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
# File 'lib/freeswitch/esl/protocol/message.rb', line 27

def successful?
  if content_type == "api/response"
    body&.start_with?("+OK")
  else
    reply_text&.start_with?("+OK")
  end
end