Module: Codecal

Defined in:
lib/codecal.rb,
lib/codecal/version.rb

Constant Summary collapse

VERSION =
"0.3.8"
@@BASE_ALPHABET =
['o','a','3','p','9','h','b','2','q','i','1','c','4','r','7','j','s','5','d','t','8','k','u','e','v','l','w','f','x','6','m','y','0','g','z','n']
@@MASK_ALPHABET =
['a','4','p','9','h','r','7','q','c','3','b','2','j','s','6','d','t','8','k','u','e','l','v','f','w','x','5','m','y','g','z','n', 'i']
@@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]

Class Method Summary collapse

Class Method Details

.bank_customer_code_generate(account_id, currency) ⇒ Object



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

def bank_customer_code_generate(, currency)
  errormsg = ""
  if  == 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 .is_a?(Integer) && .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" %  + "%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



43
44
45
46
47
48
49
50
# File 'lib/codecal.rb', line 43

def code_generate_with_mask(mask, )
  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()
  return result unless result[:success]
  offset = get_mask_offset(mask)
  {success:true, customer_code: mask_code(offset, result[:customer_code])}
end

.get_currency_name(currency_code) ⇒ Object



81
82
83
84
# File 'lib/codecal.rb', line 81

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



73
74
75
76
77
78
79
# File 'lib/codecal.rb', line 73

def get_unmasked_code(mask, masked_code)
  return false unless is_legal_masked_code?(masked_code)
  return false unless is_legal_mask?(mask) 
  offset = get_mask_offset(mask)
  code = unmask_code(offset, masked_code)
  all_digits?(code) ? code : false
end

.simple_code_generate(account_id) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/codecal.rb', line 28

def simple_code_generate()
  errormsg = ""
  if  == 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?(.to_s)
  if errormsg.size == 0
    cal_array = (.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



52
53
54
55
56
# File 'lib/codecal.rb', line 52

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



64
65
66
67
68
69
70
71
# File 'lib/codecal.rb', line 64

def validate_masked_code(mask, masked_code)
  return false unless is_legal_masked_code?(masked_code)
  return false unless is_legal_mask?(mask) 
  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



58
59
60
61
62
# File 'lib/codecal.rb', line 58

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