class CreditCardDetector::Detector

Public Class Methods

new(number) click to toggle source
# File lib/credit_card_detector/detector.rb, line 6
def initialize(number)
  @number = number.to_s.tr('- ', '')
end

Public Instance Methods

brand() click to toggle source
# File lib/credit_card_detector/detector.rb, line 25
def brand
  @brand ||= Data.brands.find { |brand| matches_brand? brand }
end
brand_id() click to toggle source
# File lib/credit_card_detector/detector.rb, line 29
def brand_id
  brand.id if brand
end
brand_name() click to toggle source
# File lib/credit_card_detector/detector.rb, line 33
def brand_name
  brand.name if brand
end
valid?(*brands) click to toggle source
# File lib/credit_card_detector/detector.rb, line 10
def valid?(*brands)
  brands.compact!

  if brands.empty?
    !brand.nil?
  else
    validate_brands(brands)
    brands.include?(brand.id)
  end
end
valid_luhn?() click to toggle source
# File lib/credit_card_detector/detector.rb, line 21
def valid_luhn?
  @valid_luhn ||= Luhn.valid?(@number)
end

Protected Instance Methods

matches_brand?(brand) click to toggle source
# File lib/credit_card_detector/detector.rb, line 39
def matches_brand?(brand)
  return if brand.luhn? && !valid_luhn?
  brand.matches?(@number)
end
validate_brands(brands) click to toggle source
# File lib/credit_card_detector/detector.rb, line 44
def validate_brands(brands)
  brands.each do |key|
    next if Data.ids.include?(key)
    fail Error, "brand #{key} is undefined"
  end
end