diff --git a/.hound.yml b/.hound.yml new file mode 100644 index 0000000..7f3d303 --- /dev/null +++ b/.hound.yml @@ -0,0 +1,2 @@ +StringLiterals: + EnforcedStyle: single_quotes \ No newline at end of file diff --git a/lib/credit_card_validations/detector.rb b/lib/credit_card_validations/detector.rb index a256672..625c496 100644 --- a/lib/credit_card_validations/detector.rb +++ b/lib/credit_card_validations/detector.rb @@ -119,7 +119,7 @@ def delete_brand(key) #create rule for detecting brand def add_rule(key, length, prefixes) unless brands.has_key?(key) - raise CreditCardValidations::Error.new("brand #{key} is undefined, please use #add_brand method") + 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} diff --git a/lib/credit_card_validations/error.rb b/lib/credit_card_validations/error.rb index 4638447..0738fd2 100644 --- a/lib/credit_card_validations/error.rb +++ b/lib/credit_card_validations/error.rb @@ -1,4 +1,4 @@ module CreditCardValidations class Error < StandardError end -end \ No newline at end of file +end diff --git a/lib/credit_card_validations/factory.rb b/lib/credit_card_validations/factory.rb index e38deaf..6e74223 100644 --- a/lib/credit_card_validations/factory.rb +++ b/lib/credit_card_validations/factory.rb @@ -12,13 +12,11 @@ module CreditCardValidations class Factory class << self def random(brand = nil) - if brand.nil? - brand = Detector.brands.keys.sample - else - raise CreditCardValidations::Error.new("Unsupported brand") if Detector.brands[brand].nil? + brand = Detector.brands.keys.sample if brand.nil? + if Detector.brands[brand].nil? + raise Error.new('Unsupported brand') end generate(Detector.brands[brand][:rules].sample) - end def generate(rule) diff --git a/lib/credit_card_validations/plugins/diners_us.rb b/lib/credit_card_validations/plugins/diners_us.rb index 1f7023c..6c80c25 100644 --- a/lib/credit_card_validations/plugins/diners_us.rb +++ b/lib/credit_card_validations/plugins/diners_us.rb @@ -1,8 +1,6 @@ -# :diners_us: -# :rules: -# - :length: -# - 16 -# :prefixes: -# - '54' -# - '55' -CreditCardValidations.add_brand(:diners_us, {length: 16, prefixes: %w(54 55)}, {brand_name: 'Diners Club US'}) \ No newline at end of file +CreditCardValidations.add_brand( + :diners_us, + { length: 16, prefixes: %w(54 55) }, + brand_name: 'Diners Club US' +) + diff --git a/lib/credit_card_validations/plugins/en_route.rb b/lib/credit_card_validations/plugins/en_route.rb index 198e726..dbb969e 100644 --- a/lib/credit_card_validations/plugins/en_route.rb +++ b/lib/credit_card_validations/plugins/en_route.rb @@ -1 +1,5 @@ -CreditCardValidations.add_brand(:en_route, {length: 15, prefixes: %w(2014 2149)}, {skip_luhn: true}) \ No newline at end of file +CreditCardValidations.add_brand( + :en_route, + { length: 15, prefixes: %w(2014 2149) }, + skip_luhn: true +) diff --git a/lib/credit_card_validations/plugins/laser.rb b/lib/credit_card_validations/plugins/laser.rb index 82919ea..8ff90a6 100644 --- a/lib/credit_card_validations/plugins/laser.rb +++ b/lib/credit_card_validations/plugins/laser.rb @@ -1,12 +1,4 @@ -# :laser: -# :rules: -# - :length: -# - 16 -# - 17 -# - 18 -# - 19 -# :prefixes: -# - '6304' -# - '6706' -# - '6771' -CreditCardValidations.add_brand(:laser, {length: [16, 17, 18, 19], prefixes: %w(6304 6706 6771)}) \ No newline at end of file +CreditCardValidations.add_brand( + :laser, + length: [16, 17, 18, 19], prefixes: %w(6304 6706 6771) +) diff --git a/lib/credit_card_validations/version.rb b/lib/credit_card_validations/version.rb index 42fc94d..f281e72 100644 --- a/lib/credit_card_validations/version.rb +++ b/lib/credit_card_validations/version.rb @@ -1,3 +1,3 @@ module CreditCardValidations - VERSION = "3.0.0" + VERSION = '3.0.0' end diff --git a/lib/plugins/diners_us.rb b/lib/plugins/diners_us.rb deleted file mode 100644 index 1f7023c..0000000 --- a/lib/plugins/diners_us.rb +++ /dev/null @@ -1,8 +0,0 @@ -# :diners_us: -# :rules: -# - :length: -# - 16 -# :prefixes: -# - '54' -# - '55' -CreditCardValidations.add_brand(:diners_us, {length: 16, prefixes: %w(54 55)}, {brand_name: 'Diners Club US'}) \ No newline at end of file diff --git a/lib/plugins/en_route.rb b/lib/plugins/en_route.rb deleted file mode 100644 index 198e726..0000000 --- a/lib/plugins/en_route.rb +++ /dev/null @@ -1 +0,0 @@ -CreditCardValidations.add_brand(:en_route, {length: 15, prefixes: %w(2014 2149)}, {skip_luhn: true}) \ No newline at end of file diff --git a/lib/plugins/laser.rb b/lib/plugins/laser.rb deleted file mode 100644 index 82919ea..0000000 --- a/lib/plugins/laser.rb +++ /dev/null @@ -1,12 +0,0 @@ -# :laser: -# :rules: -# - :length: -# - 16 -# - 17 -# - 18 -# - 19 -# :prefixes: -# - '6304' -# - '6706' -# - '6771' -CreditCardValidations.add_brand(:laser, {length: [16, 17, 18, 19], prefixes: %w(6304 6706 6771)}) \ No newline at end of file diff --git a/spec/active_model_spec.rb b/spec/active_model_spec.rb index 3889034..68f6d93 100644 --- a/spec/active_model_spec.rb +++ b/spec/active_model_spec.rb @@ -1,13 +1,13 @@ require_relative 'test_helper' -describe "ActiveModel Validator" do +describe 'ActiveModel Validator' do let(:model) { CreditCard.new } - describe "Proc support" do - it "should be valid if brands from proc valid" do + describe 'Proc support' do + it 'should be valid if brands from proc valid' do card = model.dup - card.card_type = "Master Card" + card.card_type = 'Master Card' card.number6 = CreditCardValidations::Factory.random(:visa) card.valid?.must_equal false card.number6 = CreditCardValidations::Factory.random(:mastercard) @@ -15,8 +15,8 @@ end end - describe "Any Brand" do - it "should be valid for all prepared valid numbers" do + describe 'Any Brand' do + it 'should be valid for all prepared valid numbers' do VALID_NUMBERS.each do |_, numbers| numbers.each do |number| card = model @@ -28,8 +28,8 @@ end end - describe "Except Amex and Maestro brand" do - it "should reject all other valid numbers" do + describe 'Except Amex and Maestro brand' do + it 'should reject all other valid numbers' do VALID_NUMBERS.except(:amex, :maestro).each do |_, numbers| card = model card.number = numbers.first @@ -38,7 +38,7 @@ end end - it "should accept using except options" do + it 'should accept using except options' do VALID_NUMBERS.except(:amex, :maestro).each do |_, numbers| card = model card.number3 = numbers.first @@ -47,8 +47,8 @@ end end - describe "Only Amex and Mestro brands" do - it "should accept amex and maestro brand if valid" do + describe 'Only Amex and Mestro brands' do + it 'should accept amex and maestro brand if valid' do VALID_NUMBERS.slice(:amex, :maestro).each do |_, numbers| card = model card.number = numbers.first @@ -58,12 +58,12 @@ end end - describe "Custom error message" do - it "should allow custom message" do + describe 'Custom error message' do + it 'should allow custom message' do card = model card.number7 = 'wrong' card.valid?.must_equal false - card.errors[:number7].must_equal ["Custom message"] + card.errors[:number7].must_equal ['Custom message'] end end diff --git a/spec/credit_card_validations_spec.rb b/spec/credit_card_validations_spec.rb index 447af28..edadcf6 100644 --- a/spec/credit_card_validations_spec.rb +++ b/spec/credit_card_validations_spec.rb @@ -7,29 +7,28 @@ CreditCardValidations.reload! end - describe "MMI" do - it "should detect issuer category" do + describe 'MMI' do + it 'should detect issuer category' do d = detector(VALID_NUMBERS[:visa].first) d.issuer_category.must_equal CreditCardValidations::Mmi::ISSUER_CATEGORIES[d.number[0]] end end - describe "Luhn#valid?" do + describe 'Luhn#valid?' do let(:card_detector) { detector(VALID_NUMBERS[:unionpay].first) } - it "should call Luhn.valid? once" do + it 'should call Luhn.valid? once' do CreditCardValidations::Luhn.expects(:valid?).with(card_detector.number).once card_detector.valid?(:visa, :unionpay).must_equal true end - it "should call Luhn.valid? twice" do + it 'should call Luhn.valid? twice' do CreditCardValidations::Luhn.expects(:valid?).with(card_detector.number).twice card_detector.valid?(:visa, :mastercard).must_equal false end - it "should not call Luhn.valid?" do - + it 'should not call Luhn.valid?' do CreditCardValidations::Luhn.expects(:valid?).never card_detector.valid?(:unionpay).must_equal true end @@ -37,7 +36,7 @@ end - it "should check luhn" do + it 'should check luhn' do VALID_NUMBERS.each do |brand, card_numbers| if has_luhn_check_rule?(brand) card_numbers.each do |number| @@ -47,7 +46,7 @@ end end - it "should check valid brand" do + it 'should check valid brand' do VALID_NUMBERS.each do |brand, card_numbers| card_numbers.each do |card_number| detector(card_number).send("#{brand}?").must_equal true @@ -56,7 +55,7 @@ end end - it "should check if card invalid" do + it 'should check if card invalid' do INVALID_NUMBERS.each do |card_number| detector(card_number).valid?.must_equal false detector(card_number).brand.must_be_nil @@ -66,14 +65,14 @@ end end - it "should detect by full brand name" do + it 'should detect by full brand name' do amex = CreditCardValidations::Factory.random(:amex) detector(amex).valid?('American Express').must_equal true visa = CreditCardValidations::Factory.random(:visa) detector(visa).valid?('American Express').must_equal false end - it "should support multiple brands for single check" do + it 'should support multiple brands for single check' do VALID_NUMBERS.slice(:visa, :mastercard).each do |key, value| detector(value.first).brand(:visa, :mastercard).must_equal key end @@ -83,7 +82,7 @@ end end - it "should check if valid brand without arguments" do + it 'should check if valid brand without arguments' do VALID_NUMBERS.each do |key, value| value.each do |card_number| detector(card_number).valid?(key).must_equal true @@ -92,47 +91,47 @@ end end - it "should not be valid? if wrong brand" do + it 'should not be valid? if wrong brand' do detector(VALID_NUMBERS[:visa].first).valid?(:mastercard).must_equal false detector(VALID_NUMBERS[:mastercard].first).valid?(:visa).must_equal false end - it "should be valid? if right brand" do + it 'should be valid? if right brand' do detector(VALID_NUMBERS[:visa].first).valid?(:mastercard, :visa).must_equal true detector(VALID_NUMBERS[:visa].first).valid?(:mastercard, :amex).must_equal false end - describe "adding/removing brand" do + describe 'adding/removing brand' do - describe "adding rules" do + describe 'adding rules' do let(:voyager_number) { '869926275400212' } - it "should validate number as voyager" do - CreditCardValidations::Detector.add_brand(:voyager, { length: 15, prefixes: '86' }) + it 'should validate number as voyager' do + CreditCardValidations::Detector.add_brand(:voyager, length: 15, prefixes: '86') detector(voyager_number).valid?(:voyager).must_equal true detector(voyager_number).voyager?.must_equal true detector(voyager_number).brand.must_equal :voyager end - describe "Add voyager rule" do + describe 'Add voyager rule' do before do - CreditCardValidations::Detector.add_brand(:voyager, { length: 15, prefixes: '86' }) + CreditCardValidations::Detector.add_brand(:voyager, length: 15, prefixes: '86') end - it "should validate number as voyager" do + it 'should validate number as voyager' do detector(voyager_number).valid?(:voyager).must_equal true detector(voyager_number).voyager?.must_equal true detector(voyager_number).brand.must_equal :voyager end - describe "Remove voyager rule" do + describe 'Remove voyager rule' do before do CreditCardValidations::Detector.delete_brand(:voyager) end - it "should not validate number as voyager" do + it 'should not validate number as voyager' do detector(voyager_number).respond_to?(:voyager?).must_equal false detector(voyager_number).brand.must_be_nil end @@ -140,22 +139,24 @@ end end - describe "plugins" do - + describe 'plugins' do [:diners_us, :en_route, :laser].each do |brand| it "should support #{brand}" do - -> { CreditCardValidations::Factory.random(brand) }.must_raise(CreditCardValidations::Error) - detector('somenumber').respond_to?("#{brand}?").must_equal false + -> { CreditCardValidations::Factory.random(brand) }. + must_raise(CreditCardValidations::Error) + custom_number = 'some_number' + detector(custom_number).respond_to?("#{brand}?").must_equal false require "credit_card_validations/plugins/#{brand}" number = CreditCardValidations::Factory.random(brand) - CreditCardValidations::Detector.new(number).valid?("#{brand}".to_sym).must_equal true - detector('somenumber').respond_to?("#{brand}?").must_equal true + detector(number).valid?("#{brand}".to_sym).must_equal true + detector(custom_number).respond_to?("#{brand}?").must_equal true end end end - it "should raise Error if no brand added before" do - -> { CreditCardValidations::Detector::add_rule(:undefined_brand, 20, [20]) }.must_raise(CreditCardValidations::Error) + it 'should raise Error if no brand added before' do + -> { CreditCardValidations::Detector::add_rule(:undefined_brand, 20, [20]) }. + must_raise(CreditCardValidations::Error) end end diff --git a/spec/factory_spec.rb b/spec/factory_spec.rb index fd473c6..97b93ef 100644 --- a/spec/factory_spec.rb +++ b/spec/factory_spec.rb @@ -2,22 +2,17 @@ describe CreditCardValidations::Factory do - it "should generate random brand" do + it 'should generate random brand' do number = CreditCardValidations::Factory.random CreditCardValidations::Detector.new(number).valid?.must_equal true end CreditCardValidations::Detector.brands.keys.sort.each do |key| describe "#{key}" do - it "should generate valid #{key}" do number = CreditCardValidations::Factory.random(key) CreditCardValidations::Detector.new(number).valid?(key).must_equal true end - end end - - - end \ No newline at end of file diff --git a/spec/models/credit_card.rb b/spec/models/credit_card.rb index 2a3913a..d89f6d0 100644 --- a/spec/models/credit_card.rb +++ b/spec/models/credit_card.rb @@ -1,18 +1,18 @@ class CreditCard attr_accessor :number, :number2, :number3, :number4, :number5, :number6, :number7, :card_type include ActiveModel::Validations - validates :number, credit_card_number: {brands: [:amex, :maestro]} , allow_blank: true - validates :number2, credit_card_number: {only: [:amex, :maestro]}, allow_blank: true - validates :number3, credit_card_number: {except: [:amex, :maestro]} , allow_blank: true - validates :number4, credit_card_number: {brands: :any} , allow_blank: true - validates :number5, credit_card_number: true , allow_blank: true - validates :number6, credit_card_number: { brands: ->(record){ record.supported_brand } } , allow_blank: true - validates :number7, credit_card_number: { message: "Custom message" } , allow_blank: true + validates :number, credit_card_number: { brands: [:amex, :maestro] }, allow_blank: true + validates :number2, credit_card_number: { only: [:amex, :maestro] }, allow_blank: true + validates :number3, credit_card_number: { except: [:amex, :maestro] }, allow_blank: true + validates :number4, credit_card_number: { brands: :any }, allow_blank: true + validates :number5, credit_card_number: true, allow_blank: true + validates :number6, credit_card_number: { brands: ->(record) { record.supported_brand } }, allow_blank: true + validates :number7, credit_card_number: { message: 'Custom message' }, allow_blank: true def supported_brand { - "Master Card" => :mastercard, - "Visa" => :visa + 'Master Card' => :mastercard, + 'Visa' => :visa }[self.card_type] end diff --git a/spec/string_spec.rb b/spec/string_spec.rb index 49812a8..95ccc10 100644 --- a/spec/string_spec.rb +++ b/spec/string_spec.rb @@ -1,22 +1,13 @@ require_relative 'test_helper' require 'credit_card_validations/string' -describe "String ext" do +describe 'String ext' do - let(:mastercard) { - CreditCardValidations::Factory.random(:mastercard) - } - - let(:visa) { - CreditCardValidations::Factory.random(:visa) - } - - let(:invalid) { - INVALID_NUMBERS.sample - } - - it "should allow detect brand for mastercard" do + let(:mastercard) { CreditCardValidations::Factory.random(:mastercard) } + let(:visa) { CreditCardValidations::Factory.random(:visa) } + let(:invalid) { INVALID_NUMBERS.sample } + it 'should allow detect brand for mastercard' do mastercard.credit_card_brand.must_equal :mastercard mastercard.credit_card_brand_name.must_equal 'MasterCard' mastercard.valid_credit_card_brand?(:mastercard).must_equal true @@ -24,16 +15,14 @@ mastercard.valid_credit_card_brand?(:visa, :amex).must_equal false end - it "should allow detect brand for visa" do - + it 'should allow detect brand for visa' do visa.credit_card_brand.must_equal :visa visa.credit_card_brand_name.must_equal 'Visa' visa.valid_credit_card_brand?(:mastercard).must_equal false visa.valid_credit_card_brand?(:visa, :amex).must_equal true end - it "should not allow detect brand for invalid card" do - + it 'should not allow detect brand for invalid card' do invalid.credit_card_brand.must_be_nil invalid.credit_card_brand_name.must_be_nil invalid.valid_credit_card_brand?(:mastercard).must_equal false