Class: Codecal::Calc
- Inherits:
-
Object
- Object
- Codecal::Calc
- Defined in:
- lib/codecal.rb
Constant Summary collapse
- @@MASK_ALPHABET =
['a','4','p','9','h','r','7','q','c','3','b','2','j','s','6','d','t','8','k','u','e','v','f','w','x','5','m','y','g','z','n']
- @@generate_seed =
[2,7,5,3,8,9,5,9,1,6,7,3,5]
- @@simple_seed =
[5,4,6,2,3,1,5,4,6,2,3,1]
Instance Method Summary collapse
- #bank_customer_code_generate(account_id, currency) ⇒ Object
- #code_generate_with_mask(mask, account_id) ⇒ Object
- #convert_masked_code_typo(masked_code) ⇒ Object
- #get_currency_name(currency_code) ⇒ Object
- #get_unmasked_code(mask, masked_code) ⇒ Object
-
#initialize(mask_alphabet = nil) ⇒ Calc
constructor
A new instance of Calc.
- #simple_code_generate(account_id) ⇒ Object
- #validate_bank_customer_code(customer_code) ⇒ Object
- #validate_masked_code(mask, masked_code) ⇒ Object
- #validate_simple_code(simple_code) ⇒ Object
Constructor Details
#initialize(mask_alphabet = nil) ⇒ Calc
Returns a new instance of Calc.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/codecal.rb', line 11 def initialize(mask_alphabet = nil) if mask_alphabet.is_a?(String) && mask_alphabet.size > 26 && all_letters_or_digits?(mask_alphabet) && mask_alphabet.size == mask_alphabet.split('').uniq.size @mask_alphabet = mask_alphabet.split('') else @mask_alphabet = @@MASK_ALPHABET end end |
Instance Method Details
#bank_customer_code_generate(account_id, currency) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/codecal.rb', line 22 def bank_customer_code_generate(account_id, currency) errormsg = "" if account_id == nil || currency == nil errormsg += "parameter is nil. " return {success:false, error: errormsg} end errormsg += "parameter 1 type should be Integer and length not longer than 9. " unless account_id.is_a?(Integer) && account_id.to_s.size <= 9 currency.is_a?(String) ? currency.upcase! : errormsg += "parameter 2 type should be String. " currency_code = Code.new[currency] errormsg += "currency not found. " unless currency_code if errormsg.size == 0 cal_array = ("%09d" % account_id + "%04d" % currency_code.to_i).split("").map! {|i| i.to_i} {success:true,customer_code: code_calculate(cal_array, @@generate_seed) } else {success:false, error: errormsg} end end |
#code_generate_with_mask(mask, account_id) ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/codecal.rb', line 55 def code_generate_with_mask(mask, account_id) errormsg = "mark should be string of letter or number and length should >= 5" unless is_legal_mask?(mask) return {success:false, error: errormsg} if errormsg result = simple_code_generate(account_id) return result unless result[:success] offset = get_mask_offset(mask) {success:true, customer_code: mask_code(offset, result[:customer_code])} end |
#convert_masked_code_typo(masked_code) ⇒ Object
88 89 90 |
# File 'lib/codecal.rb', line 88 def convert_masked_code_typo(masked_code) masked_code.gsub('1', 'l') end |
#get_currency_name(currency_code) ⇒ Object
102 103 104 105 |
# File 'lib/codecal.rb', line 102 def get_currency_name(currency_code) return nil if !currency_code || currency_code.size !=4 || !currency_code.is_a?(String) return Code.new.get_name(currency_code) end |
#get_unmasked_code(mask, masked_code) ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/codecal.rb', line 92 def get_unmasked_code(mask, masked_code) return false unless is_legal_masked_code?(masked_code) return false unless is_legal_mask?(mask) masked_code = convert_masked_code_typo(masked_code) offset = get_mask_offset(mask) code = unmask_code(offset, masked_code) masked_code.size == code.size ? code : false end |
#simple_code_generate(account_id) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/codecal.rb', line 40 def simple_code_generate(account_id) errormsg = "" if account_id == nil errormsg += "parameter is nil. " return {success:false, error: errormsg} end errormsg += "the type of the code to be encrypted should be Integer. " unless all_digits?(account_id.to_s) if errormsg.size == 0 cal_array = (account_id.to_i.to_s).split("").map! {|i| i.to_i} {success:true,customer_code: simple_code_calculate(cal_array, @@simple_seed) } else {success:false, error: errormsg} end end |
#validate_bank_customer_code(customer_code) ⇒ Object
64 65 66 67 68 |
# File 'lib/codecal.rb', line 64 def validate_bank_customer_code(customer_code) return false unless customer_code && customer_code.is_a?(String) && customer_code.size == 16 calcode = code_calculate(customer_code[0..12].split("").map! {|i| i.to_i}, @@generate_seed) return customer_code == calcode end |
#validate_masked_code(mask, masked_code) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/codecal.rb', line 76 def validate_masked_code(mask, masked_code) return false unless is_legal_masked_code?(masked_code) return false unless is_legal_mask?(mask) masked_code = convert_masked_code_typo(masked_code) offset = get_mask_offset(mask) result = simple_code_generate(unmask_code(offset, masked_code)[0..-2].to_i) return false unless result[:success] return masked_code == mask_code(offset, result[:customer_code]) end |
#validate_simple_code(simple_code) ⇒ Object
70 71 72 73 74 |
# File 'lib/codecal.rb', line 70 def validate_simple_code(simple_code) return false unless simple_code && simple_code.to_i > 0 && simple_code.size > 1 calcode = simple_code_calculate(simple_code[0..-2].split("").map! {|i| i.to_i}, @@simple_seed) return simple_code == calcode end |