itax_code 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: a38d68de9f4fc5be15da055a5166c402adbe71d67b9e73213875e28c83b17db6
4
- data.tar.gz: 7e27421ed321f9f6a8b8d0ec2ba38d005a81414401d5fbfbaa434c6a17e8d40d
3
+ metadata.gz: f9694d3dacddb70177a4bf256b129b20d3242cb35e8067d0583daeff2dc0755b
4
+ data.tar.gz: d05018606dcef62dd39806d81a2249f2d4e1cd3c883a4b2e1d131a36140c6fa7
5
5
  SHA512:
6
- metadata.gz: 724473a7faa374ac886a4a65cf37fe0168e4f1de0e318be0155f42b525d4ee74252c60a474e5db794c2879792f82f9c912ea2d7385b3a5724ac4d042e08d5362
7
- data.tar.gz: 7af13a9eb19e9e3558bf83072448d561b86e964a61e3c4444638b6c7ceb938a4c84232b95ec74386f10358c8f205cd4a99b54bc9e6f14152b72d87ace7635bfa
6
+ metadata.gz: c9a0f96094dc63a9a9a930ae1a63b0f188e4ff0b995038443c46fd08d4bc2362e4eecaf7cb92c7c6bd00ca1a2c330796c30bba1bb0e2f3357036d950f9d31407
7
+ data.tar.gz: 275a041c4e41271b5f0b1bc1b8d5e8ace9b78aaba2a66dc0d61ad99b07206753c576241364d0d2721385d417e719bec962ad9dde718f42eb27f78bdc4c9c5de6
data/CHANGELOG.md CHANGED
@@ -1,5 +1,29 @@
1
1
  # Changelog
2
2
 
3
+ ## [2.0.0](https://github.com/matteoredz/itax-code/compare/v1.0.1...v2.0.0) (2023-10-01)
4
+
5
+
6
+ ### ⚠ BREAKING CHANGES
7
+
8
+ * remove Validator and merge its logic into Parser ([#32](https://github.com/matteoredz/itax-code/issues/32))
9
+ * remove data hash argument from ItaxCode.valid? and ItaxCode::Validator#valid? ([#30](https://github.com/matteoredz/itax-code/issues/30))
10
+
11
+ ### Bug Fixes
12
+
13
+ * remove data hash argument from ItaxCode.valid? and ItaxCode::Validator#valid? ([#30](https://github.com/matteoredz/itax-code/issues/30)) ([997f8b0](https://github.com/matteoredz/itax-code/commit/997f8b0b9f4bc00012475950a84465bf06b73a52))
14
+
15
+
16
+ ### Code Refactoring
17
+
18
+ * remove Validator and merge its logic into Parser ([#32](https://github.com/matteoredz/itax-code/issues/32)) ([348bdd0](https://github.com/matteoredz/itax-code/commit/348bdd003d1709b0dd2d41e58b0307a1b8e23ab1))
19
+
20
+ ## [1.0.1](https://github.com/matteoredz/itax-code/compare/v1.0.0...v1.0.1) (2023-08-31)
21
+
22
+
23
+ ### Bug Fixes
24
+
25
+ * NoMethodError undefined method 'upcase' for nil:NilClass ([#27](https://github.com/matteoredz/itax-code/issues/27)) ([15470d1](https://github.com/matteoredz/itax-code/commit/15470d104a8c0c958e24de199243692e88a94b92))
26
+
3
27
  ## [1.0.0](https://github.com/matteoredz/itax-code/compare/v0.4.1...v1.0.0) (2023-08-30)
4
28
 
5
29
 
data/README.md CHANGED
@@ -33,6 +33,10 @@ ItaxCode.encode(
33
33
  birthdate: "1980-01-01", # String|Time|Date|DateTime
34
34
  birthplace: "Milano" # Name(Milano)|code(F205)
35
35
  )
36
+
37
+ # Output
38
+ #
39
+ # "RSSMRA80A01F205X"
36
40
  ```
37
41
 
38
42
  ### Decode
@@ -197,16 +201,11 @@ ItaxCode.decode("RSSMRA80A01F205X")
197
201
  ### Validate
198
202
 
199
203
  ```ruby
200
- ItaxCode.valid?(
201
- "RSSMRA80A01F205X",
202
- {
203
- surname: "Rossi",
204
- name: "Mario",
205
- gender: "M",
206
- birthdate: "1980-01-01",
207
- birthplace: "Milano"
208
- }
209
- )
204
+ ItaxCode.valid?("RSSMRA80A01F205X")
205
+
206
+ # Output
207
+ #
208
+ # true
210
209
  ```
211
210
 
212
211
  ## Development
@@ -3,12 +3,6 @@
3
3
  module ItaxCode
4
4
  # Handles the tax code generation logic.
5
5
  #
6
- # @param [String] surname The user first name
7
- # @param [String] name The user last name
8
- # @param [String] gender The user gender
9
- # @param [String, Date] birthdate The user birthdate
10
- # @param [String] birthplace The user birthplace
11
- #
12
6
  # @example
13
7
  # ItaxCode::Encoder.new(
14
8
  # surname: "Rossi",
@@ -22,16 +16,24 @@ module ItaxCode
22
16
  class Encoder
23
17
  MissingDataError = Class.new(StandardError)
24
18
 
19
+ # @param [Hash] data The user attributes
20
+ # @param [Utils] utils
21
+ #
22
+ # @option data [String] :surname The user first name
23
+ # @option data [String] :name The user last name
24
+ # @option data [String] :gender The user gender
25
+ # @option data [String, Date] :birthdate The user birthdate
26
+ # @option data [String] :birthplace The user birthplace
25
27
  def initialize(data = {}, utils = Utils.new)
26
28
  @surname = data[:surname]
27
29
  @name = data[:name]
28
30
  @gender = data[:gender]&.upcase
29
31
  @birthdate = data[:birthdate].to_s
30
32
  @birthplace = data[:birthplace]
31
- @utils = utils
32
33
  validate_data_presence!
33
34
 
34
35
  @birthdate = parse_birthdate!
36
+ @utils = utils
35
37
  end
36
38
 
37
39
  # Computes the tax code from its components.
@@ -5,26 +5,28 @@ require "itax_code/omocode"
5
5
  module ItaxCode
6
6
  # Handles the parsing logic.
7
7
  #
8
- # @param [String] tax_code
9
- #
10
8
  # @example
11
9
  #
12
10
  # ItaxCode::Parser.new("RSSMRA70A01L726S").decode
13
11
  #
14
- # @return [Hash]
12
+ # @return [Hash] The parsed tax code
15
13
  class Parser
16
14
  Error = Class.new(StandardError)
17
15
  NoTaxCodeError = Class.new(Error)
18
16
  InvalidControlInternalNumberError = Class.new(Error)
19
17
  InvalidTaxCodeError = Class.new(Error)
20
18
 
19
+ LENGTH = 16
20
+
21
+ # @param [String] tax_code
22
+ # @param [Utils] utils
21
23
  def initialize(tax_code, utils = Utils.new)
22
- @tax_code = tax_code.upcase
23
- raise NoTaxCodeError if @tax_code.blank?
24
- raise InvalidTaxCodeError unless Validator.standard_length?(@tax_code)
24
+ @tax_code = tax_code&.upcase
25
+ @utils = utils
25
26
 
26
- @utils = utils
27
- raise InvalidControlInternalNumberError if raw[:cin] != @utils.encode_cin(tax_code)
27
+ raise NoTaxCodeError if @tax_code.blank?
28
+ raise InvalidTaxCodeError if @tax_code.length != LENGTH
29
+ raise InvalidControlInternalNumberError if raw[:cin] != @utils.encode_cin(@tax_code)
28
30
  end
29
31
 
30
32
  # Decodes the tax code into its components.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ItaxCode
4
- VERSION = "1.0.0"
4
+ VERSION = "2.0.0"
5
5
  end
data/lib/itax_code.rb CHANGED
@@ -6,13 +6,12 @@ require "itax_code/version"
6
6
  require "itax_code/utils"
7
7
  require "itax_code/encoder"
8
8
  require "itax_code/parser"
9
- require "itax_code/validator"
10
9
 
11
10
  module ItaxCode
12
11
  Error = Class.new(StandardError)
13
12
 
14
13
  class << self
15
- # Encodes user tax code.
14
+ # Encodes the user tax code.
16
15
  #
17
16
  # @param [Hash] data The user attributes
18
17
  #
@@ -27,7 +26,7 @@ module ItaxCode
27
26
  Encoder.new(data).encode
28
27
  end
29
28
 
30
- # Decodes tax code in its components.
29
+ # Decodes the tax code in its components.
31
30
  #
32
31
  # @param [String] tax_code The user tax code
33
32
  #
@@ -36,21 +35,16 @@ module ItaxCode
36
35
  Parser.new(tax_code).decode
37
36
  end
38
37
 
39
- # Checks the given tax code validity against new one
40
- # encoded from user informations.
38
+ # Checks the given tax code validity.
41
39
  #
42
40
  # @param [String] tax_code The user tax code
43
- # @param [Hash] data The optional user attributes
44
- #
45
- # @option data [String] :surname
46
- # @option data [String] :name
47
- # @option data [String] :gender
48
- # @option data [String, Date] :birthdate
49
- # @option data [String] :birthplace
50
41
  #
51
42
  # @return [Boolean]
52
- def valid?(tax_code, data = {})
53
- Validator.new(tax_code, data).valid?
43
+ def valid?(tax_code)
44
+ decode(tax_code)
45
+ true
46
+ rescue Parser::Error
47
+ false
54
48
  end
55
49
  end
56
50
  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: 1.0.0
4
+ version: 2.0.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: 2023-08-30 00:00:00.000000000 Z
11
+ date: 2023-10-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -54,7 +54,6 @@ files:
54
54
  - lib/itax_code/omocode.rb
55
55
  - lib/itax_code/parser.rb
56
56
  - lib/itax_code/utils.rb
57
- - lib/itax_code/validator.rb
58
57
  - lib/itax_code/version.rb
59
58
  - rakelib/cities.rake
60
59
  homepage: https://github.com/matteoredz/itax-code
@@ -1,80 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ItaxCode
4
- # Handles the validation logic.
5
- #
6
- # @param [Hash] data The user input data
7
- class Validator
8
- LENGTH = 16
9
- REQUIRED_KEYS = %i[surname name gender birthdate birthplace].freeze
10
-
11
- # @param [String] tax_code The pre-computed tax code
12
- # @param [Hash] data The optional user attributes
13
- #
14
- def initialize(tax_code, data = {})
15
- @tax_code = tax_code
16
- @data = data
17
- end
18
-
19
- class << self
20
- # Checks the tax code standard length against user
21
- # and business fiscal code standards.
22
- #
23
- # @param [String] tax_code The tax code
24
- #
25
- # @return [true, false]
26
- def standard_length?(tax_code)
27
- tax_code.length == LENGTH
28
- end
29
- end
30
-
31
- # Checks pre-computed tax code validity against newly encoded tax code.
32
- #
33
- # @return [true, false]
34
- def valid?
35
- encoded_tax_code == tax_code
36
- end
37
-
38
- private
39
-
40
- attr_reader :tax_code, :data
41
-
42
- # Encodes the tax code from the given 'data' hash, also backfilling missing information
43
- # by decoding the pre-computed tax code.
44
- #
45
- # @return [String, nil] The encoded tax code or nil if decoding the tax code fails
46
- def encoded_tax_code
47
- if partial_data?
48
- decoded_tax_code ? backfill_data! : return
49
- end
50
-
51
- Encoder.new(data).encode
52
- end
53
-
54
- # Decodes the given tax code to backfill possibly missing data in the 'data' hash.
55
- # If the decode fails, it means that the provided tax code is not valid.
56
- #
57
- # @return [Hash, nil] The decoded hash or nil if the tax code is not valid
58
- def decoded_tax_code
59
- @decoded_tax_code ||= begin
60
- Parser.new(tax_code).decode
61
- rescue Parser::Error
62
- nil
63
- end
64
- end
65
-
66
- def partial_data?
67
- REQUIRED_KEYS.any? { |required_key| data[required_key].blank? }
68
- end
69
-
70
- def backfill_data!
71
- data.tap do |hash|
72
- hash[:surname] ||= decoded_tax_code[:raw][:surname]
73
- hash[:name] ||= decoded_tax_code[:raw][:name]
74
- hash[:gender] ||= decoded_tax_code[:gender]
75
- hash[:birthdate] ||= decoded_tax_code[:birthdate]
76
- hash[:birthplace] ||= decoded_tax_code[:birthplace][:code]
77
- end
78
- end
79
- end
80
- end