Class: Stealth::Services::Twilio::MessageHandler

Inherits:
BaseMessageHandler
  • Object
show all
Defined in:
lib/stealth/services/twilio/message_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params:, headers:) ⇒ MessageHandler

Returns a new instance of MessageHandler.



10
11
12
13
# File 'lib/stealth/services/twilio/message_handler.rb', line 10

def initialize(params:, headers:)
  @params = params
  @headers = headers
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



8
9
10
# File 'lib/stealth/services/twilio/message_handler.rb', line 8

def headers
  @headers
end

#paramsObject (readonly)

Returns the value of attribute params.



8
9
10
# File 'lib/stealth/services/twilio/message_handler.rb', line 8

def params
  @params
end

#service_messageObject (readonly)

Returns the value of attribute service_message.



8
9
10
# File 'lib/stealth/services/twilio/message_handler.rb', line 8

def service_message
  @service_message
end

Instance Method Details

#coordinateObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/stealth/services/twilio/message_handler.rb', line 15

def coordinate
  Stealth::Services::HandleMessageJob.perform_async(
    'twilio',
    params,
    headers
  )

  # Relay our acceptance
  [204, 'No Content']
end

#processObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/stealth/services/twilio/message_handler.rb', line 26

def process
  @service_message = ServiceMessage.new(service: 'twilio')
  service_message.sender_id = params['From']
  service_message.target_id = params['To']
  service_message.message = params['Body']

  # Check for media attachments
  attachment_count = params['NumMedia'].to_i

  attachment_count.times do |i|
    service_message.attachments << {
      type: params["MediaContentType#{i}"],
      url: params["MediaUrl#{i}"]
    }
  end

  service_message
end