module TwilioPhoneVerification::Phonable

Public Instance Methods

change_phone_confirmed_at() click to toggle source
# File lib/twilio_phone_verification/phonable.rb, line 14
def change_phone_confirmed_at
  self.phone_confirmed_at = nil
end
confirm_phone() click to toggle source
# 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) click to toggle source
# File lib/twilio_phone_verification/phonable.rb, line 37
def confirm_phone_by_code(code)
  if ActiveSupport::SecurityUtils.secure_compare phone_confirmation_token, code.to_s
    return confirm_phone
  else
    errors.add(:code, " is wrong, try again.")
    return false
  end
end
generate_phone_confirmation_token() click to toggle source
# 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?() click to toggle source
# File lib/twilio_phone_verification/phonable.rb, line 10
def phone_confirmed?
  !!phone_confirmed_at
end
send_phone_confirmation() click to toggle source
# 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[:success]
  else
    errors.add(:code, "can be sent once per #{phone_confirmation_delay.to_s} seconds.")
    return false
  end
end

Private Instance Methods

get_phone_confirmation_token() click to toggle source
# File lib/twilio_phone_verification/phonable.rb, line 66
def get_phone_confirmation_token
  (0..9).to_a.sample(6)
end
phone_confirmation_delay() click to toggle source
# File lib/twilio_phone_verification/phonable.rb, line 62
def phone_confirmation_delay
  1.minute
end
phone_confirmation_message() click to toggle source
# File lib/twilio_phone_verification/phonable.rb, line 70
def phone_confirmation_message
  "Hello, #{name}. Your verification code: #{phone_confirmation_token}"
end