Module: SmartSMS::VerificationCode

Defined in:
lib/smart_sms/helpers/verification_code.rb

Overview

This module provides some methods to generate random verification code

Algorithm:

* short:    Generate short code with 4 numbers
* simple:   Generate simple code with 6 numbers
* middle:   Generate middle complex code of 6 charactors with mixed numbers and letters
* complex:  Generate complex code of 8 charactors with mixed numbers, letters or special charactors

Constant Summary collapse

REGISTERED_ALGORITHMS =
[:short, :simple, :middle, :complex]

Class Method Summary collapse

Class Method Details

.complexObject



38
39
40
# File 'lib/smart_sms/helpers/verification_code.rb', line 38

def complex
  SecureRandom.base64.slice(1..8).downcase
end

.middleObject



34
35
36
# File 'lib/smart_sms/helpers/verification_code.rb', line 34

def middle
  SecureRandom.base64.gsub!(/[^0-9a-zA-Z]/, '').slice(1..6).downcase
end

.random(algorithm = '') ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/smart_sms/helpers/verification_code.rb', line 17

def random(algorithm = '')
  algorithm = SmartSMS.config.verification_code_algorithm if algorithm.blank?
  if REGISTERED_ALGORITHMS.include? algorithm
    SmartSMS::VerificationCode.send algorithm
  else
    fail NoMethodError
  end
end

.shortObject



26
27
28
# File 'lib/smart_sms/helpers/verification_code.rb', line 26

def short
  SecureRandom.random_number.to_s.slice(-4..-1)
end

.simpleObject



30
31
32
# File 'lib/smart_sms/helpers/verification_code.rb', line 30

def simple
  SecureRandom.random_number.to_s.slice(-6..-1)
end