Module: Freemium::CreditCard::ClassMethods

Defined in:
lib/freemium/credit_card.rb

Overview

Class Methods

Instance Method Summary collapse

Instance Method Details

#card_companiesObject



81
82
83
# File 'lib/freemium/credit_card.rb', line 81

def card_companies
  CARD_COMPANIES
end

#card_type?(number) ⇒ Boolean

Returns a string containing the type of card from the list of known information below. Need to check the cards in a particular order, as there is some overlap of the allowable ranges – TODO Refactor this method. We basically need to tighten up the Maestro Regexp.

Right now the Maestro regexp overlaps with the MasterCard regexp (IIRC). If we can tighten things up, we can boil this whole thing down to something like…

def type?(number)
  return 'visa' if valid_test_mode_card_number?(number)
  card_companies.find([nil]) { |type, regexp| number =~ regexp }.first.dup
end

Returns:

  • (Boolean)


98
99
100
101
102
103
104
105
106
# File 'lib/freemium/credit_card.rb', line 98

def card_type?(number)
  card_companies.reject { |c,p| c == 'maestro' }.each do |company, pattern|
    return company.dup if number =~ pattern
  end

  return 'maestro' if number =~ card_companies['maestro']

  return nil
end

#last_digits(number) ⇒ Object



108
109
110
# File 'lib/freemium/credit_card.rb', line 108

def last_digits(number)
  number.to_s.length <= 4 ? number : number.to_s.slice(-4..-1)
end

#mask(number) ⇒ Object



112
113
114
# File 'lib/freemium/credit_card.rb', line 112

def mask(number)
  "XXXX-XXXX-XXXX-#{last_digits(number)}"
end

#matching_card_type?(number, card_type) ⇒ Boolean

Checks to see if the calculated type matches the specified type

Returns:

  • (Boolean)


117
118
119
# File 'lib/freemium/credit_card.rb', line 117

def matching_card_type?(number, card_type)
  card_type?(number) == card_type
end

#valid_number?(number) ⇒ Boolean

Returns true if it validates. Optionally, you can pass a card type as an argument and make sure it is of the correct type.

References:

Returns:

  • (Boolean)


71
72
73
74
# File 'lib/freemium/credit_card.rb', line 71

def valid_number?(number)
  valid_card_number_length?(number) &&
    valid_checksum?(number)
end