Class: Twi::Event

Inherits:
Resource show all
Defined in:
lib/twi/event.rb

Overview

An event tied to a (classic) conversation.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#initialize

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.

Returns:

  • (Hash) —

    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.

Returns:

  • (String, nil) —

    error code



30
# File 'lib/twi/event.rb', line 30

def code = @params['ErrorCode']

#content ⇒ String?

Returns content.

Returns:

  • (String, nil) —

    content



18
# File 'lib/twi/event.rb', line 18

def content = @params['Body']&.squish

#conversation_id ⇒ String

Returns unique conversation identifier.

Returns:

  • (String) —

    unique conversation identifier



27
# File 'lib/twi/event.rb', line 27

def conversation_id = @params['ConversationSid']

#id ⇒ String

Returns unique message identifier.

Returns:

  • (String) —

    unique message identifier



15
# File 'lib/twi/event.rb', line 15

def id = @params['MessageSid']

#image_urls ⇒ Array<String>

Returns URLs of image attachments.

Returns:

  • (Array<String>) —

    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).

Returns:

  • (Participant) —

    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.

Returns:

  • (String) —

    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.

Returns:

  • (Symbol) —

    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