Class: Freeswitch::ESL::Protocol::Event

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

Overview

Represents a FreeSWITCH event received in JSON format.

Constant Summary collapse

ERROR_BODY_PREFIX =
/\A\s*-(ERR|USAGE)\b/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_json) ⇒ Event

Returns a new instance of Event.



14
15
16
# File 'lib/freeswitch/esl/protocol/event.rb', line 14

def initialize(raw_json)
  @data = JSON.parse(raw_json).freeze
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



12
13
14
# File 'lib/freeswitch/esl/protocol/event.rb', line 12

def data
  @data
end

Instance Method Details

#[](key) ⇒ Object?

Returns the raw value stored in the event payload for the given key.

Returns:

  • (Object, nil)

    Parsed JSON value for the requested field.



20
21
22
# File 'lib/freeswitch/esl/protocol/event.rb', line 20

def [](key)
  data[key]
end

#api_commandString?

Returns the command executed by a synchronous API event.

Returns:

  • (String, nil)

    API command name reported by an API event.



128
129
130
# File 'lib/freeswitch/esl/protocol/event.rb', line 128

def api_command
  data["API-Command"]
end

#bg_job_event?Boolean

Returns whether the event represents a bgapi job completion.

Returns:

  • (Boolean)

    True when the event name is BACKGROUND_JOB.



146
147
148
# File 'lib/freeswitch/esl/protocol/event.rb', line 146

def bg_job_event?
  name == "BACKGROUND_JOB"
end

#bodyString?

Returns the optional event body payload.

Returns:

  • (String, nil)

    Body content, commonly used for api/bgapi command output.



140
141
142
# File 'lib/freeswitch/esl/protocol/event.rb', line 140

def body
  data["_body"]
end

#calling_fileString?

Returns the FreeSWITCH source file that emitted the event.

Returns:

  • (String, nil)

    Internal source filename from the FreeSWITCH runtime.



86
87
88
# File 'lib/freeswitch/esl/protocol/event.rb', line 86

def calling_file
  data["Event-Calling-File"]
end

#calling_functionString?

Returns the FreeSWITCH source function that emitted the event.

Returns:

  • (String, nil)

    Internal function name from the FreeSWITCH runtime.



92
93
94
# File 'lib/freeswitch/esl/protocol/event.rb', line 92

def calling_function
  data["Event-Calling-Function"]
end

#calling_line_numberString?

Returns the FreeSWITCH source line number that emitted the event.

Returns:

  • (String, nil)

    Internal line number from the FreeSWITCH runtime.



98
99
100
# File 'lib/freeswitch/esl/protocol/event.rb', line 98

def calling_line_number
  data["Event-Calling-Line-Number"]
end

#channel_variable(name) ⇒ String?

Returns the value of a channel variable exported in the event payload.

Returns:

  • (String, nil)

    Value stored under the corresponding variable_* event field.



159
160
161
# File 'lib/freeswitch/esl/protocol/event.rb', line 159

def channel_variable(name)
  data["variable_#{name}"]
end

#core_uuidString?

Returns the UUID of the emitting FreeSWITCH core instance.

Returns:

  • (String, nil)

    Core identifier shared across events generated by the same node.



32
33
34
# File 'lib/freeswitch/esl/protocol/event.rb', line 32

def core_uuid
  data["Core-UUID"]
end

#date_gmtString?

Returns the event timestamp formatted in GMT.

Returns:

  • (String, nil)

    Human-readable GMT date and time string.



74
75
76
# File 'lib/freeswitch/esl/protocol/event.rb', line 74

def date_gmt
  data["Event-Date-GMT"]
end

#date_localString?

Returns the event timestamp formatted in the server local timezone.

Returns:

  • (String, nil)

    Human-readable local date and time string.



68
69
70
# File 'lib/freeswitch/esl/protocol/event.rb', line 68

def date_local
  data["Event-Date-Local"]
end

#error?Boolean

Returns whether the event body represents an ESL command error.

Returns:

  • (Boolean)

    True for bodies starting with -ERR or -USAGE, false for non-error bodies, false when the body is missing.



153
154
155
# File 'lib/freeswitch/esl/protocol/event.rb', line 153

def error?
  body&.match?(ERROR_BODY_PREFIX) || false
end

#hostnameString?

Returns the hostname reported by the FreeSWITCH node.

Returns:

  • (String, nil)

    Hostname of the server that emitted the event.



38
39
40
# File 'lib/freeswitch/esl/protocol/event.rb', line 38

def hostname
  data["FreeSWITCH-Hostname"]
end

#infoString?

Returns the human-readable informational text attached to the event.

Returns:

  • (String, nil)

    Summary text such as the HEARTBEAT status message.



134
135
136
# File 'lib/freeswitch/esl/protocol/event.rb', line 134

def info
  data["Event-Info"]
end

#ipv4String?

Returns the IPv4 address advertised by FreeSWITCH.

Returns:

  • (String, nil)

    IPv4 address of the emitting node.



50
51
52
# File 'lib/freeswitch/esl/protocol/event.rb', line 50

def ipv4
  data["FreeSWITCH-IPv4"]
end

#ipv6String?

Returns the IPv6 address advertised by FreeSWITCH.

Returns:

  • (String, nil)

    IPv6 address of the emitting node.



56
57
58
# File 'lib/freeswitch/esl/protocol/event.rb', line 56

def ipv6
  data["FreeSWITCH-IPv6"]
end

#job_commandString?

Returns the command executed by the background job.

Returns:

  • (String, nil)

    Original command name reported by a BACKGROUND_JOB event.



122
123
124
# File 'lib/freeswitch/esl/protocol/event.rb', line 122

def job_command
  data["Job-Command"]
end

#job_uuidString?

Returns the UUID assigned to a background job.

Returns:

  • (String, nil)

    Job identifier for BACKGROUND_JOB events.



116
117
118
# File 'lib/freeswitch/esl/protocol/event.rb', line 116

def job_uuid
  data["Job-UUID"]
end

#nameString?

Returns the canonical FreeSWITCH event name.

Returns:

  • (String, nil)

    Event name such as CHANNEL_CREATE, HEARTBEAT or BACKGROUND_JOB.



26
27
28
# File 'lib/freeswitch/esl/protocol/event.rb', line 26

def name
  data["Event-Name"]
end

#sequenceString?

Returns the monotonic event sequence assigned by FreeSWITCH.

Returns:

  • (String, nil)

    Sequence number as exposed by the event payload.



104
105
106
# File 'lib/freeswitch/esl/protocol/event.rb', line 104

def sequence
  data["Event-Sequence"]
end

#subclassString?

Returns the event subclass when the event provides one.

Returns:

  • (String, nil)

    Module-specific subclass, often used by CUSTOM events.



62
63
64
# File 'lib/freeswitch/esl/protocol/event.rb', line 62

def subclass
  data["Event-Subclass"]
end

#switchnameString?

Returns the logical FreeSWITCH switch name.

Returns:

  • (String, nil)

    Switch name configured on the emitting node.



44
45
46
# File 'lib/freeswitch/esl/protocol/event.rb', line 44

def switchname
  data["FreeSWITCH-Switchname"]
end

#timestampString?

Returns the raw FreeSWITCH event timestamp.

Returns:

  • (String, nil)

    Epoch timestamp expressed in microseconds.



80
81
82
# File 'lib/freeswitch/esl/protocol/event.rb', line 80

def timestamp
  data["Event-Date-Timestamp"]
end

#uuidString?

Returns the channel UUID associated with the event.

Returns:

  • (String, nil)

    Session identifier when the event refers to a channel.



110
111
112
# File 'lib/freeswitch/esl/protocol/event.rb', line 110

def uuid
  data["Unique-ID"]
end