Class: Xip::Services::Twilio::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reply:) ⇒ Client

Returns a new instance of Client.



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

def initialize(reply:)
  @reply = reply
   = Xip.config.twilio.
  api_key = Xip.config.twilio.api_key
  auth_token = Xip.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.



14
15
16
# File 'lib/xip/services/twilio/client.rb', line 14

def reply
  @reply
end

#twilio_clientObject (readonly)

Returns the value of attribute twilio_client.



14
15
16
# File 'lib/xip/services/twilio/client.rb', line 14

def twilio_client
  @twilio_client
end

Instance Method Details

#transmitObject



31
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
# File 'lib/xip/services/twilio/client.rb', line 31

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 Xip::Errors::UserOptOut
    when /21612/ # 'To' phone number is not currently reachable via SMS
      raise Xip::Errors::UserOptOut
    when /21614/ # 'To' number is not a valid mobile number
      raise Xip::Errors::UserOptOut
    when /30004/ # Message blocked
      raise Xip::Errors::UserOptOut
    when /21211/ # Invalid 'To' Phone Number
      raise Xip::Errors::InvalidSessionID
    when /30003/ # Unreachable destination handset
      raise Xip::Errors::InvalidSessionID
    when /30005/ # Unknown destination handset
      raise Xip::Errors::InvalidSessionID
    else
      raise
    end
  end

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