valvat 1.0.1 → 1.1.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.
Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/.github/workflows/ruby.yml +35 -0
  5. data/.gitignore +1 -0
  6. data/.rubocop.yml +15 -0
  7. data/CHANGES.md +9 -1
  8. data/Gemfile +2 -0
  9. data/Guardfile +6 -4
  10. data/README.md +5 -3
  11. data/Rakefile +3 -1
  12. data/gemfiles/activemodel-5 +1 -1
  13. data/gemfiles/activemodel-6 +1 -1
  14. data/lib/active_model/validations/valvat_validator.rb +65 -32
  15. data/lib/valvat.rb +4 -2
  16. data/lib/valvat/checksum.rb +14 -13
  17. data/lib/valvat/checksum/at.rb +3 -1
  18. data/lib/valvat/checksum/be.rb +3 -1
  19. data/lib/valvat/checksum/bg.rb +17 -17
  20. data/lib/valvat/checksum/cy.rb +2 -0
  21. data/lib/valvat/checksum/de.rb +5 -3
  22. data/lib/valvat/checksum/dk.rb +3 -1
  23. data/lib/valvat/checksum/ee.rb +2 -0
  24. data/lib/valvat/checksum/es.rb +14 -16
  25. data/lib/valvat/checksum/fi.rb +3 -1
  26. data/lib/valvat/checksum/fr.rb +4 -2
  27. data/lib/valvat/checksum/gb.rb +46 -31
  28. data/lib/valvat/checksum/gr.rb +4 -2
  29. data/lib/valvat/checksum/hr.rb +2 -0
  30. data/lib/valvat/checksum/hu.rb +2 -0
  31. data/lib/valvat/checksum/ie.rb +5 -6
  32. data/lib/valvat/checksum/it.rb +6 -4
  33. data/lib/valvat/checksum/lt.rb +2 -0
  34. data/lib/valvat/checksum/lu.rb +3 -1
  35. data/lib/valvat/checksum/mt.rb +2 -0
  36. data/lib/valvat/checksum/nl.rb +5 -3
  37. data/lib/valvat/checksum/pl.rb +3 -1
  38. data/lib/valvat/checksum/pt.rb +3 -1
  39. data/lib/valvat/checksum/ro.rb +2 -0
  40. data/lib/valvat/checksum/se.rb +5 -3
  41. data/lib/valvat/checksum/si.rb +4 -2
  42. data/lib/valvat/error.rb +4 -1
  43. data/lib/valvat/local.rb +6 -4
  44. data/lib/valvat/locales/de.yml +2 -1
  45. data/lib/valvat/locales/en.yml +2 -1
  46. data/lib/valvat/locales/es.yml +1 -0
  47. data/lib/valvat/locales/fr.yml +1 -0
  48. data/lib/valvat/locales/hu.yml +1 -0
  49. data/lib/valvat/locales/nl.yml +1 -0
  50. data/lib/valvat/locales/pl.yml +1 -0
  51. data/lib/valvat/lookup.rb +5 -4
  52. data/lib/valvat/lookup/fault.rb +17 -16
  53. data/lib/valvat/lookup/request.rb +21 -16
  54. data/lib/valvat/lookup/response.rb +11 -10
  55. data/lib/valvat/syntax.rb +31 -30
  56. data/lib/valvat/utils.rb +12 -9
  57. data/lib/valvat/version.rb +3 -1
  58. data/spec/active_model/validations/valvat_validator_spec.rb +99 -93
  59. data/spec/spec_helper.rb +8 -8
  60. data/spec/valvat/checksum/at_spec.rb +6 -4
  61. data/spec/valvat/checksum/be_spec.rb +6 -4
  62. data/spec/valvat/checksum/bg_spec.rb +7 -4
  63. data/spec/valvat/checksum/cy_spec.rb +3 -1
  64. data/spec/valvat/checksum/de_spec.rb +6 -4
  65. data/spec/valvat/checksum/dk_spec.rb +6 -4
  66. data/spec/valvat/checksum/ee_spec.rb +3 -1
  67. data/spec/valvat/checksum/es_spec.rb +7 -4
  68. data/spec/valvat/checksum/fi_spec.rb +6 -4
  69. data/spec/valvat/checksum/fr_spec.rb +6 -4
  70. data/spec/valvat/checksum/gb_spec.rb +4 -2
  71. data/spec/valvat/checksum/gr_spec.rb +6 -4
  72. data/spec/valvat/checksum/hr_spec.rb +3 -1
  73. data/spec/valvat/checksum/hu_spec.rb +3 -1
  74. data/spec/valvat/checksum/ie_spec.rb +8 -6
  75. data/spec/valvat/checksum/it_spec.rb +10 -8
  76. data/spec/valvat/checksum/lt_spec.rb +3 -1
  77. data/spec/valvat/checksum/lu_spec.rb +6 -4
  78. data/spec/valvat/checksum/mt_spec.rb +3 -1
  79. data/spec/valvat/checksum/nl_spec.rb +9 -7
  80. data/spec/valvat/checksum/pl_spec.rb +8 -6
  81. data/spec/valvat/checksum/pt_spec.rb +10 -11
  82. data/spec/valvat/checksum/ro_spec.rb +3 -1
  83. data/spec/valvat/checksum/se_spec.rb +8 -6
  84. data/spec/valvat/checksum/si_spec.rb +10 -8
  85. data/spec/valvat/checksum_spec.rb +11 -9
  86. data/spec/valvat/lookup/fault_spec.rb +34 -0
  87. data/spec/valvat/lookup/request_spec.rb +19 -0
  88. data/spec/valvat/lookup/response_spec.rb +29 -0
  89. data/spec/valvat/lookup_spec.rb +138 -153
  90. data/spec/valvat/syntax_spec.rb +59 -280
  91. data/spec/valvat/utils_spec.rb +62 -47
  92. data/spec/valvat_spec.rb +140 -152
  93. data/valvat.gemspec +17 -17
  94. metadata +24 -27
  95. metadata.gz.sig +0 -0
  96. data/.travis.yml +0 -47
  97. data/gemfiles/activemodel-3-2 +0 -7
  98. data/gemfiles/activemodel-4 +0 -7
  99. data/gemfiles/before-ruby21/activemodel-3-2 +0 -8
  100. data/gemfiles/before-ruby21/activemodel-4 +0 -8
  101. data/gemfiles/before-ruby21/standalone +0 -7
  102. data/spec/valvat/lockup/fault_spec.rb +0 -32
  103. data/spec/valvat/lockup/request_spec.rb +0 -15
  104. data/spec/valvat/lockup/response_spec.rb +0 -27
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6c35eea0081e4df97219ee215ea3321c8add02bc130c5b28488f1dca676765dc
4
- data.tar.gz: 4ba7b89f223d26d1ac9d967944d6c2e0dbea74c3c6066f1d69d0e2f42ba204c8
3
+ metadata.gz: ddc578b98f0c869566769b5588b5768bbdcc5ba4901ab97227c7908502a7b5c9
4
+ data.tar.gz: f7bfc69d3cd2983bb3d306d0932ef6d436890850bc67f6c63e639d2c666dc1ac
5
5
  SHA512:
6
- metadata.gz: ab9623bd1d680546ca7978751b6536b626d2d5cb9ae35240b5152d4e7c9512e99e025481bf0a844254e6ad14d3f1f320dbf54ab6901387e354b7682c45cb3c3c
7
- data.tar.gz: 79a69d5d148b86282f609d66bf053a1b14d4bd86621d7277a5f17d1d1df6c6fd5c9731cff4eca8c8bb9dde9f1265eef7ea03c60d2d66876b0f255d97a9596a6b
6
+ metadata.gz: 910d4c387c6e82adeee3146b54e928b253fcd4aae6873ae438a575612b72a136459d50411c565ba47683b1b8f325f90771774add2b7110b1c4a6c4e880ba0999
7
+ data.tar.gz: a93a4d12a53fe3601525a153c6f2ae042259371f1775c05fb27ada151e91f3ff0325f794332797eb31ff502606dd31edee502d910866e8c76feeb6613cb3b95e
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -0,0 +1,35 @@
1
+ name: Specs
2
+
3
+ on:
4
+ push:
5
+ branches: [ master ]
6
+ paths-ignore:
7
+ - 'README.md'
8
+ - 'CHANGES.md'
9
+ pull_request:
10
+ branches: [ master ]
11
+
12
+ jobs:
13
+ test:
14
+ if: "!contains(github.event.head_commit.message, '[skip ci]')"
15
+ runs-on: ubuntu-latest
16
+ env: # $BUNDLE_GEMFILE must be set at the job level, so it is set for all steps
17
+ BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}
18
+ strategy:
19
+ matrix:
20
+ ruby-version: ['2.5', '2.6', '2.7', '3.0', 'jruby-head']
21
+ gemfile: [ 'standalone', 'activemodel-5', 'activemodel-6' ]
22
+ exclude:
23
+ - ruby-version: '3.0'
24
+ gemfile: 'activemodel-5'
25
+
26
+ steps:
27
+ - uses: actions/checkout@v2
28
+ - name: Set up Ruby
29
+ uses: ruby/setup-ruby@v1
30
+ with:
31
+ ruby-version: ${{ matrix.ruby-version }}
32
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
33
+ - name: Run specs
34
+ run: |
35
+ bundle exec rspec
data/.gitignore CHANGED
@@ -6,3 +6,4 @@ gemfiles/*.lock
6
6
  tmp
7
7
  .rbenv-*
8
8
  .ruby-version
9
+ .ruby-gemset
@@ -0,0 +1,15 @@
1
+ require: rubocop-rspec
2
+ AllCops:
3
+ NewCops: enable
4
+ TargetRubyVersion: 2.5
5
+
6
+ Metrics/BlockLength:
7
+ Exclude:
8
+ - valvat.gemspec
9
+ - spec/**/*_spec.rb
10
+
11
+ Style/Documentation:
12
+ Enabled: false
13
+
14
+ RSpec/MultipleExpectations:
15
+ Enabled: false
data/CHANGES.md CHANGED
@@ -1,7 +1,15 @@
1
1
 
2
2
  ### dev
3
3
 
4
- [full changelog](http://github.com/yolk/valvat/compare/v1.0.1...master)
4
+ [full changelog](http://github.com/yolk/valvat/compare/v1.1.0...master)
5
+
6
+ ### 1.1.0 / 2021-01-15
7
+
8
+ [full changelog](http://github.com/yolk/valvat/compare/v1.0.1...v1.1.0)
9
+
10
+ * Added support for Northern Ireland XI prefixed VAT numbers (by [Avatar Ignacy Kasperowicz](https://github.com/kspe))
11
+ * ActiveModel: Return specific error message if VIES is down and fail_if_down is set to true (by [Arkadiy Zabazhanov](https://github.com/pyromaniac))
12
+ * Removed support for EOL rubies (all before 2.5) and ActiveModel before 5.0
5
13
 
6
14
  ### 1.0.1 / 2020-12-06
7
15
 
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  # Specify your gem's dependencies in valvat.gemspec
data/Guardfile CHANGED
@@ -1,5 +1,7 @@
1
- guard :rspec, cmd: "bundle exec rspec --color" do
2
- watch(/^spec\/(.*)_spec.rb/)
3
- watch(/^lib\/(.*)\.rb/) { |m| "spec/#{m[1]}_spec.rb" }
4
- watch(/^spec\/spec_helper.rb/) { "spec" }
1
+ # frozen_string_literal: true
2
+
3
+ guard :rspec, cmd: 'bundle exec rspec --color' do
4
+ watch(%r{^spec/(.*)_spec.rb})
5
+ watch(%r{^lib/(.*)\.rb}) { |m| "spec/#{m[1]}_spec.rb" }
6
+ watch(%r{^spec/spec_helper.rb}) { 'spec' }
5
7
  end
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  valvat
2
2
  ===========
3
3
 
4
- [![Gem Version](https://badge.fury.io/rb/valvat.svg)](http://badge.fury.io/rb/valvat) [![Build Status](https://travis-ci.org/yolk/valvat.svg)](https://travis-ci.org/yolk/valvat) [![Code Climate](https://codeclimate.com/github/yolk/valvat.svg)](https://codeclimate.com/github/yolk/valvat)
4
+ [![rubygems](https://badgen.net/rubygems/v/valvat)](https://rubygems.org/gems/valvat) [![Specs](https://github.com/yolk/valvat/workflows/Specs/badge.svg)](https://github.com/yolk/valvat/actions?query=workflow%3ASpecs)
5
5
 
6
6
  Validates european vat numbers. Standalone or as a ActiveModel validator.
7
7
 
@@ -9,6 +9,8 @@ Validates european vat numbers. Standalone or as a ActiveModel validator.
9
9
 
10
10
  Valvat supports validating VAT-IDs from the UK by syntax & checksum for now. Validation against the VIES web service will probably stop working (and return `false`) in 2021.
11
11
 
12
+ Northern Ireland received its own VAT number prefix - XI which is supported by VIES web service so any XI-prefixed VAT numbers should be validated as any EU VAT number.
13
+
12
14
  ## Features
13
15
 
14
16
  * Simple syntax verification
@@ -18,7 +20,7 @@ Valvat supports validating VAT-IDs from the UK by syntax & checksum for now. Val
18
20
  * I18n locales for language specific error messages in English, German, French, Spanish, Italian, Portuguese, Polish, Swedish, Dutch, Danish, Czech, Slovakian, Hungarian, Bulgarian, Romanian, Latvian, Catalan, Norwegian, and Finnish.
19
21
  * *Experimental* checksum verification
20
22
 
21
- valvat is tested and works with ruby MRI 1.9.3/2.0/2.1/2.2/2.3/2.4/2.5/2.6/2.7, jruby (including 1.9 mode) and ActiveModel 3/4/5/6
23
+ valvat is tested and works with ruby MRI 2.5/2.6/2.7/3.0, jruby-head and ActiveModel 5/6. If you need support for ruby down to 1.9.3 and ActiveModel 3 and 4 use [v1.0.1](https://github.com/yolk/valvat/tree/v1.0.1).
22
24
 
23
25
  ## Installation
24
26
 
@@ -26,7 +28,7 @@ Add it to your Gemfile:
26
28
 
27
29
  gem 'valvat'
28
30
 
29
- To use less memory (~3.5 mb vs. ~0.5 mb) and load only the local verification functionality – and not the remote lookup with VIES – add it like this instead:
31
+ To use less memory (~0.5 mb vs. ~3.5 mb) and load only the local verification functionality – and not the remote lookup with VIES – add it like this instead:
30
32
 
31
33
  gem 'valvat', require: 'valvat/local'
32
34
 
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'bundler'
2
4
  Bundler::GemHelper.install_tasks
3
5
 
@@ -5,4 +7,4 @@ require 'rspec/core/rake_task'
5
7
 
6
8
  RSpec::Core::RakeTask.new(:spec)
7
9
 
8
- task :default => :spec
10
+ task default: :spec
@@ -1,6 +1,6 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- gem "activemodel", "~> 5.2.0"
3
+ gem "activemodel", "~> 5.0"
4
4
 
5
5
  gem 'rack'
6
6
 
@@ -1,6 +1,6 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- gem "activemodel", "~> 6.0.0"
3
+ gem "activemodel", "~> 6.0"
4
4
 
5
5
  gem 'rack'
6
6
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'active_model'
2
4
  require 'valvat/syntax'
3
5
  require 'valvat/lookup'
@@ -6,16 +8,7 @@ module ActiveModel
6
8
  module Validations
7
9
  class ValvatValidator < EachValidator
8
10
  def initialize(options)
9
- if options[:lookup]
10
- options[:lookup] = if options[:lookup] == :fail_if_down
11
- {fail_if_down: true}
12
- elsif options[:lookup].is_a?(Hash)
13
- options[:lookup]
14
- else
15
- {}
16
- end
17
- end
18
-
11
+ normalize_options(options)
19
12
  super
20
13
  end
21
14
 
@@ -23,44 +16,84 @@ module ActiveModel
23
16
  vat = Valvat(value)
24
17
  iso_country_code = vat.iso_country_code
25
18
 
26
- if country_does_not_match?(record, iso_country_code)
27
- iso_country_code = iso_country_code_of(record)
28
- elsif vat.valid? && valid_checksum?(vat) && vat_exists?(vat)
29
- return
30
- end
19
+ is_valid = if country_does_not_match?(record, iso_country_code)
20
+ iso_country_code = iso_country_code_of(record)
21
+ false
22
+ else
23
+ vat_valid?(vat)
24
+ end
31
25
 
32
- iso_country_code = "eu" if iso_country_code.blank?
33
- record.errors.add(attribute, :invalid_vat,
34
- message: options[:message],
35
- country_adjective: I18n.t(
36
- :"valvat.country_adjectives.#{iso_country_code.downcase}",
37
- default: [:"valvat.country_adjectives.eu", "european"]
38
- )
39
- )
26
+ iso_country_code = 'eu' if iso_country_code.blank?
27
+
28
+ add_error(is_valid, record, attribute, iso_country_code)
40
29
  end
41
30
 
42
31
  private
43
32
 
33
+ def vat_valid?(vat)
34
+ vat.valid? && valid_checksum?(vat) && vat_exists?(vat)
35
+ end
36
+
37
+ def valid_checksum?(vat)
38
+ return true unless options[:checksum]
39
+
40
+ vat.valid_checksum?
41
+ end
42
+
43
+ def vat_exists?(vat)
44
+ return true unless options[:lookup]
45
+
46
+ exists = vat.exists?(options[:lookup])
47
+
48
+ return true if exists.nil? && !options[:lookup][:fail_if_down]
49
+
50
+ exists
51
+ end
52
+
44
53
  def country_does_not_match?(record, iso_country_code)
45
54
  return false unless options[:match_country]
55
+
46
56
  iso_country_code_of(record) != iso_country_code
47
57
  end
48
58
 
49
59
  def iso_country_code_of(record)
50
- (record.send(options[:match_country]) || "").upcase
60
+ (record.send(options[:match_country]) || '').upcase
51
61
  end
52
62
 
53
- def valid_checksum?(vat)
54
- return true unless options[:checksum]
55
- vat.valid_checksum?
63
+ def add_error(is_valid, record, attribute, iso_country_code)
64
+ case is_valid
65
+ when false
66
+ add_invalid_vat_error(record, attribute, iso_country_code)
67
+ when nil
68
+ add_vies_down_error(record, attribute)
69
+ end
56
70
  end
57
71
 
58
- def vat_exists?(vat)
59
- return true unless options[:lookup]
72
+ def add_invalid_vat_error(record, attribute, iso_country_code)
73
+ record.errors.add(attribute, :invalid_vat,
74
+ message: options[:message],
75
+ country_adjective: I18n.t(
76
+ :"valvat.country_adjectives.#{iso_country_code.downcase}",
77
+ default: [:"valvat.country_adjectives.eu", 'european']
78
+ ))
79
+ end
80
+
81
+ def add_vies_down_error(record, attribute)
82
+ record.errors.add(attribute, :vies_down,
83
+ message: options[:vies_down_message])
84
+ end
85
+
86
+ def normalize_options(options)
87
+ return unless options[:lookup]
60
88
 
61
- is_valid = vat.exists?(options[:lookup])
62
- return is_valid unless is_valid.nil?
63
- !options[:lookup][:fail_if_down]
89
+ options[:lookup] = case options[:lookup]
90
+ when :fail_if_down
91
+ { fail_if_down: true }
92
+ when Hash
93
+ options[:lookup]
94
+ else
95
+ {}
96
+ end
64
97
  end
65
98
  end
66
99
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'valvat/error'
2
4
  require 'valvat/local'
3
5
  require 'valvat/lookup'
@@ -7,8 +9,8 @@ require 'valvat/lookup/fault'
7
9
  require 'active_model/validations/valvat_validator' if defined?(ActiveModel)
8
10
 
9
11
  class Valvat
10
- def exists?(options={})
12
+ def exists?(options = {})
11
13
  Valvat::Lookup.validate(self, options)
12
14
  end
13
- alias_method :exist?, :exists?
15
+ alias exist? exists?
14
16
  end
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Valvat
2
4
  module Checksum
3
- ALGORITHMS = {}
5
+ ALGORITHMS = {} # rubocop:disable Style/MutableConstant
4
6
 
5
7
  def self.validate(vat)
6
8
  vat = Valvat(vat)
@@ -11,11 +13,12 @@ class Valvat
11
13
  class Base
12
14
  def self.inherited(klass)
13
15
  ALGORITHMS[klass.name.split(/::/).last] = klass
16
+ super
14
17
  end
15
18
 
16
19
  attr_reader :vat
17
20
 
18
- def self.check_digit_length(len=nil)
21
+ def self.check_digit_length(len = nil)
19
22
  @check_digit_length = len if len
20
23
  @check_digit_length || 1
21
24
  end
@@ -39,37 +42,35 @@ class Valvat
39
42
  end
40
43
 
41
44
  def figures_str
42
- str_wo_country[0..-(self.class.check_digit_length+1)]
45
+ str_wo_country[0..-(self.class.check_digit_length + 1)]
43
46
  end
44
47
 
45
48
  def figures
46
- figures_str.split("").map(&:to_i)
49
+ figures_str.split('').map(&:to_i)
47
50
  end
48
51
 
49
52
  def str_wo_country
50
53
  vat.to_s_wo_country
51
54
  end
52
55
 
53
- def sum_figures_by
54
- figures.reverse.each_with_index.map do |fig, i|
55
- yield(fig, i)
56
- end.inject(:+)
56
+ def sum_figures_by(&block)
57
+ figures.reverse.each_with_index.map(&block).inject(:+)
57
58
  end
58
59
 
59
60
  def sum_of_squares(num)
60
- num.to_s.split("").map(&:to_i).inject(:+)
61
+ num.to_s.split('').map(&:to_i).inject(:+)
61
62
  end
62
63
 
63
- def sum_of_figures_for_at_es_it_se(reverse_ints=false)
64
+ def sum_of_figures_for_at_es_it_se(reverse_ints: false)
64
65
  ints = reverse_ints ? [2, 1] : [1, 2]
65
66
  sum_figures_by do |fig, i|
66
- sum_of_squares(fig*(i.modulo(2) == 0 ? ints[0] : ints[1]))
67
+ sum_of_squares(fig * (i.modulo(2).zero? ? ints[0] : ints[1]))
67
68
  end
68
69
  end
69
70
 
70
71
  def sum_of_figues_for_pt_si
71
72
  11 - sum_figures_by do |fig, i|
72
- fig*(i+2)
73
+ fig * (i + 2)
73
74
  end.modulo(11)
74
75
  end
75
76
  end
@@ -79,4 +80,4 @@ end
79
80
  Dir[File.join(File.dirname(__FILE__), 'checksum', '*.rb')].each do |path|
80
81
  # On Ruby 2.1.0 ActiveSupport goes mad if you pass it paths with .rb at the end
81
82
  require path.gsub(/\.rb$/, '')
82
- end
83
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Valvat
2
4
  module Checksum
3
5
  class AT < Base
@@ -11,4 +13,4 @@ class Valvat
11
13
  end
12
14
  end
13
15
  end
14
- end
16
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Valvat
2
4
  module Checksum
3
5
  class BE < Base
@@ -8,4 +10,4 @@ class Valvat
8
10
  end
9
11
  end
10
12
  end
11
- end
13
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Valvat
2
4
  module Checksum
3
5
  class BG < Base
@@ -9,6 +11,7 @@ class Valvat
9
11
  local_person_chk = check_digit_local_natural_person
10
12
 
11
13
  return local_person_chk if given_check_digit == local_person_chk
14
+
12
15
  check_digit_foreign_natural_person
13
16
  end
14
17
 
@@ -19,43 +22,40 @@ class Valvat
19
22
  end.inject(:+).modulo(11)
20
23
 
21
24
  return chk if chk < 10
22
- return 0
25
+
26
+ 0
23
27
  end
24
28
 
25
29
  def check_digit_foreign_natural_person
26
30
  weight = [21, 19, 17, 13, 11, 9, 7, 3, 1]
27
31
 
28
- chk = figures.map do |fig|
32
+ figures.map do |fig|
29
33
  fig * weight.shift
30
34
  end.inject(:+).modulo(10)
31
-
32
- chk
33
35
  end
34
36
 
35
37
  def check_digit_legal_person
36
- prod = 0
37
- figures.each_with_index do |fig, index|
38
- prod += (index + 1) * fig.to_i
39
- end
40
-
41
- chk = prod % 11
38
+ chk = build_prod % 11
42
39
 
43
40
  if chk == 10
44
- prod = 0
45
- figures.each_with_index do |fig, index|
46
- prod += (index + 3) * fig.to_i
47
- end
48
-
49
- chk = prod % 11
41
+ chk = build_prod(3) % 11
50
42
  chk = 0 if chk == 10
51
43
  end
52
44
 
53
45
  chk
54
46
  end
55
47
 
48
+ def build_prod(add = 1)
49
+ prod = 0
50
+ figures.each_with_index do |fig, index|
51
+ prod += (index + add) * fig.to_i
52
+ end
53
+ prod
54
+ end
55
+
56
56
  def natural_person?
57
57
  vat.to_s_wo_country.length == 10
58
58
  end
59
59
  end
60
60
  end
61
- end
61
+ end