itax_code 0.1.2 → 0.1.3

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: f79a865df5d202807046608d8b774ae21b05426d6418815884ff808cae75d79f
4
- data.tar.gz: fee682091af5907dce1ef0ac32ddc16fb44cba9956e1c04534ba1505750e2c5f
3
+ metadata.gz: 902aaf958bf3e07384e9bb4cb59d060e1fb3a423d9c58929dad97f22d53f3305
4
+ data.tar.gz: 3fb7d7f218364c447cce5dd5cb3b557abc743e487484a40025358d6e29b7d53d
5
5
  SHA512:
6
- metadata.gz: 1f4552edfa936dd8224abccd20861236bcc51311d84342e4e9b0516f7b73b0fc7496a957cd5957ead747f6f44885ada49ea38fbfbf96d8444f252472e1eecc73
7
- data.tar.gz: 27ae449fc19eef5d2f30e0ca7e581837683ec2353e8003798f40ba23a05b0efa9a6f0d498c1f6927d7e7b0042a85e8940a3f09bfbb6ef8359d011bfc0ea20f97
6
+ metadata.gz: 9b3ec757113b83dd49b70314f8b8fc4a1518bead2c8a6c304b6a4c5d642439a81348f367e9b71914cf3f2f109d017d3b1e10b2576ecb967f5f201c5d2968cb80
7
+ data.tar.gz: 2ed3dc5aeec37b4ccd42882352fb3f1cde6b15c9a7727f3e76cb3687c38d67f44993ae928665ded670f6cc22feb5b047d150112a80ad505989fb56c8506c4255
@@ -1,3 +1,10 @@
1
+ # 0.1.3
2
+
3
+ - Add ability to encode and decode foreign countries tax codes
4
+ - Add custom exception to Parser and Encoder classes
5
+
6
+ ---
7
+
1
8
  # 0.1.1
2
9
 
3
10
  - Add credits to README.md
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- itax_code (0.1.2)
4
+ itax_code (0.1.3)
5
5
  activesupport
6
6
  rake
7
7
 
@@ -22,14 +22,14 @@ GEM
22
22
  concurrent-ruby (~> 1.0)
23
23
  minitest (5.14.2)
24
24
  mocha (1.11.2)
25
- parallel (1.20.0)
25
+ parallel (1.20.1)
26
26
  parser (2.7.2.0)
27
27
  ast (~> 2.4.1)
28
28
  rainbow (3.0.0)
29
29
  rake (13.0.1)
30
30
  regexp_parser (1.8.2)
31
31
  rexml (3.2.4)
32
- rubocop (1.3.1)
32
+ rubocop (1.4.1)
33
33
  parallel (~> 1.10)
34
34
  parser (>= 2.7.1.5)
35
35
  rainbow (>= 2.2.2, < 4.0)
data/README.md CHANGED
@@ -26,11 +26,11 @@ Or install it yourself as:
26
26
  ### Encode
27
27
 
28
28
  ```ruby
29
- ItaxCode.generate(
29
+ ItaxCode.encode(
30
30
  surname: "Rossi",
31
31
  name: "Mario",
32
- gender: "M", # can be M or F
33
- birthdate: "1980-1-1", # can be String, Time, Date or DateTime
32
+ gender: "M", # "M" or "F"
33
+ birthdate: "1980-1-1", # String, Time, Date or DateTime
34
34
  birthplace: "Milano"
35
35
  )
36
36
  ```
@@ -39,6 +39,39 @@ ItaxCode.generate(
39
39
 
40
40
  ```ruby
41
41
  ItaxCode.decode("RSSMRA80A01F205X")
42
+
43
+ # Output
44
+ #
45
+ # {
46
+ # code: "RSSMRA80A01F205X",
47
+ # gender: "M",
48
+ # birthdate: "1980-1-1",
49
+ # birthplace: {
50
+ # code: "F205",
51
+ # province: "MI",
52
+ # name: "MILANO"
53
+ # },
54
+ # omocodes: %w[
55
+ # RSSMRA80A01F205X
56
+ # RSSMRA80A01F20RS
57
+ # RSSMRA80A01F2LRD
58
+ # RSSMRA80A01FNLRS
59
+ # RSSMRA80A0MFNLRK
60
+ # RSSMRA80ALMFNLRV
61
+ # RSSMRA8LALMFNLRG
62
+ # RSSMRAULALMFNLRD
63
+ # ],
64
+ # raw: {
65
+ # surname: "RSS",
66
+ # name: "MRA",
67
+ # birthdate: "80A01",
68
+ # birthdate_year: "80",
69
+ # birthdate_month: "A",
70
+ # birthdate_day: "01",
71
+ # birthplace: "F205",
72
+ # cin: "X"
73
+ # }
74
+ # }
42
75
  ```
43
76
 
44
77
  ### Validate
@@ -80,5 +113,5 @@ chat rooms and mailing lists is expected to follow the [code of conduct](https:/
80
113
 
81
114
  ## Credits
82
115
 
83
- I took my cue from [python-codicefiscale](https://github.com/fabiocaccamo/python-codicefiscale) repo,
84
- coded by [Fabio Caccamo](https://github.com/fabiocaccamo) to write ItaxCode Gem.
116
+ I took insipiration from [python-codicefiscale](https://github.com/fabiocaccamo/python-codicefiscale)
117
+ repository, coded by [Fabio Caccamo](https://github.com/fabiocaccamo).
@@ -8,11 +8,11 @@ require "itax_code/validator"
8
8
  module ItaxCode
9
9
  class << self
10
10
  ##
11
- # This method encodes citizen tax code
11
+ # This method encodes user tax code.
12
12
  #
13
13
  # @example
14
14
  #
15
- # ItaxCode.generate({
15
+ # ItaxCode.encode({
16
16
  # surname: [String]
17
17
  # name: [String]
18
18
  # gender: [String]
@@ -20,7 +20,7 @@ module ItaxCode
20
20
  # birthplace: [String]
21
21
  # })
22
22
  #
23
- # @param [Hash] data The citizen data attributes
23
+ # @param [Hash] data The user data attributes
24
24
  #
25
25
  # @return [String]
26
26
 
@@ -29,13 +29,13 @@ module ItaxCode
29
29
  end
30
30
 
31
31
  ##
32
- # This method decodes tax code in its components
32
+ # This method decodes tax code in its components.
33
33
  #
34
34
  # @example
35
35
  #
36
36
  # ItaxCode.decode("CCCFBA85D03L219P")
37
37
  #
38
- # @param [String] tax_code The citizen tax code
38
+ # @param [String] tax_code The user tax code
39
39
  #
40
40
  # @return [Hash]
41
41
 
@@ -45,7 +45,7 @@ module ItaxCode
45
45
 
46
46
  ##
47
47
  # This method check given tax code validity
48
- # against new one generated by citizen informations
48
+ # against new one encoded from user informations.
49
49
  #
50
50
  # @example
51
51
  #
@@ -2,11 +2,11 @@ module ItaxCode
2
2
  ##
3
3
  # This class handles the tax code generation logic.
4
4
  #
5
- # @param [String] surname The citizen first name
6
- # @param [String] name The citizen last name
7
- # @param [String] gender The citizen gender
8
- # @param [String, Date] birthdate The citizen birthdate
9
- # @param [String] birthplace The citizen birthplace
5
+ # @param [String] surname The user first name
6
+ # @param [String] name The user last name
7
+ # @param [String] gender The user gender
8
+ # @param [String, Date] birthdate The user birthdate
9
+ # @param [String] birthplace The user birthplace
10
10
  #
11
11
  # @example
12
12
  #
@@ -21,13 +21,22 @@ module ItaxCode
21
21
  # @return [String] The encoded tax code
22
22
 
23
23
  class Encoder
24
+ class MissingDataError < StandardError; end
25
+
24
26
  def initialize(data = {}, utils = Utils.new)
25
27
  @surname = data[:surname]
26
28
  @name = data[:name]
27
29
  @gender = data[:gender].try :upcase
28
30
  @birthdate = parsed_date data[:birthdate]
29
31
  @birthplace = data[:birthplace]
30
- @utils = utils
32
+
33
+ instance_variables.each do |ivar|
34
+ next if instance_variable_get(ivar).present?
35
+
36
+ raise MissingDataError, "missing #{ivar} value"
37
+ end
38
+
39
+ @utils = utils
31
40
  end
32
41
 
33
42
  ##
@@ -81,10 +90,16 @@ module ItaxCode
81
90
  "#{year}#{month}#{day}"
82
91
  end
83
92
 
84
- def encode_birthplace
85
- utils.municipalities.find do |m|
93
+ def encode_birthplace(src = utils.municipalities, exit: false)
94
+ place = src.find do |m|
86
95
  utils.slugged(m["name"]) == utils.slugged(birthplace)
87
- end.try(:[], "code")
96
+ end
97
+
98
+ code = place.try(:[], "code")
99
+ return code if code.present?
100
+ return if exit
101
+
102
+ encode_birthplace utils.countries, exit: true
88
103
  end
89
104
 
90
105
  def parsed_date(date)
@@ -1,6 +1,6 @@
1
1
  module ItaxCode
2
2
  ##
3
- # This class handles the parsing logic for TaxCode module.
3
+ # This class handles the parsing logic.
4
4
  #
5
5
  # @param [String] tax_code
6
6
  #
@@ -11,9 +11,14 @@ module ItaxCode
11
11
  # @return [Hash]
12
12
 
13
13
  class Parser
14
+ class NoTaxCodeError < StandardError; end
15
+ class InvalidTaxCodeError < StandardError; end
16
+
14
17
  def initialize(tax_code, utils = Utils.new)
15
- @tax_code = (tax_code || "").upcase
16
18
  @utils = utils
19
+ @tax_code = (tax_code || "").upcase
20
+ raise NoTaxCodeError if @tax_code.blank?
21
+ raise InvalidTaxCodeError unless Validator.standard_length?(@tax_code)
17
22
  end
18
23
 
19
24
  ##
@@ -74,13 +79,22 @@ module ItaxCode
74
79
  end
75
80
  end
76
81
 
77
- def decode_birthplace
78
- places = utils.municipalities
79
- .select { |m| m["code"] == municipality_code }
82
+ def decode_birthplace(src = utils.municipalities, exit: false)
83
+ places = src.select do |m|
84
+ m["code"] == municipality_code
85
+ end
86
+
80
87
  place = places.find { |m| !m["name"].include? "soppresso" }
81
- place = (place.presence || places.last).deep_symbolize_keys
82
- place[:name] = place[:name].gsub(" (soppresso)", "")
83
- place
88
+ place = place.presence || places.last
89
+
90
+ if place.nil?
91
+ return if exit
92
+
93
+ decode_birthplace utils.countries, exit: true
94
+ else
95
+ place["name"] = place["name"].gsub(" (soppresso)", "")
96
+ place.deep_symbolize_keys
97
+ end
84
98
  end
85
99
 
86
100
  def municipality_code
@@ -103,10 +103,10 @@ module ItaxCode
103
103
  )
104
104
  end
105
105
 
106
- # def countries
107
- # @countries ||= JSON.parse(
108
- # File.read("#{__dir__}/data/countries.json")
109
- # )
110
- # end
106
+ def countries
107
+ @countries ||= JSON.parse(
108
+ File.read("#{__dir__}/data/countries.json")
109
+ )
110
+ end
111
111
  end
112
112
  end
@@ -1,8 +1,8 @@
1
1
  module ItaxCode
2
2
  ##
3
- # This class holds the TaxCode validation logic.
3
+ # This class holds the validation logic.
4
4
  #
5
- # @param [Hash] data The citizen input data
5
+ # @param [Hash] data The user input data
6
6
 
7
7
  class Validator
8
8
  LENGTH = 16
@@ -14,7 +14,7 @@ module ItaxCode
14
14
  class << self
15
15
  ##
16
16
  # This method checks tax code standard length
17
- # against citizen and business fical code standards.
17
+ # against user and business fical code standards.
18
18
  #
19
19
  # @param [String] code The tax code
20
20
  #
@@ -1,3 +1,3 @@
1
1
  module ItaxCode
2
- VERSION = "0.1.2".freeze
2
+ VERSION = "0.1.3".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.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matteo Rossi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-11-21 00:00:00.000000000 Z
11
+ date: 2020-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport