Class: SignalWire::Relay::Message
- Inherits:
-
Object
- Object
- SignalWire::Relay::Message
- Defined in:
- lib/signalwire/relay/message.rb
Overview
Represents a single SMS/MMS message.
For outbound messages, use message.wait to block until a terminal state (delivered, undelivered, failed) is reached.
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#direction ⇒ Object
readonly
Returns the value of attribute direction.
-
#from_number ⇒ Object
readonly
Returns the value of attribute from_number.
-
#media ⇒ Object
readonly
Returns the value of attribute media.
-
#message_id ⇒ Object
readonly
Returns the value of attribute message_id.
-
#reason ⇒ Object
readonly
Returns the value of attribute reason.
-
#segments ⇒ Object
readonly
Returns the value of attribute segments.
-
#state ⇒ Object
Returns the value of attribute state.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
-
#to_number ⇒ Object
readonly
Returns the value of attribute to_number.
Instance Method Summary collapse
-
#_dispatch_event(payload) ⇒ Object
Handle a messaging.state event for this message.
-
#_set_on_completed(callback) ⇒ Object
Set the on_completed callback from options.
- #done? ⇒ Boolean (also: #is_done?)
-
#initialize(message_id: '', context: '', direction: '', from_number: '', to_number: '', body: '', media: nil, segments: 0, state: '', reason: '', tags: nil) ⇒ Message
constructor
A new instance of Message.
- #inspect ⇒ Object
-
#on_completed(&block) ⇒ Object
Set the on_completed callback.
-
#on_event(&handler) ⇒ Object
Register an event listener for state changes.
- #result ⇒ Object
- #to_s ⇒ Object
-
#wait(timeout: nil) ⇒ Object
Wait for the message to reach a terminal state.
Constructor Details
#initialize(message_id: '', context: '', direction: '', from_number: '', to_number: '', body: '', media: nil, segments: 0, state: '', reason: '', tags: nil) ⇒ Message
Returns a new instance of Message.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/signalwire/relay/message.rb', line 14 def initialize(message_id: '', context: '', direction: '', from_number: '', to_number: '', body: '', media: nil, segments: 0, state: '', reason: '', tags: nil) @message_id = @context = context @direction = direction @from_number = from_number @to_number = to_number @body = body @media = media || [] @segments = segments @state = state @reason = reason @tags = || [] # Completion tracking @mutex = Mutex.new @condition = ConditionVariable.new @done = false @result = nil @on_completed = nil @listeners = [] end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
10 11 12 |
# File 'lib/signalwire/relay/message.rb', line 10 def body @body end |
#context ⇒ Object (readonly)
Returns the value of attribute context.
10 11 12 |
# File 'lib/signalwire/relay/message.rb', line 10 def context @context end |
#direction ⇒ Object (readonly)
Returns the value of attribute direction.
10 11 12 |
# File 'lib/signalwire/relay/message.rb', line 10 def direction @direction end |
#from_number ⇒ Object (readonly)
Returns the value of attribute from_number.
10 11 12 |
# File 'lib/signalwire/relay/message.rb', line 10 def from_number @from_number end |
#media ⇒ Object (readonly)
Returns the value of attribute media.
10 11 12 |
# File 'lib/signalwire/relay/message.rb', line 10 def media @media end |
#message_id ⇒ Object (readonly)
Returns the value of attribute message_id.
10 11 12 |
# File 'lib/signalwire/relay/message.rb', line 10 def @message_id end |
#reason ⇒ Object (readonly)
Returns the value of attribute reason.
10 11 12 |
# File 'lib/signalwire/relay/message.rb', line 10 def reason @reason end |
#segments ⇒ Object (readonly)
Returns the value of attribute segments.
10 11 12 |
# File 'lib/signalwire/relay/message.rb', line 10 def segments @segments end |
#state ⇒ Object
Returns the value of attribute state.
12 13 14 |
# File 'lib/signalwire/relay/message.rb', line 12 def state @state end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
10 11 12 |
# File 'lib/signalwire/relay/message.rb', line 10 def @tags end |
#to_number ⇒ Object (readonly)
Returns the value of attribute to_number.
10 11 12 |
# File 'lib/signalwire/relay/message.rb', line 10 def to_number @to_number end |
Instance Method Details
#_dispatch_event(payload) ⇒ Object
Handle a messaging.state event for this message.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/signalwire/relay/message.rb', line 86 def _dispatch_event(payload) event_params = payload['params'] || {} new_state = event_params['message_state'] || '' @state = new_state unless new_state.empty? @reason = event_params['reason'] if event_params.key?('reason') event = Relay.parse_event(payload) # Notify listeners @listeners.each do |handler| begin handler.call(event) rescue => e $stderr.puts "[RELAY] Error in message event handler for #{@message_id}: #{e.}" end end # Check terminal state _resolve(event) if MESSAGE_TERMINAL_STATES.include?(new_state) end |
#_set_on_completed(callback) ⇒ Object
Set the on_completed callback from options.
44 45 46 |
# File 'lib/signalwire/relay/message.rb', line 44 def _set_on_completed(callback) @on_completed = callback end |
#done? ⇒ Boolean Also known as: is_done?
53 54 55 |
# File 'lib/signalwire/relay/message.rb', line 53 def done? @done end |
#inspect ⇒ Object
113 114 115 |
# File 'lib/signalwire/relay/message.rb', line 113 def inspect to_s end |
#on_completed(&block) ⇒ Object
Set the on_completed callback.
39 40 41 |
# File 'lib/signalwire/relay/message.rb', line 39 def on_completed(&block) @on_completed = block end |
#on_event(&handler) ⇒ Object
Register an event listener for state changes.
49 50 51 |
# File 'lib/signalwire/relay/message.rb', line 49 def on_event(&handler) @listeners << handler end |
#result ⇒ Object
59 60 61 |
# File 'lib/signalwire/relay/message.rb', line 59 def result @result end |
#to_s ⇒ Object
108 109 110 111 |
# File 'lib/signalwire/relay/message.rb', line 108 def to_s "Message(id=#{@message_id}, direction=#{@direction}, " \ "state=#{@state}, from=#{@from_number}, to=#{@to_number})" end |
#wait(timeout: nil) ⇒ Object
Wait for the message to reach a terminal state. Raises ActionTimeoutError if timeout exceeded.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/signalwire/relay/message.rb', line 65 def wait(timeout: nil) @mutex.synchronize do return @result if @done if timeout deadline = Time.now + timeout while !@done remaining = deadline - Time.now if remaining <= 0 raise ActionTimeoutError, "Message #{@message_id} timed out after #{timeout}s" end @condition.wait(@mutex, remaining) end else @condition.wait(@mutex) until @done end @result end end |