itax_code 0.1.4 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e97e7183d8c6311ab15d58adef992c883882edb76045018da2497c10fb8c5bc5
4
- data.tar.gz: 845c4e2e027f06335b3aff1e2f03f7af44a60590de9b065a4c0edca444825c13
3
+ metadata.gz: 15fec89c684546176ef2c266a3c2b896225ba46b3051798bda0fca1975f03f89
4
+ data.tar.gz: bc02a9d22d009daf6800cc616d1ce84827267eaa877bc6e7b591c0707b3eee08
5
5
  SHA512:
6
- metadata.gz: e00f7799aebc8babd76844fda3e3f02dff0b4fd06eacb073243466d93356be682d0563f7c874b9b506958793bf8661b2a3135a2c06644cf84fdac0c6a25eb309
7
- data.tar.gz: b78c6f9b881232b182a051d863246193df4353f77f78fcffa6acb7131a0df13cca3587b54d3351d95a32b5064d2163253c4100cd07483492af7aa4036ca35e40
6
+ metadata.gz: 5121195f517531b125f1b8cb94c8c45b071b557068d4875e67ab87b5425d57fd4c73374d3c833885c5d66da43cae1cd0d27c2598a420aa4c0e8b83cb0e91b92e
7
+ data.tar.gz: 3805ae947b4505220130467207f5d86808825722bc3c563021cd163986a8643c996e9bdf1c01fa9918c2e111868f7a2204508a3d9c299e50c195333a7668411a
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- itax_code (0.1.4)
4
+ itax_code (0.2.0)
5
5
  activesupport
6
6
 
7
7
  GEM
@@ -22,13 +22,13 @@ GEM
22
22
  minitest (5.14.4)
23
23
  mocha (1.13.0)
24
24
  parallel (1.21.0)
25
- parser (3.0.2.0)
25
+ parser (3.0.3.0)
26
26
  ast (~> 2.4.1)
27
27
  rainbow (3.0.0)
28
28
  rake (13.0.6)
29
29
  regexp_parser (2.1.1)
30
30
  rexml (3.2.5)
31
- rubocop (1.22.3)
31
+ rubocop (1.23.0)
32
32
  parallel (~> 1.10)
33
33
  parser (>= 3.0.0.0)
34
34
  rainbow (>= 2.2.2, < 4.0)
@@ -37,9 +37,9 @@ GEM
37
37
  rubocop-ast (>= 1.12.0, < 2.0)
38
38
  ruby-progressbar (~> 1.7)
39
39
  unicode-display_width (>= 1.4.0, < 3.0)
40
- rubocop-ast (1.12.0)
40
+ rubocop-ast (1.13.0)
41
41
  parser (>= 3.0.1.1)
42
- rubocop-minitest (0.15.2)
42
+ rubocop-minitest (0.17.0)
43
43
  rubocop (>= 0.90, < 2.0)
44
44
  rubocop-performance (1.12.0)
45
45
  rubocop (>= 1.7.0, < 2.0)
@@ -71,4 +71,4 @@ DEPENDENCIES
71
71
  simplecov
72
72
 
73
73
  BUNDLED WITH
74
- 2.1.4
74
+ 2.2.28
data/README.md CHANGED
@@ -29,9 +29,9 @@ Or install it yourself as:
29
29
  ItaxCode.encode(
30
30
  surname: "Rossi",
31
31
  name: "Mario",
32
- gender: "M", # "M" or "F"
33
- birthdate: "1980-1-1", # String, Time, Date or DateTime
34
- birthplace: "Milano"
32
+ gender: "M", # M|F
33
+ birthdate: "1980-01-01", # String|Time|Date|DateTime
34
+ birthplace: "Milano" # Name(Milano)|code(F205)
35
35
  )
36
36
  ```
37
37
 
@@ -45,7 +45,7 @@ ItaxCode.decode("RSSMRA80A01F205X")
45
45
  # {
46
46
  # code: "RSSMRA80A01F205X",
47
47
  # gender: "M",
48
- # birthdate: "1980-1-1",
48
+ # birthdate: "1980-01-01",
49
49
  # birthplace: {
50
50
  # code: "F205",
51
51
  # province: "MI",
@@ -83,7 +83,7 @@ ItaxCode.valid?(
83
83
  surname: "Rossi",
84
84
  name: "Mario",
85
85
  gender: "M",
86
- birthdate: "1980-1-1",
86
+ birthdate: "1980-01-01",
87
87
  birthplace: "Milano"
88
88
  }
89
89
  )
@@ -10,9 +10,9 @@ module ItaxCode
10
10
  # @example
11
11
  # ItaxCode::Encoder.new(
12
12
  # surname: "Rossi",
13
- # name: "Matteo",
13
+ # name: "Mario",
14
14
  # gender: "M",
15
- # birthdate: "1990-08-23",
15
+ # birthdate: "1980-01-01",
16
16
  # birthplace: "Milano"
17
17
  # ).encode
18
18
  #
@@ -24,15 +24,12 @@ module ItaxCode
24
24
  @surname = data[:surname]
25
25
  @name = data[:name]
26
26
  @gender = data[:gender]&.upcase
27
- @birthdate = Date.parse(data[:birthdate].to_s)
27
+ @birthdate = data[:birthdate].to_s
28
28
  @birthplace = data[:birthplace]
29
29
  @utils = utils
30
+ validate_data_presence!
30
31
 
31
- instance_variables.each do |ivar|
32
- next if instance_variable_get(ivar).present?
33
-
34
- raise MissingDataError, "missing #{ivar} value"
35
- end
32
+ @birthdate = parse_birthdate!
36
33
  end
37
34
 
38
35
  # Computes the tax code from its components.
@@ -78,15 +75,30 @@ module ItaxCode
78
75
  "#{year}#{month}#{day}"
79
76
  end
80
77
 
81
- def encode_birthplace(src = utils.cities, exit: false)
82
- place = src.find do |item|
83
- utils.slugged(birthplace) == utils.slugged(item["name"])
84
- end
78
+ def encode_birthplace(src = utils.cities, stop: false)
79
+ lookup_key = birthplace.match?(/^\w{1}\d{3}$/) ? "code" : "name"
80
+ place_slug = utils.slugged(birthplace)
81
+ place_item = src.find { |i| place_slug == utils.slugged(i[lookup_key]) }
85
82
 
86
- code = place.try(:[], "code")
83
+ code = place_item.try(:[], "code")
87
84
  return code if code.present?
85
+ raise MissingDataError, "no code found for #{birthplace}" if stop
86
+
87
+ encode_birthplace(utils.countries, stop: true)
88
+ end
89
+
90
+ def validate_data_presence!
91
+ instance_variables.each do |ivar|
92
+ next if instance_variable_get(ivar).present?
93
+
94
+ raise MissingDataError, "missing #{ivar} value"
95
+ end
96
+ end
88
97
 
89
- exit || encode_birthplace(utils.countries, exit: true)
98
+ def parse_birthdate!
99
+ Date.parse(birthdate)
100
+ rescue StandardError
101
+ raise ArgumentError, "#{birthdate} is not a valid date"
90
102
  end
91
103
  end
92
104
  end
@@ -33,7 +33,7 @@ module ItaxCode
33
33
  {
34
34
  code: tax_code,
35
35
  gender: gender,
36
- birthdate: [year, month, day].join("-"),
36
+ birthdate: Date.parse([year, month, day].join("-")).to_s,
37
37
  birthplace: birthplace,
38
38
  omocodes: Omocode.new(tax_code).list,
39
39
  raw: raw
@@ -1,3 +1,3 @@
1
1
  module ItaxCode
2
- VERSION = "0.1.4".freeze
2
+ VERSION = "0.2.0".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: itax_code
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matteo Rossi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-11-03 00:00:00.000000000 Z
11
+ date: 2021-11-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport