card_number_validator 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 792adead3f31ccece3c62f991b8964f92dc4e3e4
4
+ data.tar.gz: 5fa9c34c2610a977061f73c83a836b267fafb709
5
+ SHA512:
6
+ metadata.gz: ead04a8bd9acdd54ed661e692c77557830b456125c196cf504765ea05c6c130fa3a4d3dd3e30e96e1ff0e6f61f843bdf3a87309658ee04ae5279672557fe7a8a
7
+ data.tar.gz: 80ab605c24bc73ecd7c9c3977c06e7fd5dd75a8a748305b4ec511842b3cd0ff042d24d6b487ea6ee91ed59e74edc939998f01418b676e9812f31936f78cac899
File without changes
@@ -0,0 +1 @@
1
+ coverage/**/*
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --require spec_helper
2
+ --color
3
+ --format documentation
@@ -0,0 +1,17 @@
1
+ env:
2
+ global:
3
+ - CC_TEST_REPORTER_ID=0068493eada9bebcf78849cdb6abeb964653cd7646a000519cf148c9731de82a
4
+ language: ruby
5
+ rvm:
6
+ - 2.3.3
7
+ before_install: gem install bundler -v 1.16.1
8
+ notifications:
9
+ - false
10
+ before_script:
11
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
12
+ - chmod +x ./cc-test-reporter
13
+ - ./cc-test-reporter before-build
14
+ scripts:
15
+ - bundle exec rspec spec
16
+ after_script:
17
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
@@ -0,0 +1,49 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ card_number_validator (0.1.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ansi (1.5.0)
10
+ diff-lcs (1.3)
11
+ docile (1.3.0)
12
+ hirb (0.7.3)
13
+ json (2.1.0)
14
+ rake (12.3.1)
15
+ rspec (3.7.0)
16
+ rspec-core (~> 3.7.0)
17
+ rspec-expectations (~> 3.7.0)
18
+ rspec-mocks (~> 3.7.0)
19
+ rspec-core (3.7.1)
20
+ rspec-support (~> 3.7.0)
21
+ rspec-expectations (3.7.0)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.7.0)
24
+ rspec-mocks (3.7.0)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.7.0)
27
+ rspec-support (3.7.1)
28
+ simplecov (0.16.1)
29
+ docile (~> 1.1)
30
+ json (>= 1.8, < 3)
31
+ simplecov-html (~> 0.10.0)
32
+ simplecov-console (0.4.2)
33
+ ansi
34
+ hirb
35
+ simplecov
36
+ simplecov-html (0.10.2)
37
+
38
+ PLATFORMS
39
+ ruby
40
+
41
+ DEPENDENCIES
42
+ card_number_validator!
43
+ rake
44
+ rspec
45
+ simplecov
46
+ simplecov-console
47
+
48
+ BUNDLED WITH
49
+ 1.16.1
@@ -0,0 +1,22 @@
1
+ Copyright © 2007-2008 Sharon Rosner Copyright © 2008-2018 Jeremy Evans
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the “Software”), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom
9
+ the Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE
19
+ FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,78 @@
1
+ # card_number_validator [![Build Status][travis-badge]][travis] [![Code Climate][code-climate-badge]][code-climate]
2
+
3
+ Card Validator provides you some methods to validate the most used card flags in the world.
4
+ In addition, you can generate fake card numbers for development and tests purposes.
5
+
6
+ ## Installation
7
+
8
+ Add `card_number_validator` to `Gemfile`
9
+ `Gemfile`:
10
+
11
+ ```ruby
12
+ gem 'card_number_validator', '0.1.0'
13
+ ```
14
+
15
+ Download and install by running:
16
+
17
+ ```
18
+ bundle install
19
+ ```
20
+
21
+ ----
22
+
23
+ ## Usage
24
+
25
+ ### CardNumberValidator.flag(card_number)
26
+ Returns flag name to provided card number or :unknown
27
+ when the provided number is invalid
28
+
29
+ See Example:
30
+ ``` ruby
31
+ CardNumberValidator.flag('378282246310005') # :amex
32
+ CardNumberValidator.flag('30569309025904') # :diners
33
+ CardNumberValidator.flag('6363683839698868') # :elo
34
+ CardNumberValidator.flag('6062826289081176') # :hipercard
35
+ CardNumberValidator.flag('4012888888881881') # :visa
36
+ CardNumberValidator.flag('5223358786371817') # :mastercard
37
+ CardNumberValidator.flag('111') # :unknown
38
+ ```
39
+ ### CardNumberValidator.valid?(card_number)
40
+ Returns if provided card number is valid or not
41
+
42
+ See Example:
43
+ ``` ruby
44
+ CardNumberValidator.valid?('378282246310005') # true
45
+ CardNumberValidator.valid?('30569309025904') # true
46
+ CardNumberValidator.valid?('6363683839698868') # true
47
+ CardNumberValidator.valid?('6062826289081176') # true
48
+ CardNumberValidator.valid?('4012888888881881') # true
49
+ CardNumberValidator.valid?('5223358786371817') # true
50
+ CardNumberValidator.valid?('111') # false
51
+ ```
52
+
53
+ ### CardNumberValidator.generate(:flag_name)
54
+ Returns fake card number by provided flag name
55
+
56
+ See Example:
57
+ ``` ruby
58
+ CardNumberValidator.generate(:amex) # '378282246310005'
59
+ ```
60
+
61
+ ## Contributing
62
+
63
+ Card Validator is open source, and we are grateful for
64
+ [everyone][contributors] who's contributed so far.
65
+
66
+ ## License
67
+
68
+ It is free software, and may be redistributed under the terms specified in the [MIT-LICENSE](MIT-LICENSE) file.
69
+
70
+ ## Feature Requests & Bugs
71
+
72
+ See <http://github.com/pauloh-silva/card_number_validator/issues>
73
+
74
+ [contributors]: https://github.com/pauloh-silva/card_number_validator/contributors
75
+ [travis]: https://travis-ci.org/pauloh-silva/card_number_validator
76
+ [travis-badge]: https://travis-ci.org/pauloh-silva/card_number_validator.svg?branch=master
77
+ [code-climate]: https://codeclimate.com/github/pauloh-silva/card_number_validator
78
+ [code-climate-badge]: https://codeclimate.com/github/codeclimate/codeclimate/badges/gpa.svg
@@ -0,0 +1,17 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'card_number_validator'
3
+ s.version = '0.1.1'
4
+ s.authors = ['Paulo Henrique da Silva']
5
+ s.email = ['paulo.silva1337@gmail.com']
6
+ s.homepage = 'https://github.com/pauloh-silva/card_number_validator'
7
+ s.summary = 'Ruby library to validate payment card numbers'
8
+ s.license = 'MIT'
9
+
10
+ s.files = `git ls-files`.split("\n")
11
+ s.test_files = `git ls-files -- spec/*`.split("\n")
12
+
13
+ s.add_development_dependency 'rake'
14
+ s.add_development_dependency 'rspec'
15
+ s.add_development_dependency 'simplecov'
16
+ s.add_development_dependency 'simplecov-console'
17
+ end
@@ -0,0 +1,40 @@
1
+ require 'securerandom'
2
+ require 'card_number_validator/cards/amex'
3
+ require 'card_number_validator/cards/diners'
4
+ require 'card_number_validator/cards/elo'
5
+ require 'card_number_validator/cards/hiper'
6
+ require 'card_number_validator/cards/hipercard'
7
+ require 'card_number_validator/cards/mastercard'
8
+ require 'card_number_validator/cards/visa'
9
+
10
+ module CardNumberValidator
11
+
12
+ FLAGS = %i(
13
+ amex diners elo hiper hipercard mastercard visa
14
+ )
15
+
16
+ def self.flag(card_number)
17
+ card_number = card_number.to_s
18
+ self::FLAGS.each do |flag|
19
+ flag_found = flag_class(flag).send('validate', card_number)
20
+ return flag if flag_found
21
+ end
22
+ :unknown
23
+ end
24
+
25
+ def self.valid?(card_number)
26
+ flag(card_number) != :unknown
27
+ end
28
+
29
+ def self.generate(flag)
30
+ flag = flag.to_sym
31
+ return unless self::FLAGS.include?(flag)
32
+ flag_class(flag).send('generate')
33
+ end
34
+
35
+ def self.flag_class(flag)
36
+ flag_class = "CardNumberValidator::Cards::#{flag.to_s.capitalize}"
37
+ flag_class = 'CardNumberValidator::Cards::Diners' if flag == :diners
38
+ Kernel.const_get flag_class
39
+ end
40
+ end
@@ -0,0 +1,14 @@
1
+ module CardNumberValidator
2
+ module Cards
3
+ class Amex
4
+
5
+ def self.validate(card_number)
6
+ card_number ? /^3(7|4)\d{13}/.match(card_number) : false
7
+ end
8
+
9
+ def self.generate
10
+ "37#{rand(10**14).to_s.rjust(14,rand(1..9).to_s)}"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module CardNumberValidator
2
+ module Cards
3
+ class Diners
4
+
5
+ def self.validate(card_number)
6
+ card_number ? /^3\d{13}/.match(card_number) : false
7
+ end
8
+
9
+ def self.generate
10
+ "3#{rand(10**13).to_s.rjust(13,rand(1..9).to_s)}"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,21 @@
1
+ module CardNumberValidator
2
+ module Cards
3
+ class Elo
4
+ ELO_BINS = %w[
5
+ 401178 401179 431274 438935 451416
6
+ 457393 457631 457632 504175 627780
7
+ 636297 636368 636369
8
+ ].freeze
9
+
10
+ def self.validate(card_number)
11
+ return unless card_number && card_number.size == 16
12
+ self::ELO_BINS.include? card_number[0..5]
13
+ end
14
+
15
+ def self.generate
16
+ card_prefix = self::ELO_BINS[rand(0..self::ELO_BINS.size-1)]
17
+ "#{card_prefix}#{rand(10**10).to_s.rjust(10,rand(1..9).to_s)}"
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,17 @@
1
+ module CardNumberValidator
2
+ module Cards
3
+ class Hiper
4
+ HIPER_BINS = %w[637095 637612 637599 637609 637568].freeze
5
+
6
+ def self.validate(card_number)
7
+ return unless card_number && card_number.size == 16
8
+ self::HIPER_BINS.include? card_number[0..5]
9
+ end
10
+
11
+ def self.generate
12
+ card_prefix = self::HIPER_BINS[rand(0..self::HIPER_BINS.size-1)]
13
+ "#{card_prefix}#{rand(10**10).to_s.rjust(10,rand(1..9).to_s)}"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ module CardNumberValidator
2
+ module Cards
3
+ class Hipercard
4
+
5
+ def self.validate(card_number)
6
+ return unless card_number
7
+ [
8
+ /^606282\d{10}$/,
9
+ /^3841(0|4|6)0\d{13}$/
10
+ ].each { |rgx| return true if rgx.match(card_number) }
11
+ false
12
+ end
13
+
14
+ def self.generate
15
+ "606282#{rand(10**10).to_s.rjust(10,rand(1..9).to_s)}"
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,29 @@
1
+ module CardNumberValidator
2
+ module Cards
3
+ class Mastercard
4
+
5
+ MASTERCARD_RANGES = [
6
+ [222_100, 272_099]
7
+ ].freeze
8
+
9
+ def self.validate(card_number)
10
+ return unless card_number
11
+ /5\d{15}/.match(card_number) || valid_master_bin?(card_number)
12
+ end
13
+
14
+ def self.valid_master_bin?(card_number)
15
+ return unless card_number.size > 5
16
+ self::MASTERCARD_RANGES.each do |mc_range|
17
+ next unless (mc_range[0]..mc_range[1]).include?(card_number[0..5].to_i)
18
+ return true
19
+ end
20
+ false
21
+ end
22
+
23
+ def self.generate
24
+ master_range = self::MASTERCARD_RANGES[rand(0..self::MASTERCARD_RANGES.size-1)]
25
+ "#{rand(master_range[0]..master_range[1])}#{rand(10**10).to_s.rjust(10,rand(1..9).to_s)}"
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,14 @@
1
+ module CardNumberValidator
2
+ module Cards
3
+ class Visa
4
+
5
+ def self.validate(card_number)
6
+ /^4\d{15}/.match(card_number)
7
+ end
8
+
9
+ def self.generate
10
+ "4#{rand(10**15).to_s.rjust(15,rand(1..9).to_s)}"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,62 @@
1
+ RSpec.describe CardNumberValidator do
2
+ before do
3
+ @valid_card_numbers = {
4
+ amex: '378282246310005',
5
+ diners: '30569309025904',
6
+ elo: '6363683839698868',
7
+ hipercard: '6062826289081176',
8
+ visa: '4012888888881881',
9
+ mastercard: '5223358786371817'
10
+ }
11
+ end
12
+
13
+ context 'Checking card flag' do
14
+ describe 'Valid card numbers' do
15
+ it 'Should returns card flag name' do
16
+ @valid_card_numbers.each do |flag, number|
17
+ expect(CardNumberValidator.flag(number)).to eq flag
18
+ end
19
+ end
20
+ end
21
+
22
+ describe 'Invalid card numbers' do
23
+ it 'Should returns :unknow as card flag name' do
24
+ expect(CardNumberValidator.flag(111)).to eq :unknown
25
+ end
26
+ end
27
+ end
28
+
29
+ context 'Checking card validity' do
30
+ describe 'Valid card numbers' do
31
+ it 'Should returns true' do
32
+ @valid_card_numbers.each do |flag, number|
33
+ expect(CardNumberValidator.valid?(number)).to be_truthy
34
+ end
35
+ end
36
+ end
37
+
38
+ describe 'Invalid card numbers' do
39
+ it 'Should returns false' do
40
+ expect(CardNumberValidator.valid?(111)).to be_falsey
41
+ end
42
+ end
43
+ end
44
+
45
+ context 'Checking card generation' do
46
+ describe 'Valid flags' do
47
+ it 'Should create a valid card number to provided flag' do
48
+ @valid_card_numbers.each do |flag, _number|
49
+ generated_card_number = CardNumberValidator.generate(flag)
50
+ expect(CardNumberValidator.flag(generated_card_number)).to eq flag
51
+ end
52
+ end
53
+ end
54
+
55
+ describe 'Invalid flags' do
56
+ it 'Should returns empty response' do
57
+ generated_card_number = CardNumberValidator.generate(:invalid_flag)
58
+ expect(generated_card_number).to be_nil
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,27 @@
1
+ require 'simplecov'
2
+ require 'simplecov-console'
3
+
4
+ require "#{Dir.getwd}/lib/card_number_validator"
5
+ SimpleCov.formatter = SimpleCov::Formatter::Console
6
+ SimpleCov.start
7
+
8
+ RSpec.configure do |config|
9
+ config.expect_with :rspec do |expectations|
10
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
11
+ end
12
+
13
+ config.mock_with :rspec do |mocks|
14
+ mocks.verify_partial_doubles = true
15
+ end
16
+
17
+ config.shared_context_metadata_behavior = :apply_to_host_groups
18
+
19
+ # The settings below are suggested to provide a good initial experience
20
+ # with RSpec, but feel free to customize to your heart's content.
21
+ config.filter_run_when_matching :focus
22
+ config.disable_monkey_patching!
23
+ config.warnings = true
24
+
25
+ config.profile_examples = 1
26
+ config.order = :random
27
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: card_number_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Paulo Henrique da Silva
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-04-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: simplecov
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov-console
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - paulo.silva1337@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".editorconfig"
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - MIT-LICENSE
83
+ - README.md
84
+ - bin/versions/card_validator-0.1.0.gem
85
+ - card_number_validator.gemspec
86
+ - lib/card_number_validator.rb
87
+ - lib/card_number_validator/cards/amex.rb
88
+ - lib/card_number_validator/cards/diners.rb
89
+ - lib/card_number_validator/cards/elo.rb
90
+ - lib/card_number_validator/cards/hiper.rb
91
+ - lib/card_number_validator/cards/hipercard.rb
92
+ - lib/card_number_validator/cards/mastercard.rb
93
+ - lib/card_number_validator/cards/visa.rb
94
+ - spec/lib/card_number_validator_spec.rb
95
+ - spec/spec_helper.rb
96
+ homepage: https://github.com/pauloh-silva/card_number_validator
97
+ licenses:
98
+ - MIT
99
+ metadata: {}
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubyforge_project:
116
+ rubygems_version: 2.5.2
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Ruby library to validate payment card numbers
120
+ test_files:
121
+ - spec/lib/card_number_validator_spec.rb
122
+ - spec/spec_helper.rb