dixon 0.0.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: fd4287564dded1221c414b3d82f74530a455bc32
4
+ data.tar.gz: 354729bdd37756951627dda36db658f57e07c009
5
+ SHA512:
6
+ metadata.gz: 4fac674805c6df5e41ffdf367c56568c4abe746ebde49ae664f0c132e52434f7f21c73f57f68eb5d9ee29f51008622c8695c0599ba574a71a995813b02a3c5af
7
+ data.tar.gz: 766eabfa151d89352b72ba8cca0f0ee75953cc95e01021e0abfda90225353d977a54eb60b51148b162972610092b540d0a79e0a01ee1b00d8d131f97269bb37b
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .pryrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dixon.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Juanito Fatas
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,54 @@
1
+ # Dixon
2
+
3
+ [Frank] Dixon checks your ID number.
4
+
5
+ Now only supports identification number for Taiwan, more are coming up.
6
+
7
+ ## Requirements
8
+
9
+ Ruby 2.0+
10
+
11
+ ## Installation
12
+
13
+ $ gem install dixon
14
+
15
+ ## API
16
+
17
+ Provides three instance methods: `checks`, `gender`, and `issued_by`.
18
+
19
+ * `checks` checks if given ID is a valid Taiwan identification number.
20
+
21
+ * `gender` returns gender of given ID.
22
+
23
+ * `issued_by` returns which local agencies issued this ID.
24
+
25
+ ## Usage
26
+
27
+ Dixon::Validators::Taiwan.new.checks 'A123456789'
28
+ => true
29
+
30
+ Dixon::Validators::Taiwan.new.gender 'A123456789'
31
+ => "male"
32
+
33
+ Dixon::Validators::Taiwan.new.issued_by 'A123456789'
34
+ => "Taipei City"
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create new Pull Request
43
+
44
+ ## TODO
45
+
46
+ * add tests.
47
+
48
+ ## Author
49
+
50
+ Juanito Fatas
51
+
52
+ ## License
53
+
54
+ MIT License
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -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 'dixon/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.platform = Gem::Platform::RUBY
8
+
9
+ spec.name = "dixon"
10
+ spec.version = Dixon::VERSION
11
+
12
+ spec.authors = ["Juanito Fatas"]
13
+ spec.email = ["katehuang0320@gmail.com"]
14
+
15
+ spec.description = %q{Dixon checks your identification number!}
16
+ spec.summary = spec.description
17
+ spec.homepage = "https://github.com/JuanitoFatas/dixon"
18
+
19
+ spec.license = "MIT"
20
+
21
+ spec.files = `git ls-files`.split($/)
22
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
23
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.required_ruby_version = '>= 2.0.0'
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.3"
29
+ spec.add_development_dependency "rake"
30
+ end
@@ -0,0 +1,3 @@
1
+ require 'dixon/version'
2
+ require 'dixon/validators/helpers'
3
+ require 'dixon/validators/taiwan'
@@ -0,0 +1,11 @@
1
+ module Dixon
2
+ module ArrayHelpers
3
+ def element_of_same_size
4
+ self.all? { |a| a.size == self.first.size }
5
+ end
6
+ end
7
+ end
8
+
9
+ class Array
10
+ include Dixon::ArrayHelpers
11
+ end
@@ -0,0 +1,133 @@
1
+ module Dixon
2
+ module Validators
3
+ class Taiwan
4
+
5
+ INVALID_MESSAGE = 'Invalid ID number.'
6
+
7
+ LETTER_ISSUED_BY = {
8
+ # Active Letters
9
+ 'A' => 'Taipei City' , 'B' => 'Taichung City',
10
+ 'C' => 'Keelung City' , 'D' => 'Tainan City',
11
+ 'E' => 'Kaohsiung City' , 'F' => 'New Taipei City',
12
+ 'G' => 'Yilan County' , 'H' => 'Taoyuan County',
13
+ 'I' => 'Chiayi City' , 'J' => 'Hsinchu County',
14
+ 'K' => 'Miaoli County' , 'M' => 'Nantou County',
15
+ 'N' => 'Changhua County' , 'O' => 'Hsinchu City',
16
+ 'P' => 'Yunlin County' , 'Q' => 'Chiayi County',
17
+ 'T' => 'Pingtung County' , 'U' => 'Hualien County',
18
+ 'V' => 'Taitung County' , 'W' => 'Kinmen County',
19
+ 'X' => 'Penghu County' , 'Z' => 'Lienchiang County',
20
+ # Letters no longer issued
21
+ 'L' => 'Taichung County' ,
22
+ 'R' => 'Tainan County' ,
23
+ 'S' => 'Kaohsiung County',
24
+ 'Y' => 'Yangmingshan Management Bureau'
25
+ }
26
+
27
+ LETTER_NUMBERS = {
28
+ 'A' => '10', 'B' => '11', 'C' => '12', 'D' => '13', 'E' => '14',
29
+ 'F' => '15', 'G' => '16', 'H' => '17', 'J' => '18', 'K' => '19',
30
+ 'L' => '20', 'M' => '21', 'N' => '22', 'P' => '23', 'Q' => '24',
31
+ 'R' => '25', 'S' => '26', 'T' => '27', 'U' => '28', 'V' => '29',
32
+ 'X' => '30', 'Y' => '31', 'W' => '32', 'Z' => '33', 'I' => '34',
33
+ 'O' => '35'
34
+ }
35
+
36
+ RULE_NUMBERS = [1, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1]
37
+
38
+ ## Check if a given ID is valid.
39
+ # `id` must be a string, case is insensitive.
40
+ # First convert ID into all numbers, then multiply to RULE_NUMBERS
41
+ # element by element. Sum up and take a 10 modulo then check
42
+ # if remainder is 0. If it's, return true, false otherwise.
43
+ def checks(id)
44
+ # "F127337575" => [1, 5, 1, 2, 7, 3, 3, 7, 5, 7, 5]
45
+ converted = convert id.to_s
46
+ # [1, 5, 1, 2, 7, 3, 3, 7, 5, 7, 5]
47
+ # * [1, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1]
48
+ # --------------------------------------------
49
+ # sum 1, 45, 8, 14, 43, 15, 12, 21, 10, 7, 5
50
+ # => 180
51
+ # 180 mod 10, is the remainder equal to zero? If so, return true.
52
+ #
53
+ return (mapcar(-> (a,b) { a * b }, converted, RULE_NUMBERS).reduce(:+).divmod 10)[1] == 0
54
+ end
55
+
56
+ ## Returns gender for given ID.
57
+ # By check the second digit of ID.
58
+ # 1 is male, 2 is female.
59
+ def gender(id)
60
+ case id[1]
61
+ when '1' then 'male'
62
+ when '2' then 'female'
63
+ else puts INVALID_MESSAGE
64
+ end
65
+ end
66
+
67
+ ## Returns local agencies who issued your id.
68
+ def issued_by(id)
69
+ LETTER_ISSUED_BY[id[0]]
70
+ end
71
+
72
+ private
73
+
74
+ ## Convert array of strings into integer array.
75
+ def to_i_array(str_arr)
76
+ str_arr.map(&:to_i)
77
+ end
78
+
79
+ ## Convert an identification number to array of strings
80
+ # First letter conversion rules is defined in LETTER_NUMBERS
81
+ # `convert 'F127337575'` will result in
82
+ # [1, 5, 1, 2, 7, 3, 3, 7, 5, 7, 5]
83
+ def convert(id)
84
+ to_i_array((LETTER_NUMBERS[id[0]] + id[1..-1]).split(''))
85
+ end
86
+
87
+ ## mapcar operates on successive elements of the arrays.
88
+ # fn is applied to the first element of each array, then to the second element of each array, and so on. The iteration terminates when the shortest array runs out, and excess elements in other arrays are ignored. The value returned by mapcar is a array of the results of successive calls to function
89
+ def mapcar(fn, *arrs)
90
+ return [] if arrs.empty? or arrs.include? []
91
+ transposed = if arrs.element_of_same_size
92
+ arrs.transpose
93
+ else
94
+ arrs[0].zip(*arrs.drop(1))
95
+ end
96
+ transposed.map do |e|
97
+ e.reduce(&fn) unless e.include? nil
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
103
+
104
+ module ValidateFormat
105
+
106
+ TAIWAN_ID_REGEXP = /[a-zA-Z][1|2][0-9]+/i
107
+
108
+ ## Check if given id's format is valid.
109
+ # The current ID number has exact 10 digits
110
+ # The first digit is one capital English letter and
111
+ # is followed by nine Arabic numerals.
112
+ def valid_format?(id)
113
+ return true if id =~ TAIWAN_ID_REGEXP and id.size == 10
114
+ puts 'Invalid ID number.'
115
+ false
116
+ end
117
+
118
+ %i(checks gender issued_by convert).each do |m|
119
+ define_method(m) { |id| super(id) if valid_format? id }
120
+ end
121
+
122
+ private :convert
123
+
124
+ end
125
+
126
+ module Dixon
127
+ module Validators
128
+ class Taiwan
129
+ prepend ValidateFormat
130
+ end
131
+ end
132
+ end
133
+
@@ -0,0 +1,3 @@
1
+ module Dixon
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dixon
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Juanito Fatas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-09 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
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
+ description: Dixon checks your identification number!
42
+ email:
43
+ - katehuang0320@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE
51
+ - README.md
52
+ - Rakefile
53
+ - dixon.gemspec
54
+ - lib/dixon.rb
55
+ - lib/dixon/validators/helpers.rb
56
+ - lib/dixon/validators/taiwan.rb
57
+ - lib/dixon/version.rb
58
+ homepage: https://github.com/JuanitoFatas/dixon
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - '>='
69
+ - !ruby/object:Gem::Version
70
+ version: 2.0.0
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.0.6
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Dixon checks your identification number!
82
+ test_files: []
83
+ has_rdoc: