Class: Stealth::Services::Twilio::Client

Inherits:
BaseClient
  • Object
show all
Defined in:
lib/stealth/services/twilio/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reply:) ⇒ Client

Returns a new instance of Client.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/stealth/services/twilio/client.rb', line 17

def initialize(reply:)
  @reply = reply
   = Stealth.config.twilio.
  api_key = Stealth.config.twilio.api_key
  auth_token = Stealth.config.twilio.auth_token

  if api_key.present?
    @twilio_client = ::Twilio::REST::Client.new(
      api_key, auth_token, 
    )
  else
    @twilio_client = ::Twilio::REST::Client.new(, auth_token)
  end
end

Instance Attribute Details

#replyObject (readonly)

Returns the value of attribute reply.



15
16
17
# File 'lib/stealth/services/twilio/client.rb', line 15

def reply
  @reply
end

#twilio_clientObject (readonly)

Returns the value of attribute twilio_client.



15
16
17
# File 'lib/stealth/services/twilio/client.rb', line 15

def twilio_client
  @twilio_client
end

Instance Method Details

#transmitObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/stealth/services/twilio/client.rb', line 32

def transmit
  # Don't transmit anything for delays
  return true if reply.blank?

  begin
    response = twilio_client.messages.create(**reply)
  rescue ::Twilio::REST::RestError => e
    case e.message
    when /21610/ # Attempt to send to unsubscribed recipient
      raise Stealth::Errors::UserOptOut
    when /21211/ # Invalid 'To' Phone Number
      raise Stealth::Errors::InvalidSessionID
    when /21612/ # 'To' phone number is not currently reachable via SMS
      raise Stealth::Errors::InvalidSessionID
    when /21614/ # 'To' number is not a valid mobile number
      raise Stealth::Errors::InvalidSessionID
    when /30003/ # Unreachable destination handset
      raise Stealth::Errors::InvalidSessionID
    when /30004/ # Message Blocked
      raise Stealth::Errors::MessageFiltered
    when /30005/ # Unknown destination handset
      raise Stealth::Errors::InvalidSessionID
    when /30006/ # Landline or unreachable carrier
      raise Stealth::Errors::InvalidSessionID
    when /30007/ # Message filtered
      raise Stealth::Errors::MessageFiltered
    when /30008/ # Unknown error 🤷‍♂️
      raise Stealth::Errors::UnknownServiceError
    else
      raise
    end
  end

  Stealth::Logger.l(
    topic: "twilio",
    message: "Transmitting. Response: #{response.status}: #{response.error_message}"
  )
end