luhnacy 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +6 -1
- data/VERSION +1 -1
- data/lib/luhnacy.rb +15 -14
- data/luhnacy.gemspec +2 -2
- metadata +4 -4
data/README.rdoc
CHANGED
@@ -30,6 +30,8 @@ To generate a string of 10 digits that satisfies Luhn and starts with predefined
|
|
30
30
|
|
31
31
|
There are named validators and generators for Visa, Mastercard and American Express credit cards. Note: Visa validation/generation only works for 16-digit credit card numbers.
|
32
32
|
|
33
|
+
Also, there is also a named generator for the National Provider Identifier (thanks to Taylor Yelverton) used in the US to identify health care providers - see example usage section.
|
34
|
+
|
33
35
|
Example usage (mastercard)
|
34
36
|
|
35
37
|
To validate a mastercard:
|
@@ -44,7 +46,7 @@ To generate a master card number that does not satisfy Luhn:
|
|
44
46
|
|
45
47
|
Luhnacy.mastercard(:invalid => true)
|
46
48
|
|
47
|
-
Similar usage exists for Visa
|
49
|
+
Similar usage exists for Visa, American Express and NPI:
|
48
50
|
|
49
51
|
Luhnacy.visa
|
50
52
|
Luhnacy.visa?
|
@@ -52,6 +54,9 @@ Similar usage exists for Visa and American Express:
|
|
52
54
|
Luhnacy.amex
|
53
55
|
Luhnacy.amex?
|
54
56
|
|
57
|
+
Luhnacy.doctor_npi
|
58
|
+
Luhnacy.doctor_npi?
|
59
|
+
|
55
60
|
== Planned changes
|
56
61
|
|
57
62
|
At some point it may make sense to store the patterns for the named generator in a seperate class/module or even in a YAML file. This will be driven by the need for additional named generators. If a YAML file is used, then the very next release may also include the ability to pass Luhnacy a custom YAML file for generating/validating strings.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.1
|
data/lib/luhnacy.rb
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
class Luhnacy
|
2
|
-
@
|
2
|
+
@named = {
|
3
3
|
:mastercard => { :prefixes => [ '51', '52', '53', '54', '55' ], :size => 16 },
|
4
4
|
:visa => { :prefixes => ['4'], :size => 16 },
|
5
|
-
:amex => { :prefixes => ['34','37'], :size => 15 }
|
5
|
+
:amex => { :prefixes => ['34','37'], :size => 15 },
|
6
|
+
:doctor_npi => { :prefixes => ['80840'], :size => 15 }
|
6
7
|
}
|
7
8
|
def self.valid?(candidate)
|
8
9
|
calc_modulus(candidate) == 0
|
@@ -36,11 +37,11 @@ class Luhnacy
|
|
36
37
|
end
|
37
38
|
|
38
39
|
def self.method_missing(type, *args)
|
39
|
-
|
40
|
+
pattern_name, method_type = parse_method_name(type)
|
40
41
|
|
41
|
-
raise NoMethodError unless @
|
42
|
+
raise NoMethodError unless @named[pattern_name] && @named[pattern_name][:prefixes] && @named[pattern_name][:size]
|
42
43
|
|
43
|
-
send(method_type, *(args.unshift(
|
44
|
+
send(method_type, *(args.unshift(pattern_name)))
|
44
45
|
end
|
45
46
|
|
46
47
|
private
|
@@ -66,23 +67,23 @@ class Luhnacy
|
|
66
67
|
method_name = method_name.to_s
|
67
68
|
case method_name
|
68
69
|
when /(.*)\?$/
|
69
|
-
[ $~[1].to_sym, :
|
70
|
+
[ $~[1].to_sym, :valid_pattern? ]
|
70
71
|
else
|
71
|
-
[ method_name.to_sym, :
|
72
|
+
[ method_name.to_sym, :generate_pattern ]
|
72
73
|
end
|
73
74
|
end
|
74
75
|
|
75
|
-
def self.
|
76
|
-
valid?(
|
76
|
+
def self.valid_pattern?(pattern_name, pattern_candidate)
|
77
|
+
valid?(pattern_candidate) && valid_format?(pattern_name, pattern_candidate)
|
77
78
|
end
|
78
79
|
|
79
|
-
def self.
|
80
|
-
generate(@
|
80
|
+
def self.generate_pattern(pattern_name, options={})
|
81
|
+
generate(@named[pattern_name][:size], options.merge({:prefix => @named[pattern_name][:prefixes][rand(@named[pattern_name][:prefixes].size)]}))
|
81
82
|
end
|
82
83
|
|
83
|
-
def self.
|
84
|
-
@
|
85
|
-
return true if
|
84
|
+
def self.valid_format?(pattern_name, pattern_candidate)
|
85
|
+
@named[pattern_name][:prefixes].each do |prefix|
|
86
|
+
return true if pattern_candidate =~ /^#{prefix}\d{#{@named[pattern_name][:size] - prefix.size}}$/
|
86
87
|
end
|
87
88
|
false
|
88
89
|
end
|
data/luhnacy.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{luhnacy}
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Rory McKinley"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-10-24}
|
13
13
|
s.description = %q{luhnacy can be used to validate strings for Luhn compliance as well as generating valid or invalid strings for the purposes of testing }
|
14
14
|
s.email = %q{rorymckinley@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: luhnacy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 1
|
10
|
+
version: 0.2.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Rory McKinley
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-10-24 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|