Class: BabySMS::Adapters::TwilioAdapter::WebHook

Inherits:
WebHook
  • Object
show all
Defined in:
lib/babysms/adapters/twilio_adapter.rb

Instance Attribute Summary

Attributes inherited from WebHook

#adapter

Instance Method Summary collapse

Methods inherited from WebHook

#end_point, #initialize, #mount_point

Constructor Details

This class inherits a constructor from BabySMS::WebHook

Instance Method Details

#process(app:, report:) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/babysms/adapters/twilio_adapter.rb', line 33

def process(app:, report:)
  validate!(app.request)

  # Of note: NumMedia, and MediaContentType0, MediaUrl0
  message = BabySMS::Message.new(from: app.params['From'],
                                 to: app.params['To'],
                                 contents: app.params['Body'],
                                 uuid: app.params['MessageSid'])
  report.incoming_message(message)
  [200, { 'Content-Type' => 'application/xml' }, '<Response></Response>']
end

#validate!(request) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/babysms/adapters/twilio_adapter.rb', line 25

def validate!(request)
  validator = Twilio::Security::RequestValidator.new(adapter.client.auth_token)
  unless validator.validate(end_point, request.params,
                            request.headers['X-Twilio-Signature'])
    fail BabySMS::Unauthorized, 'twilio signature failed'
  end
end