File: twilio_messaging.rb

package info (click to toggle)
ruby-noticed 3.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 324 kB
  • sloc: ruby: 1,186; makefile: 4
file content (45 lines) | stat: -rw-r--r-- 1,345 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
module Noticed
  module DeliveryMethods
    class TwilioMessaging < DeliveryMethod
      def deliver
        post_request url, basic_auth: {user: account_sid, pass: auth_token}, form: json.stringify_keys
      rescue Noticed::ResponseUnsuccessful => exception
        if exception.response.code.start_with?("4") && config[:error_handler]
          notification.instance_exec(exception.response, &config[:error_handler])
        else
          raise
        end
      end

      def json
        evaluate_option(:json) || {
          From: phone_number,
          To: recipient.phone_number,
          Body: params.fetch(:message)
        }
      end

      def url
        evaluate_option(:url) || "https://api.twilio.com/2010-04-01/Accounts/#{account_sid}/Messages.json"
      end

      def account_sid
        evaluate_option(:account_sid) || credentials.fetch(:account_sid)
      end

      def auth_token
        evaluate_option(:auth_token) || credentials.fetch(:auth_token)
      end

      def phone_number
        evaluate_option(:phone_number) || credentials.fetch(:phone_number)
      end

      def credentials
        evaluate_option(:credentials) || Rails.application.credentials.twilio
      end
    end
  end
end

ActiveSupport.run_load_hooks :noticed_delivery_methods_twilio_messaging, Noticed::DeliveryMethods::TwilioMessaging