chinese_id_card 0.0.1

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
+ SHA256:
3
+ metadata.gz: a35ae67d25b434919c9ec233f732994869d2092cb7981163d71f6420a806577f
4
+ data.tar.gz: 4ba17c4c8878497f48ef0bff77340fd4996cd0d26e08d7c755b658971ce4b876
5
+ SHA512:
6
+ metadata.gz: d0b3b9839c35cfe18ac7d81cb96c5e8bc7361d87d7119437785a7c9c137a960ca1594450258fb496c6cdcac89a6bcb9c109b4fdba5ecec30a57a8d68612fdc42
7
+ data.tar.gz: ccab0ec1e0ca4263c0e8676b690b38530650f68cc7a557935aafbbe941950cc4a972638c17bf4aee60565c962f819c86e80d848d5dccb98ebc2c2d46fd29f815
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-05-05
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders 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, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at ianlynxk@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in chinese_id_card.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
data/Gemfile.lock ADDED
@@ -0,0 +1,55 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ chinese_id_card (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ diff-lcs (1.5.0)
11
+ parallel (1.22.1)
12
+ parser (3.1.2.0)
13
+ ast (~> 2.4.1)
14
+ rainbow (3.1.1)
15
+ rake (13.0.6)
16
+ regexp_parser (2.3.1)
17
+ rexml (3.2.5)
18
+ rspec (3.11.0)
19
+ rspec-core (~> 3.11.0)
20
+ rspec-expectations (~> 3.11.0)
21
+ rspec-mocks (~> 3.11.0)
22
+ rspec-core (3.11.0)
23
+ rspec-support (~> 3.11.0)
24
+ rspec-expectations (3.11.0)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.11.0)
27
+ rspec-mocks (3.11.1)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.11.0)
30
+ rspec-support (3.11.0)
31
+ rubocop (1.28.2)
32
+ parallel (~> 1.10)
33
+ parser (>= 3.1.0.0)
34
+ rainbow (>= 2.2.2, < 4.0)
35
+ regexp_parser (>= 1.8, < 3.0)
36
+ rexml
37
+ rubocop-ast (>= 1.17.0, < 2.0)
38
+ ruby-progressbar (~> 1.7)
39
+ unicode-display_width (>= 1.4.0, < 3.0)
40
+ rubocop-ast (1.17.0)
41
+ parser (>= 3.1.1.0)
42
+ ruby-progressbar (1.11.0)
43
+ unicode-display_width (2.1.0)
44
+
45
+ PLATFORMS
46
+ x86_64-linux
47
+
48
+ DEPENDENCIES
49
+ chinese_id_card!
50
+ rake (~> 13.0)
51
+ rspec (~> 3.0)
52
+ rubocop (~> 1.21)
53
+
54
+ BUNDLED WITH
55
+ 2.3.7
data/README-CN.md ADDED
@@ -0,0 +1,62 @@
1
+ # ChineseIdCard
2
+ 一个解析和校验身份证信息的 Gem,支持长度为 15 和 18 位的身份证号。
3
+
4
+ ## 安装
5
+
6
+ 在 Gemfile 里边添加:
7
+
8
+ ```ruby
9
+ gem 'chinese_id_card'
10
+ ```
11
+
12
+ 然后执行:
13
+
14
+ $ bundle install
15
+
16
+ ## Usage
17
+
18
+ ### Parse user info
19
+ ```ruby
20
+ valid_card_no = "37142819800508053x"
21
+ card = ChineseIdCard::Card.new(valid_card_no).parse!
22
+
23
+ p card.age # => 41
24
+ p card.gender # => "male"
25
+ p card.birth_date # => #<Date: 1980-05-08 ((2444368j,0s,0n),+0s,2299161j)>
26
+
27
+ card_no_with_invalid_length = "37142819800508053x123"
28
+ ChineseIdCard::Card.new(card_no_with_invalid_length).parse! # => raise ChineseIdCard::CardNoLengthInvalidError
29
+
30
+ card_no_with_invalid_format = "a7142819800508053x"
31
+ ChineseIdCard::Card.new(card_no_with_invalid_format).parse! # => raise ChineseIdCard::CardNoFormatInvalidError
32
+ ```
33
+
34
+ ### Validate card no format
35
+ ```ruby
36
+ valid_card_no = "37142819800508053x"
37
+ card = ChineseIdCard::Card.new(valid_card_no)
38
+ p card.valid? # => true
39
+
40
+ card_no_with_invalid_length = "37142819800508053x123"
41
+ card = ChineseIdCard::Card.new(card_no_with_invalid_length)
42
+ p card.valid? # => false
43
+
44
+ card_no_with_invalid_format = "a7142819800508053x"
45
+ card = ChineseIdCard::Card.new(card_no_with_invalid_format)
46
+ p card.valid? # => false
47
+ ```
48
+
49
+
50
+ ## Development
51
+
52
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
53
+
54
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
55
+
56
+ ## Contributing
57
+
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/chinese_id_card. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/chinese_id_card/blob/master/CODE_OF_CONDUCT.md).
59
+
60
+ ## Code of Conduct
61
+
62
+ Everyone interacting in the ChineseIdCard project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/chinese_id_card/blob/master/CODE_OF_CONDUCT.md).
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # ChineseIdCard
2
+ A Gem for parse and validate personal info in id card no of chinese mainland. It supports 15 and 18 characters long card no.
3
+
4
+ [中文文档 Chinese document](/README-CN.md)
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'chinese_id_card'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle install
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install chinese_id_card
21
+
22
+ ## Usage
23
+
24
+ ### Parse user info
25
+ ```ruby
26
+ valid_card_no = "37142819800508053x"
27
+ card = ChineseIdCard::Card.new(valid_card_no).parse!
28
+
29
+ p card.age # => 41
30
+ p card.gender # => "male" (The value of gender can be "male" or "female")
31
+ p card.birth_date # => #<Date: 1980-05-08 ((2444368j,0s,0n),+0s,2299161j)>
32
+
33
+ card_no_with_invalid_length = "37142819800508053x123"
34
+ ChineseIdCard::Card.new(card_no_with_invalid_length).parse! # => raise ChineseIdCard::CardNoLengthInvalidError
35
+
36
+ card_no_with_invalid_format = "a7142819800508053x"
37
+ ChineseIdCard::Card.new(card_no_with_invalid_format).parse! # => raise ChineseIdCard::CardNoFormatInvalidError
38
+ ```
39
+
40
+ ### Validate card no format
41
+ ```ruby
42
+ valid_card_no = "37142819800508053x"
43
+ card = ChineseIdCard::Card.new(valid_card_no)
44
+ p card.valid? # => true
45
+
46
+ card_no_with_invalid_length = "37142819800508053x123"
47
+ card = ChineseIdCard::Card.new(card_no_with_invalid_length)
48
+ p card.valid? # => false
49
+
50
+ card_no_with_invalid_format = "a7142819800508053x"
51
+ card = ChineseIdCard::Card.new(card_no_with_invalid_format)
52
+ p card.valid? # => false
53
+ ```
54
+
55
+
56
+ ## Development
57
+
58
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
59
+
60
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
61
+
62
+ ## Contributing
63
+
64
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/chinese_id_card. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/chinese_id_card/blob/master/CODE_OF_CONDUCT.md).
65
+
66
+ ## Code of Conduct
67
+
68
+ Everyone interacting in the ChineseIdCard project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/chinese_id_card/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/chinese_id_card/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "chinese_id_card"
7
+ spec.version = ChineseIdCard::VERSION
8
+ spec.authors = ["ian"]
9
+ spec.email = ["ianlynxk@gmail.com"]
10
+
11
+ spec.summary = "A gem for parse personal info from chinese mainland id card no"
12
+ spec.description = spec.summary
13
+ spec.homepage = "https://github.com/otorain/chinese_id_card"
14
+ spec.required_ruby_version = ">= 2.6.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = spec.homepage
18
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/CHANGELOG.md"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
25
+ end
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ # Uncomment to register a new dependency of your gem
32
+ # spec.add_dependency "example-gem", "~> 1.0"
33
+
34
+ # For more information and examples about making a new gem, check out our
35
+ # guide at: https://bundler.io/guides/creating_gem.html
36
+ end
@@ -0,0 +1,90 @@
1
+ require "time"
2
+ require "date"
3
+
4
+ module ChineseIdCard
5
+ class Card
6
+ attr_accessor :card_no
7
+ attr_reader :birth_date, :gender, :age
8
+
9
+ def initialize(card_no)
10
+ @card_no = card_no
11
+ end
12
+
13
+ def parse!
14
+ validate!
15
+
16
+ if card_no.size == 18
17
+ parse_length_18(card_no)
18
+ else
19
+ parse_length_15(card_no)
20
+ end
21
+ end
22
+
23
+ def valid?
24
+ length_valid? && format_valid?
25
+ end
26
+
27
+ private
28
+
29
+ def validate!
30
+ length_valid! && format_valid!
31
+ end
32
+
33
+ def parse_length_18(card_no)
34
+ @birth_date = Date.parse(card_no[6...14])
35
+ @age = calculate_age(birth_date)
36
+ @gender = card_no[-2].to_i.odd? ? "male" : "female"
37
+
38
+ self
39
+ end
40
+
41
+ def parse_length_15(card_no)
42
+ @birth_date = Date.parse("19#{card_no[6...12]}")
43
+ @age = calculate_age(birth_date)
44
+ @gender = card_no[-1].to_i.odd? ? "male" : "female"
45
+
46
+ self
47
+ end
48
+
49
+ def calculate_age(birth_date)
50
+ ((current - birth_date.to_time) / seconds_of_one_year).floor
51
+ end
52
+
53
+ def current
54
+ if Time.respond_to? :current
55
+ Time.current
56
+ else
57
+ Time.now
58
+ end
59
+ end
60
+
61
+ def seconds_of_one_year
62
+ 31556952
63
+ end
64
+
65
+ def length_valid?
66
+ @card_no.size == 18 || @card_no.size == 15
67
+ end
68
+
69
+ def length_valid!
70
+ if length_valid?
71
+ true
72
+ else
73
+ raise CardNoLengthInvalidError, "Id card no length is invalid, expect 18 or 15, actual #{card_no.size}"
74
+ end
75
+ end
76
+
77
+ def format_valid?
78
+ regex = /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)/
79
+ !!regex.match(@card_no)
80
+ end
81
+
82
+ def format_valid!
83
+ if format_valid?
84
+ true
85
+ else
86
+ raise CardNoFormatInvalidError, "Id card no format is invalid"
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,4 @@
1
+
2
+ module ChineseIdCard
3
+ class CardNoFormatInvalidError < StandardError; end
4
+ end
@@ -0,0 +1,4 @@
1
+
2
+ module ChineseIdCard
3
+ class CardNoLengthInvalidError < StandardError; end
4
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChineseIdCard
4
+ VERSION = "0.0.1"
5
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "chinese_id_card/version"
4
+ require_relative 'chinese_id_card/card'
5
+ require_relative 'chinese_id_card/card_no_length_invalid_error'
6
+ require_relative 'chinese_id_card/card_no_format_invalid_error'
7
+
8
+ module ChineseIdCard
9
+ class Error < StandardError; end
10
+ end
@@ -0,0 +1,4 @@
1
+ module ChineseIdCard
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chinese_id_card
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - ian
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-05-05 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A gem for parse personal info from chinese mainland id card no
14
+ email:
15
+ - ianlynxk@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - CHANGELOG.md
23
+ - CODE_OF_CONDUCT.md
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - README-CN.md
27
+ - README.md
28
+ - Rakefile
29
+ - chinese_id_card.gemspec
30
+ - lib/chinese_id_card.rb
31
+ - lib/chinese_id_card/card.rb
32
+ - lib/chinese_id_card/card_no_format_invalid_error.rb
33
+ - lib/chinese_id_card/card_no_length_invalid_error.rb
34
+ - lib/chinese_id_card/version.rb
35
+ - sig/chinese_id_card.rbs
36
+ homepage: https://github.com/otorain/chinese_id_card
37
+ licenses: []
38
+ metadata:
39
+ homepage_uri: https://github.com/otorain/chinese_id_card
40
+ source_code_uri: https://github.com/otorain/chinese_id_card
41
+ changelog_uri: https://github.com/otorain/chinese_id_card/CHANGELOG.md
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 2.6.0
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubygems_version: 3.3.7
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: A gem for parse personal info from chinese mainland id card no
61
+ test_files: []