Class: CreditCardValidations::Detector
- Inherits:
-
Object
- Object
- CreditCardValidations::Detector
- Includes:
- Mmi
- Defined in:
- lib/credit_card_validations/detector.rb
Constant Summary
Constants included from Mmi
Instance Attribute Summary collapse
-
#number ⇒ Object
readonly
Returns the value of attribute number.
Class Method Summary collapse
-
.add_brand(key, rules, options = {}) ⇒ Object
add brand.
-
.add_rule(key, length, prefixes) ⇒ Object
create rule for detecting brand.
- .brand_key(brand_name) ⇒ Object
- .brand_name(brand_key) ⇒ Object
-
.delete_brand(key) ⇒ Object
CreditCardValidations.delete_brand(:en_route).
- .has_luhn_check_rule?(key) ⇒ Boolean
Instance Method Summary collapse
-
#brand(*keys) ⇒ Object
brand name.
- #brand_name ⇒ Object
-
#initialize(number) ⇒ Detector
constructor
A new instance of Detector.
-
#valid?(*brands) ⇒ Boolean
credit card number validation.
-
#valid_luhn? ⇒ Boolean
check if luhn valid.
- #valid_number?(*keys) ⇒ Boolean
Methods included from Mmi
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
#number ⇒ Object (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
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/credit_card_validations/detector.rb', line 91 def add_brand(key, rules, = {}) brands[key] = {rules: [], 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
126 127 128 129 130 131 132 |
# File 'lib/credit_card_validations/detector.rb', line 126 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
112 113 114 115 116 |
# File 'lib/credit_card_validations/detector.rb', line 112 def brand_key(brand_name) brands.detect do |_, brand| brand[:options][:brand_name] == brand_name end.try(:first) end |
.brand_name(brand_key) ⇒ Object
103 104 105 106 107 108 109 110 |
# File 'lib/credit_card_validations/detector.rb', line 103 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)
119 120 121 122 123 |
# File 'lib/credit_card_validations/detector.rb', line 119 def delete_brand(key) key = key.to_sym undef_brand_method(key) brands.reject! { |k, _| k == key } end |
.has_luhn_check_rule?(key) ⇒ Boolean
82 83 84 |
# File 'lib/credit_card_validations/detector.rb', line 82 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_name ⇒ Object
49 50 51 |
# File 'lib/credit_card_validations/detector.rb', line 49 def brand_name self.class.brand_name(brand) end |
#valid?(*brands) ⇒ Boolean
credit card number validation
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
45 46 47 |
# File 'lib/credit_card_validations/detector.rb', line 45 def valid_luhn? @valid_luhn ||= Luhn.valid?(number) end |
#valid_number?(*keys) ⇒ Boolean
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# 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? matched_brands = [] selected_brands.each do |key, brand| match_data = matches_brand?(brand) matched_brands << {brand: key, matched_prefix_length: match_data.to_s.length} if match_data end if matched_brands.present? return matched_brands.sort{|a, b| a[:matched_prefix_length] <=> b[:matched_prefix_length]}.last[:brand] end end nil end |