Class: CreditCardValidations::Detector

Inherits:
Object
  • Object
show all
Includes:
Mmi
Defined in:
lib/credit_card_validations/detector.rb

Constant Summary

Constants included from Mmi

Mmi::ISSUER_CATEGORIES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mmi

#issuer_category

Constructor Details

#initialize(number) ⇒ Detector

Returns a new instance of Detector.



14
15
16
# File 'lib/credit_card_validations/detector.rb', line 14

def initialize(number)
  @number = number.to_s.tr('- ', '')
end

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number.



12
13
14
# File 'lib/credit_card_validations/detector.rb', line 12

def number
  @number
end

Class Method Details

.add_brand(key, rules, options = {}) ⇒ Object

add brand

CreditCardValidations.add_brand(:en_route, {length: 15, prefixes: ['2014', '2149']}, {skip_luhn: true}) #skip luhn


85
86
87
88
89
90
91
92
93
94
95
# File 'lib/credit_card_validations/detector.rb', line 85

def add_brand(key, rules, options = {})

  brands[key] = {rules: [], options: options || {}}

  Array.wrap(rules).each do |rule|
    add_rule(key, rule[:length], rule[:prefixes])
  end

  define_brand_method(key)

end

.add_rule(key, length, prefixes) ⇒ Object

create rule for detecting brand



120
121
122
123
124
125
126
# File 'lib/credit_card_validations/detector.rb', line 120

def add_rule(key, length, prefixes)
  unless brands.has_key?(key)
    raise Error.new("brand #{key} is undefined, please use #add_brand method")
  end
  length, prefixes = Array(length), Array(prefixes)
  brands[key][:rules] << {length: length, regexp: compile_regexp(prefixes), prefixes: prefixes}
end

.brand_key(brand_name) ⇒ Object



106
107
108
109
110
# File 'lib/credit_card_validations/detector.rb', line 106

def brand_key(brand_name)
  brands.detect do |_, brand|
    brand[:options][:brand_name] == brand_name
  end.try(:first)
end

.brand_name(brand_key) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/credit_card_validations/detector.rb', line 97

def brand_name(brand_key)
  brand = brands[brand_key]
  if brand
    brand.fetch(:options, {})[:brand_name] || brand_key.to_s.titleize
  else
    nil
  end
end

.delete_brand(key) ⇒ Object

CreditCardValidations.delete_brand(:en_route)



113
114
115
116
117
# File 'lib/credit_card_validations/detector.rb', line 113

def delete_brand(key)
  key = key.to_sym
  undef_brand_method(key)
  brands.reject! { |k, _| k == key }
end

.has_luhn_check_rule?(key) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/credit_card_validations/detector.rb', line 76

def has_luhn_check_rule?(key)
  !brands[key].fetch(:options, {}).fetch(:skip_luhn, false)
end

Instance Method Details

#brand(*keys) ⇒ Object

brand name



24
25
26
# File 'lib/credit_card_validations/detector.rb', line 24

def brand(*keys)
  valid_number?(*keys)
end

#brand_nameObject



43
44
45
# File 'lib/credit_card_validations/detector.rb', line 43

def brand_name
  self.class.brand_name(brand)
end

#valid?(*brands) ⇒ Boolean

credit card number validation

Returns:

  • (Boolean)


19
20
21
# File 'lib/credit_card_validations/detector.rb', line 19

def valid?(*brands)
  !!valid_number?(*brands)
end

#valid_luhn?Boolean

check if luhn valid

Returns:

  • (Boolean)


39
40
41
# File 'lib/credit_card_validations/detector.rb', line 39

def valid_luhn?
  @valid_luhn ||= Luhn.valid?(number)
end

#valid_number?(*keys) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
# File 'lib/credit_card_validations/detector.rb', line 28

def valid_number?(*keys)
  selected_brands = keys.blank? ? self.brands : resolve_keys(*keys)
  if selected_brands.any?
    selected_brands.each do |key, brand|
      return key if matches_brand?(brand)
    end
  end
  nil
end