swedish-pin 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bb6a746d0ef33cae631f59b11006ee17a98cc38af65b69b35a0f025a22beeae1
4
- data.tar.gz: c9e0ca8008e77aab92808afee2e619bf6a7ed8d9461319449a304128a50237d5
3
+ metadata.gz: 8dd38e3a23539a5d2c44a2f47dca405748640377ff60f4f5f64359ccf78739b2
4
+ data.tar.gz: c03e1cd7d9d34ab5739a5e9e7647621138c3a7cb7e0ef8c9d12cd14d0ae28b57
5
5
  SHA512:
6
- metadata.gz: cd9f5e5328d76e4d709a4d0b8e688b3e13fd186fb57c66eb82a8793215e01231cb1799d41a4e16a75b306d1d37c9cb3d7a2c1cb98a0ae79dec18ea5bc9cf554e
7
- data.tar.gz: 4b7ed29721a100dc98515d181748f47d0c3957d677a19fb93a6ee287837a270dd845ccdaf20ec504735d7e20ad4d03629dc48cdb052d112380fafb3315ced81a
6
+ metadata.gz: 8c44152a32e0f99508dd754ab975e7bf8bb28dc5029c6d37f0f193edf82c7b0a410807c00e9beee2a8377fd3acc295ee4a5b15453dc3f9783151c9d9d0fa9df2
7
+ data.tar.gz: 13f23fd9dac7c68c6dc2227a7ada375f4ed335d1caf3f42496cf947932054e39c5b0a71c5a1183b55d6a13160071aefe0a9c402ef5a91d7bdc96ca52d95ba803
@@ -0,0 +1,33 @@
1
+ # yaml-language-server: $schema=https://github.com/SchemaStore/schemastore/raw/master/src/schemas/json/github-workflow.json
2
+ name: CI
3
+
4
+ on: [push, pull_request]
5
+
6
+ jobs:
7
+ lint:
8
+ runs-on: ubuntu-latest
9
+ steps:
10
+ - uses: actions/checkout@v3
11
+ - uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: "2.7"
14
+ bundler-cache: true
15
+ - run: bundle exec standardrb --no-fix
16
+
17
+ tests:
18
+ strategy:
19
+ fail-fast: false
20
+ matrix:
21
+ os: [ubuntu-latest]
22
+ # Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0'
23
+ ruby: ["2.7", "3.0", "3.1", "3.2"]
24
+
25
+ runs-on: ${{ matrix.os }}
26
+
27
+ steps:
28
+ - uses: actions/checkout@v3
29
+ - uses: ruby/setup-ruby@v1
30
+ with:
31
+ ruby-version: ${{ matrix.ruby }}
32
+ bundler-cache: true
33
+ - run: rake
data/.solargraph.yml ADDED
@@ -0,0 +1,16 @@
1
+ ---
2
+ include:
3
+ - "**/*.rb"
4
+ exclude:
5
+ - spec/**/*
6
+ - test/**/*
7
+ - vendor/**/*
8
+ - ".bundle/**/*"
9
+ require: []
10
+ domains: []
11
+ plugins:
12
+ - solargraph-standardrb
13
+ reporters:
14
+ - standardrb
15
+ require_paths: []
16
+ max_files: 5000
data/CHANGELOG.md CHANGED
@@ -7,9 +7,50 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ Nothing yet.
11
+
12
+ ## [2.0.0] - 2023-01-11
13
+
14
+ ### Breaking changes
15
+
16
+ * Minimum supported Ruby version bumped from 2.5 to 2.7.
17
+
18
+ ### Fixed
19
+
20
+ * PINs with future birthdates are now parsed as long as they are for the
21
+ current year.
22
+ * **Example:** `20xyab-nnnn` used to be parsed as `1920` until `2020-xy-ab`,
23
+ but now it's parsed as `2020` from 2020-01-01 and onwards. This is more
24
+ correct, since all 1920s PINs should be written with a `+` after
25
+ 2020-01-01.
26
+ * Parsing of 10-digit PINs were improved so it is always consistent so
27
+ `parse(pin.to_s(10)) == pin`.
28
+
29
+ ### Removed
30
+
31
+ * Explicit installation of `rake` and `minitest` as development dependencies
32
+ removed. Now relying on the versions bundled with Ruby.
33
+
34
+ ### Other
35
+
36
+ * Travis CI setup changed into Github Actions.
37
+
38
+ ## [1.1.0] - 2022-07-04
39
+
40
+ ### Fixed
41
+
42
+ * `rake test` actually runs tests now.
43
+
44
+ ### Added
45
+
46
+ * `Personnummer` can now be compared and sorted.
47
+ * `Personnummer` works as hash keys by value.
48
+
10
49
  ## [1.0.0] - 2020-09-09
11
50
 
12
51
  Initial release.
13
52
 
14
- [Unreleased]: https://github.com/Mange/swedish-pin-ruby/compare/v1.0.0...HEAD
15
53
  [1.0.0]: https://github.com/Mange/swedish-pin-ruby/releases/tag/v1.0.0
54
+ [1.1.0]: https://github.com/Mange/swedish-pin-ruby/releases/tag/v1.1.0
55
+ [2.0.0]: https://github.com/Mange/swedish-pin-ruby/releases/tag/v2.0.0
56
+ [Unreleased]: https://github.com/Mange/swedish-pin-ruby/compare/v2.0.0...HEAD
data/Gemfile.lock CHANGED
@@ -1,47 +1,48 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- swedish-pin (1.0.0)
4
+ swedish-pin (2.0.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- ast (2.4.1)
10
- minitest (5.14.2)
11
- parallel (1.19.2)
12
- parser (2.7.1.4)
9
+ ast (2.4.2)
10
+ json (2.6.3)
11
+ language_server-protocol (3.17.0.2)
12
+ parallel (1.22.1)
13
+ parser (3.2.0.0)
13
14
  ast (~> 2.4.1)
14
- rainbow (3.0.0)
15
- rake (13.0.1)
16
- regexp_parser (1.7.1)
17
- rexml (3.2.4)
18
- rubocop (0.90.0)
15
+ rainbow (3.1.1)
16
+ regexp_parser (2.6.1)
17
+ rexml (3.2.5)
18
+ rubocop (1.42.0)
19
+ json (~> 2.3)
19
20
  parallel (~> 1.10)
20
- parser (>= 2.7.1.1)
21
+ parser (>= 3.1.2.1)
21
22
  rainbow (>= 2.2.2, < 4.0)
22
- regexp_parser (>= 1.7)
23
- rexml
24
- rubocop-ast (>= 0.3.0, < 1.0)
23
+ regexp_parser (>= 1.8, < 3.0)
24
+ rexml (>= 3.2.5, < 4.0)
25
+ rubocop-ast (>= 1.24.1, < 2.0)
25
26
  ruby-progressbar (~> 1.7)
26
- unicode-display_width (>= 1.4.0, < 2.0)
27
- rubocop-ast (0.3.0)
28
- parser (>= 2.7.1.4)
29
- rubocop-performance (1.8.0)
30
- rubocop (>= 0.87.0)
31
- ruby-progressbar (1.10.1)
32
- standard (0.6.0)
33
- rubocop (~> 0.90)
34
- rubocop-performance (~> 1.8.0)
35
- unicode-display_width (1.7.0)
27
+ unicode-display_width (>= 1.4.0, < 3.0)
28
+ rubocop-ast (1.24.1)
29
+ parser (>= 3.1.1.0)
30
+ rubocop-performance (1.15.2)
31
+ rubocop (>= 1.7.0, < 2.0)
32
+ rubocop-ast (>= 0.4.0)
33
+ ruby-progressbar (1.11.0)
34
+ standard (1.21.1)
35
+ language_server-protocol (~> 3.17.0.2)
36
+ rubocop (= 1.42.0)
37
+ rubocop-performance (= 1.15.2)
38
+ unicode-display_width (2.4.2)
36
39
 
37
40
  PLATFORMS
38
41
  ruby
39
42
 
40
43
  DEPENDENCIES
41
- minitest
42
- rake
43
44
  standard
44
45
  swedish-pin!
45
46
 
46
47
  BUNDLED WITH
47
- 2.1.4
48
+ 2.2.22
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2017-2020 - Personnummer, Magnus Bergmark and Contributors
3
+ Copyright (c) 2017-2022 - Personnummer, Magnus Bergmark and Contributors
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # swedish-pin
2
2
 
3
- [![Build Status](https://secure.travis-ci.org/Mange/swedish-pin-ruby.png?branch=master)](http://travis-ci.org/Mange/swedish-pin-ruby) [![Inline docs](http://inch-ci.org/github/Mange/swedish-pin-ruby.svg?branch=master)](http://inch-ci.org/github/Mange/swedish-pin-ruby)
3
+ [![Inline docs](http://inch-ci.org/github/Mange/swedish-pin-ruby.svg?branch=master)](http://inch-ci.org/github/Mange/swedish-pin-ruby)
4
4
 
5
5
  Validate, parse, and generate [Swedish Personal Identity
6
6
  Numbers](https://en.wikipedia.org/wiki/Personal_identity_number_(Sweden))
data/Rakefile CHANGED
@@ -3,8 +3,6 @@ require "rake/testtask"
3
3
 
4
4
  Rake::TestTask.new(:test) do |t|
5
5
  t.libs << "test"
6
- t.libs << "lib"
7
- t.test_files = FileList["test/**/*_test.rb"]
8
6
  end
9
7
 
10
8
  task default: :test
data/bin/standardrb ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'standardrb' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("standard", "standardrb")
@@ -8,37 +8,37 @@ module SwedishPIN
8
8
  #
9
9
  # Generator for PINs.
10
10
  class Generator
11
- # The date all generated PINs will be based on.
12
- attr_reader :date
13
-
14
11
  # Creates a new generator for a particular date.
15
- def initialize(date)
16
- @date = date || random_date
12
+ def initialize(random: Random)
13
+ @random = random
17
14
  end
18
15
 
19
16
  # Generate a {Personnummer} with the given sequence number.
20
- def generate(sequence_number)
17
+ def generate(date: random_date, sequence_number: random_sequence_number)
18
+ # Handle someone explicitly passing `nil`.
19
+ date ||= random_date
21
20
  sequence_number ||= random_sequence_number
21
+
22
22
  Personnummer.new(
23
23
  year: date.year,
24
24
  month: date.month,
25
25
  day: date.day,
26
26
  sequence_number: sequence_number,
27
- control_digit: control_digit(sequence_number)
27
+ control_digit: control_digit(date, sequence_number)
28
28
  )
29
29
  end
30
30
 
31
31
  private
32
32
 
33
33
  def random_date
34
- Date.today - Random.rand(0..(110 * 365))
34
+ Date.today - @random.rand(0..(110 * 365))
35
35
  end
36
36
 
37
37
  def random_sequence_number
38
- Random.rand(0..999)
38
+ @random.rand(0..999)
39
39
  end
40
40
 
41
- def control_digit(sequence_number)
41
+ def control_digit(date, sequence_number)
42
42
  padded = ("%03d" % sequence_number)
43
43
  SwedishPIN.luhn("#{date.strftime("%y%m%d")}#{padded}")
44
44
  end
@@ -17,7 +17,7 @@ module SwedishPIN
17
17
  (?<sequence_number>\d{3})
18
18
  (?<control_digit>\d)
19
19
  \z
20
- /x.freeze
20
+ /x
21
21
  private_constant :MATCHER
22
22
 
23
23
  # Setup a new parser.
@@ -105,19 +105,30 @@ module SwedishPIN
105
105
  end
106
106
 
107
107
  def guess_century
108
- guessed_year = (now.year / 100) * 100 + year
109
-
110
- # Don't guess future dates; skip back a century when that happens.
111
- if Time.new(guessed_year, month, real_day) > now
112
- guessed_year -= 100
108
+ century = now.year / 100
109
+
110
+ # Century could take up to three different values, depending on
111
+ # combination of "future" dates and `+` separators.
112
+ #
113
+ # For example, on year 2010:
114
+ # 100101-nnnn => 2010-01-01
115
+ # 110101-nnnn => 1911-01-01 (because 2011 has not happened yet)
116
+ # 110101+nnnn => 1811-01-01 (1911 - 100 years)
117
+ # 090101+nnnn => 1909-01-01 (2009 - 100 years)
118
+ #
119
+ # One step comes from using the `+` separator, which means "remove
120
+ # another 100 years", despite what the rest of the date seems to imply.
121
+
122
+ # Assume previous century on future years
123
+ if (century * 100) + year > now.year
124
+ century -= 1
113
125
  end
114
126
 
115
- # The "+" separator means another century back.
116
127
  if @matches["separator"] == "+"
117
- guessed_year -= 100
128
+ century -= 1
118
129
  end
119
130
 
120
- guessed_year / 100
131
+ century
121
132
  end
122
133
 
123
134
  def validate_match
@@ -8,6 +8,8 @@ module SwedishPIN
8
8
  #
9
9
  # @see https://en.wikipedia.org/wiki/Personal_identity_number_(Sweden) Personnummer on Wikipedia.
10
10
  class Personnummer
11
+ include Comparable
12
+
11
13
  attr_reader :year, :month, :day, :sequence_number, :control_digit
12
14
  # @!attribute [r] year
13
15
  # The full year of the _personnummer_. For example +1989+.
@@ -42,7 +44,7 @@ module SwedishPIN
42
44
  @year = year
43
45
  @month = month
44
46
  @coordination_number = day > 60
45
- @day = (day > 60 ? day - 60 : day)
47
+ @day = ((day > 60) ? day - 60 : day)
46
48
  @sequence_number = sequence_number
47
49
  @control_digit = control_digit
48
50
  end
@@ -175,9 +177,32 @@ module SwedishPIN
175
177
  sequence_number.even?
176
178
  end
177
179
 
180
+ def ==(other)
181
+ if other.is_a?(self.class)
182
+ format_long == other.format_long
183
+ else
184
+ super
185
+ end
186
+ end
187
+
188
+ def <=>(other)
189
+ if other.is_a?(self.class)
190
+ format_long <=> other.format_long
191
+ else
192
+ super
193
+ end
194
+ end
195
+
196
+ def hash
197
+ [self.class, format_long].hash
198
+ end
199
+ alias_method :eql?, :==
200
+
178
201
  private
179
202
 
180
203
  def short_separator(now)
204
+ # Turn into `+` on the same year as the PINs 100th birthday, even before
205
+ # the actual date.
181
206
  if year <= (now.year - 100)
182
207
  "+"
183
208
  else
@@ -2,5 +2,5 @@
2
2
 
3
3
  module SwedishPIN
4
4
  # The version number of this library.
5
- VERSION = "1.0.0"
5
+ VERSION = "2.0.0"
6
6
  end
data/lib/swedish_pin.rb CHANGED
@@ -81,7 +81,7 @@ module SwedishPIN
81
81
  # @param [Date, Time, nil] birthday The birthday of the person the PIN identifies, or +nil+ for a random date in the past.
82
82
  # @param [String, Integer, nil] sequence_number The sequence number that correspond to the three digits after the birthday, or +nil+ to pick a random one.
83
83
  def self.generate(birthday = nil, sequence_number = nil)
84
- Generator.new(birthday).generate(sequence_number)
84
+ Generator.new.generate(date: birthday, sequence_number: sequence_number)
85
85
  end
86
86
 
87
87
  # @api private
data/personnummer.gemspec CHANGED
@@ -3,7 +3,7 @@ require_relative "lib/swedish_pin/version"
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "swedish-pin"
5
5
  s.version = SwedishPIN::VERSION
6
- s.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
6
+ s.required_ruby_version = Gem::Requirement.new(">= 2.7.0")
7
7
 
8
8
  s.license = "MIT"
9
9
  s.summary = "Work with Swedish PINs (Personnummer)"
@@ -22,7 +22,5 @@ Gem::Specification.new do |s|
22
22
  s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
23
  s.require_paths = ["lib"]
24
24
 
25
- s.add_development_dependency "rake"
26
- s.add_development_dependency "minitest"
27
25
  s.add_development_dependency "standard"
28
26
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swedish-pin
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Magnus Bergmark
@@ -10,36 +10,8 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2020-09-09 00:00:00.000000000 Z
13
+ date: 2023-01-11 00:00:00.000000000 Z
14
14
  dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: rake
17
- requirement: !ruby/object:Gem::Requirement
18
- requirements:
19
- - - ">="
20
- - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :development
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- version: '0'
29
- - !ruby/object:Gem::Dependency
30
- name: minitest
31
- requirement: !ruby/object:Gem::Requirement
32
- requirements:
33
- - - ">="
34
- - !ruby/object:Gem::Version
35
- version: '0'
36
- type: :development
37
- prerelease: false
38
- version_requirements: !ruby/object:Gem::Requirement
39
- requirements:
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- version: '0'
43
15
  - !ruby/object:Gem::Dependency
44
16
  name: standard
45
17
  requirement: !ruby/object:Gem::Requirement
@@ -64,14 +36,16 @@ executables: []
64
36
  extensions: []
65
37
  extra_rdoc_files: []
66
38
  files:
39
+ - ".github/workflows/ci.yml"
67
40
  - ".gitignore"
68
- - ".travis.yml"
41
+ - ".solargraph.yml"
69
42
  - CHANGELOG.md
70
43
  - Gemfile
71
44
  - Gemfile.lock
72
45
  - LICENSE
73
46
  - README.md
74
47
  - Rakefile
48
+ - bin/standardrb
75
49
  - lib/swedish-pin.rb
76
50
  - lib/swedish_pin.rb
77
51
  - lib/swedish_pin/errors.rb
@@ -92,14 +66,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
92
66
  requirements:
93
67
  - - ">="
94
68
  - !ruby/object:Gem::Version
95
- version: 2.5.0
69
+ version: 2.7.0
96
70
  required_rubygems_version: !ruby/object:Gem::Requirement
97
71
  requirements:
98
72
  - - ">="
99
73
  - !ruby/object:Gem::Version
100
74
  version: '0'
101
75
  requirements: []
102
- rubygems_version: 3.0.8
76
+ rubygems_version: 3.3.7
103
77
  signing_key:
104
78
  specification_version: 4
105
79
  summary: Work with Swedish PINs (Personnummer)
data/.travis.yml DELETED
@@ -1,8 +0,0 @@
1
- language: ruby
2
- before_script:
3
- bundle install
4
- rvm:
5
- - 2.5
6
- - 2.6
7
- - 2.7
8
- sudo: false