Class: CreditCardDetector::Detector

Inherits:
Object
  • Object
show all
Defined in:
lib/credit_card_detector/detector.rb

Instance Method Summary collapse

Constructor Details

#initialize(number) ⇒ Detector

Returns a new instance of Detector.



6
7
8
# File 'lib/credit_card_detector/detector.rb', line 6

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

Instance Method Details

#brandObject



25
26
27
# File 'lib/credit_card_detector/detector.rb', line 25

def brand
  @brand ||= Data.brands.find { |brand| matches_brand? brand }
end

#brand_idObject



29
30
31
# File 'lib/credit_card_detector/detector.rb', line 29

def brand_id
  brand.id if brand
end

#brand_nameObject



33
34
35
# File 'lib/credit_card_detector/detector.rb', line 33

def brand_name
  brand.name if brand
end

#valid?(*brands) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
# 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?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/credit_card_detector/detector.rb', line 21

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