dixon 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/README.md +1 -5
- data/Rakefile +7 -0
- data/dixon.gemspec +3 -0
- data/lib/dixon.rb +1 -1
- data/lib/dixon/validators/{helpers.rb → helper.rb} +0 -0
- data/lib/dixon/validators/taiwan.rb +44 -14
- data/lib/dixon/version.rb +1 -1
- data/test/test_helper.rb +5 -0
- data/test/validators/taiwan_test.rb +34 -0
- metadata +22 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a4aa8e4b5a9dc6293366fae03a4c2fb867859edf
|
4
|
+
data.tar.gz: e624ff4bbf6c43dbf331641e1786dd5816600524
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef071bc29ab77ef85174416f105e97bd7d7629f165bba2f33114605f3eaf5696a552d9019df25a132f20f7f167044b9eecb924591571702e8f35accca8ad1397
|
7
|
+
data.tar.gz: 628aa11550933d7e0df1efde482639811b7028df5259765441928397c116659a7625c805dfba9c6839771765632c36f900c64a1793cf2a1237127fab804d691e
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Dixon
|
1
|
+
# Dixon [](http://badge.fury.io/rb/dixon)
|
2
2
|
|
3
3
|
[Frank] Dixon checks your ID number.
|
4
4
|
|
@@ -41,10 +41,6 @@ Provides three instance methods: `checks`, `gender`, and `issued_by`.
|
|
41
41
|
4. Push to the branch (`git push origin my-new-feature`)
|
42
42
|
5. Create new Pull Request
|
43
43
|
|
44
|
-
## TODO
|
45
|
-
|
46
|
-
* add tests.
|
47
|
-
|
48
44
|
## Author
|
49
45
|
|
50
46
|
Juanito Fatas
|
data/Rakefile
CHANGED
data/dixon.gemspec
CHANGED
@@ -25,6 +25,9 @@ Gem::Specification.new do |spec|
|
|
25
25
|
|
26
26
|
spec.required_ruby_version = '>= 2.0.0'
|
27
27
|
|
28
|
+
# spec.add_dependency "add-your-runtime-dependency"
|
29
|
+
|
28
30
|
spec.add_development_dependency "bundler", "~> 1.3"
|
29
31
|
spec.add_development_dependency "rake"
|
32
|
+
spec.add_development_dependency "minitest"
|
30
33
|
end
|
data/lib/dixon.rb
CHANGED
File without changes
|
@@ -5,6 +5,7 @@ module Dixon
|
|
5
5
|
INVALID_MESSAGE = 'Invalid ID number.'
|
6
6
|
|
7
7
|
LETTER_ISSUED_BY = {
|
8
|
+
##
|
8
9
|
# Active Letters
|
9
10
|
'A' => 'Taipei City' , 'B' => 'Taichung City',
|
10
11
|
'C' => 'Keelung City' , 'D' => 'Tainan City',
|
@@ -17,6 +18,7 @@ module Dixon
|
|
17
18
|
'T' => 'Pingtung County' , 'U' => 'Hualien County',
|
18
19
|
'V' => 'Taitung County' , 'W' => 'Kinmen County',
|
19
20
|
'X' => 'Penghu County' , 'Z' => 'Lienchiang County',
|
21
|
+
##
|
20
22
|
# Letters no longer issued
|
21
23
|
'L' => 'Taichung County' ,
|
22
24
|
'R' => 'Tainan County' ,
|
@@ -35,7 +37,8 @@ module Dixon
|
|
35
37
|
|
36
38
|
RULE_NUMBERS = [1, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1]
|
37
39
|
|
38
|
-
##
|
40
|
+
##
|
41
|
+
# Check if a given ID is valid.
|
39
42
|
# `id` must be a string, case is insensitive.
|
40
43
|
# First convert ID into all numbers, then multiply to RULE_NUMBERS
|
41
44
|
# element by element. Sum up and take a 10 modulo then check
|
@@ -49,11 +52,11 @@ module Dixon
|
|
49
52
|
# sum 1, 45, 8, 14, 43, 15, 12, 21, 10, 7, 5
|
50
53
|
# => 180
|
51
54
|
# 180 mod 10, is the remainder equal to zero? If so, return true.
|
52
|
-
#
|
53
55
|
return (mapcar(-> (a,b) { a * b }, converted, RULE_NUMBERS).reduce(:+).divmod 10)[1] == 0
|
54
56
|
end
|
55
57
|
|
56
|
-
##
|
58
|
+
##
|
59
|
+
# Returns gender for given ID.
|
57
60
|
# By check the second digit of ID.
|
58
61
|
# 1 is male, 2 is female.
|
59
62
|
def gender(id)
|
@@ -64,19 +67,22 @@ module Dixon
|
|
64
67
|
end
|
65
68
|
end
|
66
69
|
|
67
|
-
##
|
70
|
+
##
|
71
|
+
# Returns local agencies who issued your id.
|
68
72
|
def issued_by(id)
|
69
73
|
LETTER_ISSUED_BY[id[0]]
|
70
74
|
end
|
71
75
|
|
72
76
|
private
|
73
77
|
|
74
|
-
##
|
78
|
+
##
|
79
|
+
# Convert array of strings into integer array.
|
75
80
|
def to_i_array(str_arr)
|
76
81
|
str_arr.map(&:to_i)
|
77
82
|
end
|
78
83
|
|
79
|
-
##
|
84
|
+
##
|
85
|
+
# Convert an identification number to array of strings
|
80
86
|
# First letter conversion rules is defined in LETTER_NUMBERS
|
81
87
|
# `convert 'F127337575'` will result in
|
82
88
|
# [1, 5, 1, 2, 7, 3, 3, 7, 5, 7, 5]
|
@@ -84,8 +90,14 @@ module Dixon
|
|
84
90
|
to_i_array((LETTER_NUMBERS[id[0]] + id[1..-1]).split(''))
|
85
91
|
end
|
86
92
|
|
87
|
-
##
|
88
|
-
#
|
93
|
+
##
|
94
|
+
# mapcar operates on successive elements of the arrays.
|
95
|
+
# fn is applied to the first element of each array,
|
96
|
+
# then to the secondf element of each array, and so on.
|
97
|
+
# The iteration terminates when the shortest array runs out,
|
98
|
+
# and excess elements in other arrays are ignored.
|
99
|
+
# The value returned by mapcar is a array of the results of
|
100
|
+
# successive calls to function.
|
89
101
|
def mapcar(fn, *arrs)
|
90
102
|
return [] if arrs.empty? or arrs.include? []
|
91
103
|
transposed = if arrs.element_of_same_size
|
@@ -103,20 +115,38 @@ end
|
|
103
115
|
|
104
116
|
module ValidateFormat
|
105
117
|
|
118
|
+
##
|
119
|
+
# A123456789
|
106
120
|
TAIWAN_ID_REGEXP = /[a-zA-Z][1|2][0-9]+/i
|
107
121
|
|
108
|
-
##
|
122
|
+
##
|
123
|
+
# Check if given id's format is valid.
|
109
124
|
# The current ID number has exact 10 digits
|
110
125
|
# The first digit is one capital English letter and
|
111
126
|
# is followed by nine Arabic numerals.
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
127
|
+
# returns
|
128
|
+
# 'Empty ID.' for empty string
|
129
|
+
# 'Valid ID.' if the format is correct.
|
130
|
+
# otherwise, 'Invalid ID number.'
|
131
|
+
def validate_id_format(id)
|
132
|
+
id_str = id.to_s
|
133
|
+
return 'Empty ID.' if id_str.empty?
|
134
|
+
return 'Valid ID.' if id_str =~ TAIWAN_ID_REGEXP and id_str.size == 10
|
135
|
+
return Dixon::Validators::Taiwan::INVALID_MESSAGE
|
116
136
|
end
|
117
137
|
|
138
|
+
##
|
139
|
+
# Add a before_hook for methods that
|
140
|
+
# defined in literal symbol array.
|
118
141
|
%i(checks gender issued_by convert).each do |m|
|
119
|
-
define_method(m)
|
142
|
+
define_method(m) do |id|
|
143
|
+
is_it_valid = validate_id_format id
|
144
|
+
if is_it_valid == 'Valid ID.'
|
145
|
+
super(id)
|
146
|
+
else
|
147
|
+
return is_it_valid
|
148
|
+
end
|
149
|
+
end
|
120
150
|
end
|
121
151
|
|
122
152
|
private :convert
|
data/lib/dixon/version.rb
CHANGED
data/test/test_helper.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require_relative '../test_helper'
|
2
|
+
|
3
|
+
class TaiwanTest < Minitest::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@taiwan = Dixon::Validators::Taiwan.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_checks_for_empty_id
|
10
|
+
empty_id = ''
|
11
|
+
assert_equal 'Empty ID.', @taiwan.checks(empty_id)
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_checks_for_bad_format_id
|
15
|
+
bad_format_id = "A12345678"
|
16
|
+
assert_equal 'Invalid ID number.', @taiwan.checks(bad_format_id)
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_checks_for_gender_out_of_range_id
|
20
|
+
gender_out_of_range_id = "A324567945"
|
21
|
+
assert_equal 'Invalid ID number.', @taiwan.checks(gender_out_of_range_id)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_checks_for_invalid_id
|
25
|
+
invalid_id = "A123456788"
|
26
|
+
assert_equal false, @taiwan.checks(invalid_id)
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_checks_for_valid_id
|
30
|
+
valid_id = 'A123456789'
|
31
|
+
assert_equal true, @taiwan.checks(valid_id)
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dixon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juanito Fatas
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-08-
|
11
|
+
date: 2013-08-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
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'
|
41
55
|
description: Dixon checks your identification number!
|
42
56
|
email:
|
43
57
|
- katehuang0320@gmail.com
|
@@ -52,9 +66,11 @@ files:
|
|
52
66
|
- Rakefile
|
53
67
|
- dixon.gemspec
|
54
68
|
- lib/dixon.rb
|
55
|
-
- lib/dixon/validators/
|
69
|
+
- lib/dixon/validators/helper.rb
|
56
70
|
- lib/dixon/validators/taiwan.rb
|
57
71
|
- lib/dixon/version.rb
|
72
|
+
- test/test_helper.rb
|
73
|
+
- test/validators/taiwan_test.rb
|
58
74
|
homepage: https://github.com/JuanitoFatas/dixon
|
59
75
|
licenses:
|
60
76
|
- MIT
|
@@ -79,5 +95,7 @@ rubygems_version: 2.0.6
|
|
79
95
|
signing_key:
|
80
96
|
specification_version: 4
|
81
97
|
summary: Dixon checks your identification number!
|
82
|
-
test_files:
|
98
|
+
test_files:
|
99
|
+
- test/test_helper.rb
|
100
|
+
- test/validators/taiwan_test.rb
|
83
101
|
has_rdoc:
|