Class: ActiveModel::Validations::CreditCardFieldsValidator
- Inherits:
-
Validator
- Object
- Validator
- ActiveModel::Validations::CreditCardFieldsValidator
- Defined in:
- lib/validate_credit_card_fields.rb
Constant Summary collapse
- ERROR_TYPES =
[:invalid, :blank, :not_supported]
- PROVIDERS =
{ visa: /^4[0-9]{12}(?:[0-9]{3})?$/, master_card: /^5[1-5][0-9]{14}$/, maestro: /^(?:5[0678]\d\d|6304|6390|67\d\d)\d{8,15}$/, diners_club: /^3(?:0[0-5]|[68][0-9])[0-9]{11}$/, amex: /^3[47][0-9]{13}$/, discover: /^6(?:011|5[0-9]{2})[0-9]{12}$/, jcb: /^(?:2131|1800|35\d{3})\d{11}$/, solo: /(^(6334)[5-9](\d{11}$|\d{13,14}$)) |(^(6767)(\d{12}$|\d{14,15}$))/, china_union: /^62[0-5]\d{13,16}$/, dankort: /^5019\d{12}$/ }
Instance Attribute Summary collapse
-
#cc_cvv ⇒ Object
Returns the value of attribute cc_cvv.
-
#cc_first_name ⇒ Object
Returns the value of attribute cc_first_name.
-
#cc_last_name ⇒ Object
Returns the value of attribute cc_last_name.
-
#cc_month ⇒ Object
Returns the value of attribute cc_month.
-
#cc_number ⇒ Object
Returns the value of attribute cc_number.
-
#cc_owner ⇒ Object
Returns the value of attribute cc_owner.
-
#cc_providers ⇒ Object
Returns the value of attribute cc_providers.
-
#cc_type ⇒ Object
Returns the value of attribute cc_type.
-
#cc_year ⇒ Object
Returns the value of attribute cc_year.
-
#custom_messages ⇒ Object
Returns the value of attribute custom_messages.
-
#options ⇒ Object
Returns the value of attribute options.
-
#using_owner ⇒ Object
Returns the value of attribute using_owner.
Instance Method Summary collapse
- #init_option(key, default) ⇒ Object
- #init_option_from_hash(hash, default) ⇒ Object
-
#initialize(options = {}) ⇒ CreditCardFieldsValidator
constructor
A new instance of CreditCardFieldsValidator.
- #validate(record) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ CreditCardFieldsValidator
Returns a new instance of CreditCardFieldsValidator.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/validate_credit_card_fields.rb', line 30 def initialize(={}) @options = @custom_messages = {} @cc_number = init_option(:number, :cc_number) @cc_cvv = init_option(:cvv, :cc_cvv) @cc_month = init_option(:month, :cc_month) @cc_year = init_option(:year, :cc_year) @cc_providers = [:providers] @using_owner = ![:owner].nil? || [:first_name].nil? || [:last_name].nil? if @using_owner @cc_owner = init_option(:owner, :cc_owner) else @cc_first_name = init_option(:first_name, :cc_first_name) @cc_last_name = init_option(:last_name, :cc_last_name) end end |
Instance Attribute Details
#cc_cvv ⇒ Object
Returns the value of attribute cc_cvv.
26 27 28 |
# File 'lib/validate_credit_card_fields.rb', line 26 def cc_cvv @cc_cvv end |
#cc_first_name ⇒ Object
Returns the value of attribute cc_first_name.
26 27 28 |
# File 'lib/validate_credit_card_fields.rb', line 26 def cc_first_name @cc_first_name end |
#cc_last_name ⇒ Object
Returns the value of attribute cc_last_name.
26 27 28 |
# File 'lib/validate_credit_card_fields.rb', line 26 def cc_last_name @cc_last_name end |
#cc_month ⇒ Object
Returns the value of attribute cc_month.
26 27 28 |
# File 'lib/validate_credit_card_fields.rb', line 26 def cc_month @cc_month end |
#cc_number ⇒ Object
Returns the value of attribute cc_number.
26 27 28 |
# File 'lib/validate_credit_card_fields.rb', line 26 def cc_number @cc_number end |
#cc_owner ⇒ Object
Returns the value of attribute cc_owner.
26 27 28 |
# File 'lib/validate_credit_card_fields.rb', line 26 def cc_owner @cc_owner end |
#cc_providers ⇒ Object
Returns the value of attribute cc_providers.
26 27 28 |
# File 'lib/validate_credit_card_fields.rb', line 26 def cc_providers @cc_providers end |
#cc_type ⇒ Object
Returns the value of attribute cc_type.
26 27 28 |
# File 'lib/validate_credit_card_fields.rb', line 26 def cc_type @cc_type end |
#cc_year ⇒ Object
Returns the value of attribute cc_year.
26 27 28 |
# File 'lib/validate_credit_card_fields.rb', line 26 def cc_year @cc_year end |
#custom_messages ⇒ Object
Returns the value of attribute custom_messages.
26 27 28 |
# File 'lib/validate_credit_card_fields.rb', line 26 def @custom_messages end |
#options ⇒ Object
Returns the value of attribute options.
26 27 28 |
# File 'lib/validate_credit_card_fields.rb', line 26 def @options end |
#using_owner ⇒ Object
Returns the value of attribute using_owner.
26 27 28 |
# File 'lib/validate_credit_card_fields.rb', line 26 def using_owner @using_owner end |
Instance Method Details
#init_option(key, default) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/validate_credit_card_fields.rb', line 47 def init_option(key, default) if [key].is_a? Hash init_option_from_hash [key], default else [key] || default end end |
#init_option_from_hash(hash, default) ⇒ Object
55 56 57 58 59 |
# File 'lib/validate_credit_card_fields.rb', line 55 def init_option_from_hash(hash, default) field_name = hash[:field] || default [field_name] = hash.select{ |k,_| ERROR_TYPES.include? k } field_name end |
#validate(record) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/validate_credit_card_fields.rb', line 61 def validate(record) @record = record check_fields_format @cc_type = get_cc_type validate_fields_presence validate_cc_number validate_cc_cvv validate_cc_month validate_cc_year validate_cc_expiry_date end |