credit_card_validator 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -42,6 +42,63 @@ You can also use the validator to learn about the type of the card:
42
42
 
43
43
  CreditCardValidator::Validator.is_allowed_card_type?(number)
44
44
 
45
+ == Command Line:
46
+
47
+ The gem comes with a basic command line application that allows you to do validation in a variety of ways. The utility produces tab delimited output for easy processing by other utilities.
48
+
49
+ $ credit_card_validator --help
50
+ Usage: credit_card_validator [options]
51
+ -v, --verboseverbose Be more verbose.
52
+ -h, --help Request help.
53
+ -a=s Comma Delimited List of allowed card types (default is 'amex,diners_club,discover,maestro,master_card,visa')
54
+ --allowed-card-types
55
+ -t
56
+ --test-numbers-are-validtest-numbers-are-valid
57
+ Allow test Credit Card Numbers to be considred valid.
58
+
59
+ You can validate a number directly from the command line:
60
+
61
+ $ credit_card_validator 5105105105105100
62
+ card_number valid?
63
+ 5105105105105100 false
64
+
65
+
66
+ Verbose mode will emit more detailed information:
67
+
68
+ $ credit_card_validator -v 5105105105105100
69
+ credit_card_validator Parameters:
70
+ verbose : true
71
+ help :
72
+ allowed-card-types : amex,diners_club,discover,maestro,master_card,visa
73
+ test-numbers-are-valid :
74
+
75
+ card_number is_allowed_card_type? luhn_valid? card_type is_test_number? valid?
76
+ 5105105105105100 true true master_card true false
77
+
78
+ The option summary is printed to $stderr and is easy to suppress:
79
+
80
+ $ credit_card_validator -v 5105105105105100 2>/dev/null
81
+ card_number is_allowed_card_type? luhn_valid? card_type is_test_number? valid?
82
+ 5105105105105100 true true master_card true false
83
+
84
+ You may also validate a file full of numbers (one per line):
85
+
86
+ $ cat nums.tab
87
+ 5105105105105100
88
+ 4012888888881882
89
+ $ credit_card_validator -v nums.tab 2>/dev/null
90
+ card_number is_allowed_card_type? luhn_valid? card_type is_test_number? valid?
91
+ 5105105105105100 true true master_card true false
92
+ 4012888888881882 true false visa false false
93
+
94
+ Even from standard input:
95
+
96
+ $ cat nums.tab | credit_card_validator -v 2>/dev/null
97
+ card_number is_allowed_card_type? luhn_valid? card_type is_test_number? valid?
98
+ 5105105105105100 true true master_card true false
99
+ 4012888888881882 true false visa false false
100
+
101
+
45
102
  == INSTALL:
46
103
 
47
104
  * gem install credit_card_validator
@@ -0,0 +1,89 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'base_app'
4
+ require 'credit_card_validator'
5
+
6
+ class CreditCardValidatorApp < BaseApp
7
+ attr_accessor :allowed_card_types
8
+
9
+ def initialize
10
+ self.allowed_card_types = CreditCardValidator::CARD_TYPES.keys.sort {|a,b| a.to_s <=> b.to_s }.map(&:to_s).join(",")
11
+ super
12
+ end
13
+
14
+ def command_line_arguments
15
+ super | [
16
+ ['a', 'allowed-card-types=s', "Comma Delimited List of allowed card types (default is '#{self.allowed_card_types}')"],
17
+ ['t', 'test-numbers-are-valid', "Allow test Credit Card Numbers to be considred valid."]
18
+ ]
19
+ end
20
+
21
+ def card_info card_num
22
+ is_valid = CreditCardValidator::Validator::valid?(card_num)
23
+ is_valid = false if is_valid.nil?
24
+ { :is_allowed_card_type? => CreditCardValidator::Validator::is_allowed_card_type?(card_num),
25
+ :luhn_valid? => CreditCardValidator::Validator::verify_luhn(card_num),
26
+ :card_type => CreditCardValidator::Validator::card_type(card_num),
27
+ :is_test_number? => CreditCardValidator::Validator::is_test_number(card_num),
28
+ :valid? => is_valid }
29
+ end
30
+
31
+ def validate_card card_num
32
+ res = card_info card_num
33
+ all_valid = false unless res[:valid?]
34
+ if self.verbose
35
+ puts [
36
+ card_num,
37
+ res[:is_allowed_card_type?],
38
+ res[:luhn_valid?],
39
+ res[:card_type],
40
+ res[:is_test_number?],
41
+ res[:valid?],
42
+ ].join("\t")
43
+ else
44
+ puts [card_num,res[:valid?]].join("\t")
45
+ end
46
+ end
47
+
48
+ def validate_file fhandle
49
+ fhandle.readlines.each do |line|
50
+ line.chomp!
51
+ validate_card line
52
+ end
53
+ end
54
+
55
+ def run
56
+ CreditCardValidator::Validator.options[:test_numbers_are_valid] = self.test_numbers_are_valid
57
+ CreditCardValidator::Validator.options[:allowed_card_types] = self.allowed_card_types.split(',').map(&:to_sym)
58
+
59
+ if self.verbose
60
+ puts %w[card_number is_allowed_card_type? luhn_valid? card_type is_test_number? valid?].join("\t")
61
+ else
62
+ puts %w[card_number valid?].join("\t")
63
+ end
64
+
65
+ all_valid = true
66
+ if $stdin.tty?
67
+ if ARGV.empty?
68
+ $stderr.puts "Error: nothing to validate"
69
+ exit 1
70
+ end
71
+
72
+ ARGV.each do |thing|
73
+ if File.exist? thing
74
+ File.open(thing,'r') do |f|
75
+ validate_file f
76
+ end
77
+ else
78
+ validate_card thing
79
+ end
80
+ end
81
+ else
82
+ validate_file $stdin
83
+ end
84
+
85
+ exit all_valid ? 0 : 1
86
+ end
87
+ end
88
+
89
+ CreditCardValidatorApp.main
@@ -46,6 +46,7 @@ module CreditCardValidator
46
46
 
47
47
  def is_allowed_card_type?(number)
48
48
  card_type = card_type(number)
49
+ return nil if card_type.nil?
49
50
  if options[:allowed_card_types] and options[:allowed_card_types].respond_to?('include?')
50
51
  options[:allowed_card_types].include?(card_type.to_sym)
51
52
  else
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: credit_card_validator
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 0
9
8
  - 1
10
- version: 1.0.1
9
+ - 0
10
+ version: 1.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tobias Crawley
@@ -15,6 +15,7 @@ authors:
15
15
  - Phil McClure
16
16
  - Gabriel Reis
17
17
  - Eric Young
18
+ - Kyle Burton <kyle.burton@gmail.com>
18
19
  autorequire:
19
20
  bindir: bin
20
21
  cert_chain: []
@@ -53,11 +54,27 @@ dependencies:
53
54
  version: 1.8.0
54
55
  type: :development
55
56
  version_requirements: *id002
57
+ - !ruby/object:Gem::Dependency
58
+ name: base_app
59
+ prerelease: false
60
+ requirement: &id003 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ hash: 29
66
+ segments:
67
+ - 1
68
+ - 0
69
+ - 5
70
+ version: 1.0.5
71
+ type: :development
72
+ version_requirements: *id003
56
73
  description: A gem that provides credit card validation. It is basically a ruby port of the javascript credit card validator by Thomas Fuchs (madrobby) (http://github.com/madrobby/creditcard_js).
57
74
  email:
58
75
  - tcrawley@gmail.com
59
- executables: []
60
-
76
+ executables:
77
+ - credit_card_validator
61
78
  extensions: []
62
79
 
63
80
  extra_rdoc_files:
@@ -73,6 +90,7 @@ files:
73
90
  - lib/credit_card_validator/validator.rb
74
91
  - test/test_credit_card_validator.rb
75
92
  - test/test_helper.rb
93
+ - bin/credit_card_validator
76
94
  homepage: http://github.com/tobias/credit_card_validator
77
95
  licenses: []
78
96
 
@@ -81,6 +99,7 @@ rdoc_options:
81
99
  - --main
82
100
  - README.rdoc
83
101
  require_paths:
102
+ - bin
84
103
  - lib
85
104
  required_ruby_version: !ruby/object:Gem::Requirement
86
105
  none: false