Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New match brand logic. #127

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/credit_card_validations/detector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@ def brand(*keys)
def valid_number?(*keys)
selected_brands = keys.blank? ? self.brands : resolve_keys(*keys)
if selected_brands.any?
matched_brands = []
selected_brands.each do |key, brand|
return key if matches_brand?(brand)
match_data = matches_brand?(brand)
matched_brands << {brand: key, matched_prefix_length: match_data.to_s.length} if match_data
end

if matched_brands.present?
return matched_brands.sort{|a, b| a[:matched_prefix_length] <=> b[:matched_prefix_length]}.last[:brand]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pavelgyravel i think can be implemented in more easy way , commented PR with separate message take a look please

end
end
nil
Expand Down Expand Up @@ -64,8 +70,8 @@ def matches_brand?(brand)
rules.each do |rule|
if (options[:skip_luhn] || valid_luhn?) &&
rule[:length].include?(number.length) &&
number.match(rule[:regexp])
return true
match_data = number.match(rule[:regexp])
return match_data
end
end
false
Expand Down
2 changes: 1 addition & 1 deletion lib/credit_card_validations/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module CreditCardValidations
VERSION = '6.1.0'
VERSION = '6.2.0'
end
22 changes: 22 additions & 0 deletions spec/credit_card_validations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,28 @@
end
end
end

describe 'Add kortimilli rule' do
let(:kortimilli_number) { '505827028341713' }

it 'before invoke add_brand credit card behaves like maestro' do
expect(detector(kortimilli_number).valid?(:maestro)).must_equal true
expect(detector(kortimilli_number).maestro?).must_equal true
expect(detector(kortimilli_number).brand).must_equal :maestro
end

describe 'after adding wider prefix for kortimilli' do
before do
CreditCardValidations::Detector.add_brand(:kortimilli, length: 15, prefixes: '505827028')
end

it 'should validate number as kortimilli' do
expect(detector(kortimilli_number).valid?(:kortimilli)).must_equal true
expect(detector(kortimilli_number).kortimilli?).must_equal true
expect(detector(kortimilli_number).brand).must_equal :kortimilli
end
end
end
end

describe 'plugins' do
Expand Down
Loading