Class: Twi::Event
Overview
An event tied to a (classic) conversation.
Direct Known Subclasses
ConversationEvent, DeliveryEvent, MessageEvent, Mock::Event, ParticipantEvent
Class Method Summary collapse
-
.params_for(id:, type:, status: nil, participant: nil) ⇒ Hash
The shape of the payload send by Twilio to the callback URL.
- .participant_params_for(participant = nil) ⇒ Object
Instance Method Summary collapse
-
#code ⇒ String?
Error code.
-
#content ⇒ String?
Content.
-
#conversation_id ⇒ String
Unique conversation identifier.
-
#id ⇒ String
Unique message identifier.
-
#image_urls ⇒ Array<String>
URLs of image attachments.
-
#participant ⇒ Participant
The participant the event is about (e.g.: joined the conversation).
-
#status ⇒ String
Conversation state, one of active, inactive, closed, initializing.
-
#target ⇒ Symbol
Event target type, can be :conversation, :participant, :message, :delivery.
Methods inherited from Resource
Constructor Details
This class inherits a constructor from Twi::Resource
Class Method Details
.params_for(id:, type:, status: nil, participant: nil) ⇒ Hash
Returns the shape of the payload send by Twilio to the callback URL.
33 34 35 36 37 38 |
# File 'lib/twi/event.rb', line 33 def self.params_for(id:, type:, status: nil, participant: nil) { ConversationSid: id, EventType: "on_#{type}_changed".camelize(:lower), State: status&.to_s, }.merge(participant_params_for participant).compact end |
.participant_params_for(participant = nil) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/twi/event.rb', line 40 def self.participant_params_for(participant = nil) if participant if participant[:identity] { ParticipantSid: participant[:id], Identity: participant[:identity] } else { ParticipantSid: participant[:id], 'MessagingBinding.Address' => "+1#{participant[:phone]}" } end else {} end end |
Instance Method Details
#code ⇒ String?
Returns error code.
30 |
# File 'lib/twi/event.rb', line 30 def code = @params['ErrorCode'] |
#content ⇒ String?
Returns content.
18 |
# File 'lib/twi/event.rb', line 18 def content = @params['Body']&.squish |
#conversation_id ⇒ String
Returns unique conversation identifier.
27 |
# File 'lib/twi/event.rb', line 27 def conversation_id = @params['ConversationSid'] |
#id ⇒ String
Returns unique message identifier.
15 |
# File 'lib/twi/event.rb', line 15 def id = @params['MessageSid'] |
#image_urls ⇒ Array<String>
Returns URLs of image attachments.
21 22 23 24 |
# File 'lib/twi/event.rb', line 21 def image_urls media = JSON(@params.fetch('Media', '[]')).map { |params| medium_for params } media.filter(&:image?).map { |image| image.url } end |
#participant ⇒ Participant
Returns the participant the event is about (e.g.: joined the conversation).
12 |
# File 'lib/twi/event.rb', line 12 def participant = Participant.new @params |
#status ⇒ String
Returns conversation state, one of active, inactive, closed, initializing.
9 |
# File 'lib/twi/event.rb', line 9 def status = @params['Status'] || @params['StateTo'] || @params['State'] |
#target ⇒ Symbol
Returns event target type, can be :conversation, :participant, :message, :delivery.
6 |
# File 'lib/twi/event.rb', line 6 def target = @params['EventType'].underscore.split('_').second.to_sym |