valid_credit_card 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f1e9fda95062d2ba3a704757d75e1734318ee2ab
4
+ data.tar.gz: bd83f5f9ad396c8a45eda5d37f27905de98a5042
5
+ SHA512:
6
+ metadata.gz: 2e06c2f88278ae1aff3b36fefa3c4f0b2bc779e7c5e67da8c6c83f42f70de0e7461e3e97c95e674465c8e34aa0b0babf901edfbe802fc83d7cc39062b9999456
7
+ data.tar.gz: cc367daa6262fed4f1f708954ebfaf6044e9ee55aea268fe26008a18fbf7fe3e95770b67ba97e53c5dc30f8f92e82afaae5dcc312a1c3eee9c17d194d44fc1fe
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.4
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in valid_credit_card.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Pablo Moreira Mora
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # ValidCreditCard
2
+
3
+ ActiveModel validation for credit card number.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'valid_credit_card'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install valid_credit_card
20
+
21
+ ## Usage
22
+
23
+ ### Use with ActiveModel
24
+
25
+ If you want to validate that it is a valid credit card number:
26
+ ```ruby
27
+ class User < ActiveRecord::Base
28
+ validates :cc_number, presence: true, credit_card: true
29
+ end
30
+ ```
31
+
32
+ To validate that the credit card type:
33
+ ```ruby
34
+ validates :cc_number, credit_card: { card_types: ['visa'] }
35
+ ```
36
+
37
+ > Note that this gem will let an empty credit card number pass through so you will need to
38
+ > add `presence: true` if you require an credit card number
39
+
40
+ ### Use without ActiveModel
41
+
42
+ ```ruby
43
+ credit_card = ValidCreditCard::CreditCard.new(3530111333300000)
44
+ credit_card.valid? => true
45
+ credit_card.is_visa? => false
46
+ credit_card.is_jbc? => true
47
+ credit_card.card_type? => jbc
48
+ ```
49
+
50
+ ## Requirements
51
+ * Ruby-1.9
52
+ * Ruby-2.0
53
+ * Ruby-2.1
54
+ * Ruby-2.2
55
+ * JRuby-1.9
56
+
57
+ ## Contributing
58
+
59
+ 1. Fork it
60
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
61
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
62
+ 4. Push to the branch (`git push origin my-new-feature`)
63
+ 5. Create new Pull Request
64
+
65
+
66
+ ## License
67
+
68
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ desc "Run specs"
5
+ RSpec::Core::RakeTask.new do |t|
6
+ t.pattern = "spec/**/*_spec.rb"
7
+ end
8
+
9
+ task :default => [:spec]
10
+ task :build => [:spec]
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "valid_credit_card"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,18 @@
1
+ require "valid_credit_card/credit_card_validator"
2
+
3
+ module ValidCreditCard
4
+ def self.test_numbers
5
+ @@test_numbers ||= {
6
+ amex: %w{ 378282246310005 371449635398431 378734493671000 },
7
+ diners_club: %w{ 30569309025904 38520000023237 },
8
+ discover: %w{ 6011000990139424 6011111111111117 },
9
+ master_card: %w{ 5555555555554444 5105105105105100 },
10
+ visa: %w{
11
+ 4111111111111111 4012888888881881 4222222222222
12
+ 4005519200000004 4009348888881881 4012000033330026
13
+ 4012000077777777 4217651111111119 4500600000000061
14
+ 4000111111111115 },
15
+ jcb: %w{ 3530111333300000 3566002020360505 }
16
+ }.values.flatten
17
+ end
18
+ end
@@ -0,0 +1,61 @@
1
+ require "valid_credit_card"
2
+
3
+ module ValidCreditCard
4
+ class CreditCard
5
+ CARD_TYPES = {
6
+ visa: /\A4[0-9]{12}(?:[0-9]{3})?\z/,
7
+ master_card: /\A5[1-5][0-9]{14}\z/,
8
+ maestro: /(\A6759[0-9]{2}([0-9]{10})$)|(\A6759[0-9]{2}([0-9]{12})$)|(\A6759[0-9]{2}([0-9]{13})$)/,
9
+ diners_club: /\A3(?:0[0-5]|[68][0-9])[0-9]{11}\z/,
10
+ amex: /\A3[47][0-9]{13}\z/,
11
+ discover: /\A6(?:011|5[0-9]{2})[0-9]{12}\z/,
12
+ jcb: /\A(?:2131|1800|35\d{3})\d{11}\z/
13
+ }
14
+
15
+ attr_accessor :number
16
+
17
+ # == Initializer
18
+ def initialize(number)
19
+ @number = number.to_s
20
+ end
21
+
22
+ def valid?
23
+ (verify_luhn && strip_number.length >= 11 && card_type.present?) || is_test_number?
24
+ end
25
+
26
+ def card_type
27
+ CARD_TYPES.select{ |k, v| (strip_number =~ v).present? }.keys[0].to_s
28
+ end
29
+
30
+ def is_test_number?
31
+ ValidCreditCard.test_numbers.include?(strip_number)
32
+ end
33
+
34
+ def verify_luhn
35
+ total = strip_number.reverse().split(//).inject([0,0]) do |accum, n|
36
+ n = n.to_i
37
+ accum[0] += (accum[1] % 2 == 1 ? rotate(n * 2) : n)
38
+ accum[1] += 1
39
+ accum
40
+ end
41
+
42
+ (total[0] % 10 == 0)
43
+ end
44
+
45
+ CARD_TYPES.each do |k, v|
46
+ define_method "is_#{k.to_s}?" do
47
+ (strip_number =~ v).present?
48
+ end
49
+ end
50
+
51
+ protected
52
+
53
+ def strip_number
54
+ number.gsub(/\s/,'')
55
+ end
56
+
57
+ def rotate(n)
58
+ n > 9 ? (n % 10 + 1) : n
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,26 @@
1
+ require "valid_credit_card/credit_card"
2
+ require "active_model"
3
+ require "active_model/validations"
4
+
5
+ class CreditCardValidator < ActiveModel::EachValidator
6
+
7
+ def default_options
8
+ { card_types: ValidCreditCard::CreditCard::CARD_TYPES.keys.map(&:to_s) }
9
+ end
10
+
11
+ def validate_each(record, attribute, value)
12
+ return unless value.present?
13
+
14
+ options = default_options.merge(self.options)
15
+
16
+ cc = ValidCreditCard::CreditCard.new(value)
17
+
18
+ error(record, attribute) && return unless cc.valid?
19
+
20
+ error(record, attribute) && return unless options[:card_types].include?(cc.card_type)
21
+ end
22
+
23
+ def error(record, attribute)
24
+ record.errors.add(attribute, options[:message] || :invalid)
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module ValidCreditCard
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'valid_credit_card/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "valid_credit_card"
8
+ spec.version = ValidCreditCard::VERSION
9
+ spec.authors = ["Pablo Moreira Mora"]
10
+ spec.email = ["pablo.moreiramora@gmail.com"]
11
+
12
+ spec.summary = %q{ActiveModel validation for credit card number.}
13
+ spec.description = %q{ActiveModel validation for credit card number.}
14
+ spec.homepage = "https://github.com/pablodv/valid_credit_card"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.required_ruby_version = ">= 1.9.3"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.10"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec"
28
+
29
+ spec.add_runtime_dependency "activemodel", ">= 3.2"
30
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: valid_credit_card
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Pablo Moreira Mora
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
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: activemodel
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '3.2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '3.2'
69
+ description: ActiveModel validation for credit card number.
70
+ email:
71
+ - pablo.moreiramora@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".rvmrc"
79
+ - ".travis.yml"
80
+ - CODE_OF_CONDUCT.md
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - lib/valid_credit_card.rb
88
+ - lib/valid_credit_card/credit_card.rb
89
+ - lib/valid_credit_card/credit_card_validator.rb
90
+ - lib/valid_credit_card/version.rb
91
+ - valid_credit_card.gemspec
92
+ homepage: https://github.com/pablodv/valid_credit_card
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: 1.9.3
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.4.8
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: ActiveModel validation for credit card number.
116
+ test_files: []