Class: TwilioTwoFactorAuthClient

Inherits:
Object
  • Object
show all
Defined in:
lib/devise_twilio_two_factor/services/twilio_two_factor_client.rb

Constant Summary collapse

STATUS_PENDING =
"pending"
STATUS_APPROVED =
"approved"

Instance Method Summary collapse

Constructor Details

#initialize(resource) ⇒ TwilioTwoFactorAuthClient

Returns a new instance of TwilioTwoFactorAuthClient.



5
6
7
8
# File 'lib/devise_twilio_two_factor/services/twilio_two_factor_client.rb', line 5

def initialize(resource)
  @resource = resource
  @client = Twilio::REST::Client.new(resource.class., resource.class.twilio_auth_token)
end

Instance Method Details

#send_codeObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/devise_twilio_two_factor/services/twilio_two_factor_client.rb', line 10

def send_code
  begin
    response = @client.verify
      .v2
      .services(@resource.class.twilio_verify_service_sid)
      .verifications
      .create(to: @resource.send(@resource.class.otp_destination), channel: @resource.class.communication_type)

    return true if response.status == STATUS_PENDING
    return false
  rescue Twilio::REST::RestError => e
    puts e.message
    return false
  end
end

#verify_code(code) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/devise_twilio_two_factor/services/twilio_two_factor_client.rb', line 26

def verify_code(code)
  begin
    response = @client.verify
      .v2
      .services(@resource.class.twilio_verify_service_sid)
      .verification_checks
      .create(to: @resource.send(@resource.class.otp_destination), code: code)
    return true if response.status == STATUS_APPROVED
    return false
  rescue Twilio::REST::RestError => e
    puts e.message
    return false
  end
end