Module: TwilioPhoneVerification::Phonable

Extended by:
ActiveSupport::Concern
Defined in:
lib/twilio_phone_verification/phonable.rb

Instance Method Summary collapse

Instance Method Details

#change_phone_confirmed_atObject



14
15
16
# File 'lib/twilio_phone_verification/phonable.rb', line 14

def change_phone_confirmed_at
  self.phone_confirmed_at = nil
end

#confirm_phoneObject



46
47
48
49
50
51
52
53
# File 'lib/twilio_phone_verification/phonable.rb', line 46

def confirm_phone
  if phone_confirmed?
    errors.add(:phone, "has already been confirmed.")
    return false
  end
  self.phone_confirmed_at = Time.now
  save
end

#confirm_phone_by_code(code) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/twilio_phone_verification/phonable.rb', line 37

def confirm_phone_by_code(code)
  if phone_confirmation_token == code.to_s
    return confirm_phone
  else
    errors.add(:code, " is wrong, try again.")
    return false
  end
end

#generate_phone_confirmation_tokenObject



55
56
57
58
59
# File 'lib/twilio_phone_verification/phonable.rb', line 55

def generate_phone_confirmation_token
  self.phone_confirmation_token = get_phone_confirmation_token
  self.phone_confirmation_sent_at = Time.now
  save
end

#phone_confirmed?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/twilio_phone_verification/phonable.rb', line 10

def phone_confirmed?
  !!phone_confirmed_at
end

#send_phone_confirmationObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/twilio_phone_verification/phonable.rb', line 18

def send_phone_confirmation
  if phone_confirmed?
    errors.add(:phone, "has already been confirmed.")
    return false
  end
  if !phone_confirmation_sent_at || Time.now - phone_confirmation_sent_at > phone_confirmation_delay
    generate_phone_confirmation_token
    twilio_res = TwilioPhoneVerification::TwilioService.send_message(phone_confirmation_message, phone)
    unless twilio_res[:success]
      errors.add(:phone, "Error occured, while sending code. Please try again later.")
      return false
    end
    twilio_res
  else
    errors.add(:code, "can be sent once per #{phone_confirmation_delay.to_s} seconds.")
    return false
  end
end