itax_code 1.0.1 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
24
  @tax_code = tax_code&.upcase
23
- raise NoTaxCodeError if @tax_code.blank?
24
- raise InvalidTaxCodeError unless Validator.standard_length?(@tax_code)
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.1"
4
+ VERSION = "2.0.1"
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
data/rakelib/cities.rake CHANGED
@@ -16,7 +16,7 @@ namespace :cities do
16
16
  output_string = CSV.generate do |csv|
17
17
  csv << %w[code province name created_on deleted_on]
18
18
 
19
- CSV.foreach(tempfile.path, headers: true) do |row|
19
+ CSV.foreach(tempfile.path, encoding: "bom|utf-8", headers: true) do |row|
20
20
  csv << [
21
21
  row["CODCATASTALE"],
22
22
  row["SIGLAPROVINCIA"],
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.1
4
+ version: 2.0.1
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-31 00:00:00.000000000 Z
11
+ date: 2023-10-06 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