valvat 1.0.1 → 1.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/.github/workflows/ruby.yml +41 -0
  4. data/.gitignore +1 -0
  5. data/.rubocop.yml +15 -0
  6. data/CHANGES.md +27 -1
  7. data/Gemfile +2 -0
  8. data/Guardfile +6 -4
  9. data/README.md +6 -4
  10. data/Rakefile +3 -1
  11. data/certs/yolk.pem +25 -24
  12. data/gemfiles/activemodel-5 +1 -1
  13. data/gemfiles/activemodel-6 +1 -1
  14. data/gemfiles/{activemodel-3-2 → activemodel-7} +2 -2
  15. data/lib/active_model/validations/valvat_validator.rb +65 -32
  16. data/lib/valvat/checksum/at.rb +3 -1
  17. data/lib/valvat/checksum/be.rb +3 -1
  18. data/lib/valvat/checksum/bg.rb +17 -17
  19. data/lib/valvat/checksum/cy.rb +2 -0
  20. data/lib/valvat/checksum/de.rb +5 -3
  21. data/lib/valvat/checksum/dk.rb +3 -1
  22. data/lib/valvat/checksum/ee.rb +2 -0
  23. data/lib/valvat/checksum/es.rb +14 -16
  24. data/lib/valvat/checksum/fi.rb +3 -1
  25. data/lib/valvat/checksum/fr.rb +4 -2
  26. data/lib/valvat/checksum/gb.rb +46 -31
  27. data/lib/valvat/checksum/gr.rb +4 -2
  28. data/lib/valvat/checksum/hr.rb +2 -0
  29. data/lib/valvat/checksum/hu.rb +2 -0
  30. data/lib/valvat/checksum/ie.rb +5 -6
  31. data/lib/valvat/checksum/it.rb +7 -5
  32. data/lib/valvat/checksum/lt.rb +2 -0
  33. data/lib/valvat/checksum/lu.rb +3 -1
  34. data/lib/valvat/checksum/mt.rb +2 -0
  35. data/lib/valvat/checksum/nl.rb +5 -3
  36. data/lib/valvat/checksum/pl.rb +3 -1
  37. data/lib/valvat/checksum/pt.rb +3 -1
  38. data/lib/valvat/checksum/ro.rb +2 -0
  39. data/lib/valvat/checksum/se.rb +5 -3
  40. data/lib/valvat/checksum/si.rb +4 -2
  41. data/lib/valvat/checksum.rb +24 -28
  42. data/lib/valvat/error.rb +8 -2
  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/fault.rb +29 -21
  52. data/lib/valvat/lookup/request.rb +21 -16
  53. data/lib/valvat/lookup/response.rb +11 -10
  54. data/lib/valvat/lookup.rb +5 -4
  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/lib/valvat.rb +4 -2
  59. data/spec/active_model/validations/valvat_validator_spec.rb +99 -93
  60. data/spec/spec_helper.rb +8 -8
  61. data/spec/valvat/checksum/at_spec.rb +6 -4
  62. data/spec/valvat/checksum/be_spec.rb +6 -4
  63. data/spec/valvat/checksum/bg_spec.rb +7 -4
  64. data/spec/valvat/checksum/cy_spec.rb +3 -1
  65. data/spec/valvat/checksum/de_spec.rb +6 -4
  66. data/spec/valvat/checksum/dk_spec.rb +6 -4
  67. data/spec/valvat/checksum/ee_spec.rb +3 -1
  68. data/spec/valvat/checksum/es_spec.rb +7 -4
  69. data/spec/valvat/checksum/fi_spec.rb +6 -4
  70. data/spec/valvat/checksum/fr_spec.rb +6 -4
  71. data/spec/valvat/checksum/gb_spec.rb +4 -2
  72. data/spec/valvat/checksum/gr_spec.rb +6 -4
  73. data/spec/valvat/checksum/hr_spec.rb +3 -1
  74. data/spec/valvat/checksum/hu_spec.rb +3 -1
  75. data/spec/valvat/checksum/ie_spec.rb +8 -6
  76. data/spec/valvat/checksum/it_spec.rb +10 -8
  77. data/spec/valvat/checksum/lt_spec.rb +3 -1
  78. data/spec/valvat/checksum/lu_spec.rb +6 -4
  79. data/spec/valvat/checksum/mt_spec.rb +3 -1
  80. data/spec/valvat/checksum/nl_spec.rb +9 -7
  81. data/spec/valvat/checksum/pl_spec.rb +8 -6
  82. data/spec/valvat/checksum/pt_spec.rb +10 -11
  83. data/spec/valvat/checksum/ro_spec.rb +3 -1
  84. data/spec/valvat/checksum/se_spec.rb +8 -6
  85. data/spec/valvat/checksum/si_spec.rb +10 -8
  86. data/spec/valvat/checksum_spec.rb +24 -9
  87. data/spec/valvat/lookup/fault_spec.rb +34 -0
  88. data/spec/valvat/lookup/request_spec.rb +32 -0
  89. data/spec/valvat/lookup/response_spec.rb +29 -0
  90. data/spec/valvat/lookup_spec.rb +171 -152
  91. data/spec/valvat/syntax_spec.rb +59 -280
  92. data/spec/valvat/utils_spec.rb +64 -47
  93. data/spec/valvat_spec.rb +140 -152
  94. data/valvat.gemspec +17 -17
  95. data.tar.gz.sig +1 -2
  96. metadata +48 -50
  97. metadata.gz.sig +0 -0
  98. data/.travis.yml +0 -47
  99. data/gemfiles/activemodel-4 +0 -7
  100. data/gemfiles/before-ruby21/activemodel-3-2 +0 -8
  101. data/gemfiles/before-ruby21/activemodel-4 +0 -8
  102. data/gemfiles/before-ruby21/standalone +0 -7
  103. data/spec/valvat/lockup/fault_spec.rb +0 -32
  104. data/spec/valvat/lockup/request_spec.rb +0 -15
  105. data/spec/valvat/lockup/response_spec.rb +0 -27
@@ -1,51 +1,66 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Valvat
2
4
  module Checksum
3
5
  class GB < Base
4
- OLD_FORMAT_FORBIDDEN_RANGES = [(100_000..999_999), (9_490_001..9_700_000), (9_990_001..9_999_999)]
5
- NEW_FORMAT_FORBIDDEN_RANGES = [(1..100_000), (100_001..1_000_000)]
6
- GOV_NUMBER = /\A(GD[0-4]{1}\d{2})\Z/
7
- HEALTH_NUMBER = /\A(HA[5-9]{1}\d{2})\Z/
8
-
9
- def validate
10
- vat_number = vat.to_s_wo_country
6
+ OLD_FORMAT_FORBIDDEN_RANGES = [(100_000..999_999), (9_490_001..9_700_000), (9_990_001..9_999_999)].freeze
7
+ NEW_FORMAT_FORBIDDEN_RANGES = [(1..100_000), (100_001..1_000_000)].freeze
8
+ GOV_NUMBER = /\A(GD[0-4]{1}\d{2})\Z/.freeze
9
+ HEALTH_NUMBER = /\A(HA[5-9]{1}\d{2})\Z/.freeze
11
10
 
11
+ def validate # rubocop:disable Metrics/CyclomaticComplexity
12
12
  # government departments and health authorities, so no checksum
13
- return true if gov_or_health?(vat_number)
14
- return false if all_zero?(vat_number)
13
+ return true if gov_or_health?
14
+ return false if all_zero?
15
15
 
16
- checksum = vat_number[7..8].to_i
17
- vat_base = vat_number[0..6]
18
- vat_base_int = vat_base.to_i
19
- vat_base_sum = generate_vat_base_sum(vat_base)
16
+ return false if old_format_remainder.zero? && fobidden_in_old_format?
17
+ return false if new_format_remainder.zero? && fobidden_in_new_format?
20
18
 
21
- old_format_remainder = (vat_base_sum + checksum).modulo(97)
22
- new_format_remainder = (vat_base_sum + 55 + checksum).modulo(97)
19
+ old_format_remainder.zero? || new_format_remainder.zero?
20
+ end
23
21
 
24
- return false if old_format_remainder == 0 &&
25
- OLD_FORMAT_FORBIDDEN_RANGES.any? { |range| range.include? vat_base_int }
22
+ private
26
23
 
27
- return false if new_format_remainder == 0 &&
28
- NEW_FORMAT_FORBIDDEN_RANGES.any? { |range| range.include? vat_base_int }
24
+ def checksum
25
+ @checksum ||= str_wo_country[7..8].to_i
26
+ end
29
27
 
30
- old_format_remainder == 0 || new_format_remainder == 0
28
+ def vat_base
29
+ @vat_base ||= str_wo_country[0..6]
31
30
  end
32
31
 
33
- private
32
+ def gov_or_health?
33
+ str_wo_country =~ GOV_NUMBER || str_wo_country =~ HEALTH_NUMBER
34
+ end
34
35
 
35
- def gov_or_health?(vat_number)
36
- vat_number =~ GOV_NUMBER || vat_number =~ HEALTH_NUMBER
36
+ def all_zero?
37
+ str_wo_country =~ /\A0{9}\Z/ || str_wo_country =~ /\A0{12}\Z/
37
38
  end
38
39
 
39
- def all_zero?(vat_number)
40
- vat_number =~ /\A0{9}\Z/ || vat_number =~ /\A0{12}\Z/
40
+ def vat_base_sum
41
+ @vat_base_sum ||= vat_base.split('')
42
+ .map(&:to_i)
43
+ .zip([8, 7, 6, 5, 4, 3, 2])
44
+ .map { |vat_number_digit, multiplier| vat_number_digit * multiplier }
45
+ .inject(:+)
41
46
  end
42
47
 
43
- def generate_vat_base_sum(vat_base)
44
- vat_base.split('').
45
- map(&:to_i).
46
- zip([8, 7, 6, 5, 4, 3, 2]).
47
- map { |vat_number_digit, multiplier| vat_number_digit * multiplier }.
48
- inject(:+)
48
+ def old_format_remainder
49
+ @old_format_remainder ||= (vat_base_sum + checksum).modulo(97)
50
+ end
51
+
52
+ def new_format_remainder
53
+ @new_format_remainder ||= (vat_base_sum + 55 + checksum).modulo(97)
54
+ end
55
+
56
+ def fobidden_in_new_format?
57
+ vat_base_int = vat_base.to_i
58
+ NEW_FORMAT_FORBIDDEN_RANGES.any? { |range| range.include?(vat_base_int) }
59
+ end
60
+
61
+ def fobidden_in_old_format?
62
+ vat_base_int = vat_base.to_i
63
+ OLD_FORMAT_FORBIDDEN_RANGES.any? { |range| range.include?(vat_base_int) }
49
64
  end
50
65
  end
51
66
  end
@@ -1,12 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Valvat
2
4
  module Checksum
3
5
  class GR < Base
4
6
  def check_digit
5
7
  chk = sum_figures_by do |fig, i|
6
- fig*(2**(i+1))
8
+ fig * (2**(i + 1))
7
9
  end.modulo(11)
8
10
  chk > 9 ? 0 : chk
9
11
  end
10
12
  end
11
13
  end
12
- end
14
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Valvat
2
4
  module Checksum
3
5
  class HR < Base
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Valvat
2
4
  module Checksum
3
5
  class HU < Base
@@ -1,17 +1,17 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Valvat
2
4
  module Checksum
3
5
  class IE < Base
4
6
  def check_digit
5
7
  total = sum_figures_by do |fig, i|
6
- fig*(i+2)
7
- end
8
- if str_wo_country.size == 9
9
- total += ((CHARS.index(str_wo_country[8]) || 0) * 9)
8
+ fig * (i + 2)
10
9
  end
10
+ total += ((CHARS.index(str_wo_country[8]) || 0) * 9) if str_wo_country.size == 9
11
11
  total.modulo(23)
12
12
  end
13
13
 
14
- CHARS = "WABCDEFGHIJKLMNOPQRSTUV".split("")
14
+ CHARS = 'WABCDEFGHIJKLMNOPQRSTUV'.split('')
15
15
 
16
16
  def given_check_digit
17
17
  if str_wo_country.size == 9
@@ -38,7 +38,6 @@ class Valvat
38
38
  super
39
39
  end
40
40
  end
41
-
42
41
  end
43
42
  end
44
43
  end
@@ -1,17 +1,19 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Valvat
2
4
  module Checksum
3
5
  class IT < Base
4
6
  def validate
5
7
  y = figures_str[7..9].to_i
6
- y >= 1 && (y <= 100 || [120, 121].include?(y)) &&
7
- figures_str[0..6] != "0000000" &&
8
- super
8
+ y >= 1 && (y <= 100 || [120, 121, 999].include?(y)) &&
9
+ figures_str[0..6] != '0000000' &&
10
+ super
9
11
  end
10
12
 
11
13
  def check_digit
12
- chk = 10 - sum_of_figures_for_at_es_it_se(true).modulo(10)
14
+ chk = 10 - sum_of_figures_for_at_es_it_se(reverse_ints: true).modulo(10)
13
15
  chk == 10 ? 0 : chk
14
16
  end
15
17
  end
16
18
  end
17
- end
19
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Valvat
2
4
  module Checksum
3
5
  class LT < Base
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Valvat
2
4
  module Checksum
3
5
  class LU < 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 MT < Base
@@ -1,14 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Valvat
2
4
  module Checksum
3
5
  class NL < Base
4
6
  def validate
5
7
  vat.to_s.gsub(/[A-Z]/) { |let| (let.ord - 55).to_s }.to_i % 97 == 1 ||
6
- super
8
+ super
7
9
  end
8
10
 
9
11
  def check_digit
10
12
  sum_figures_by do |fig, i|
11
- fig*(i+2)
13
+ fig * (i + 2)
12
14
  end.modulo(11)
13
15
  end
14
16
 
@@ -17,4 +19,4 @@ class Valvat
17
19
  end
18
20
  end
19
21
  end
20
- end
22
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Valvat
2
4
  module Checksum
3
5
  class PL < Base
@@ -9,4 +11,4 @@ class Valvat
9
11
  end
10
12
  end
11
13
  end
12
- end
14
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Valvat
2
4
  module Checksum
3
5
  class PT < Base
@@ -7,4 +9,4 @@ class Valvat
7
9
  end
8
10
  end
9
11
  end
10
- end
12
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Valvat
2
4
  module Checksum
3
5
  class RO < Base
@@ -1,11 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Valvat
2
4
  module Checksum
3
5
  class SE < Base
4
6
  check_digit_length 0
5
7
 
6
8
  def validate
7
- vat.to_s_wo_country[-2..-1].to_i > 0 &&
8
- super
9
+ vat.to_s_wo_country[-2..-1].to_i.positive? &&
10
+ super
9
11
  end
10
12
 
11
13
  private
@@ -23,4 +25,4 @@ class Valvat
23
25
  end
24
26
  end
25
27
  end
26
- end
28
+ end
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Valvat
2
4
  module Checksum
3
5
  class SI < Base
4
6
  def validate
5
- figures_str.to_i > 999999 &&
6
- super
7
+ figures_str.to_i > 999_999 &&
8
+ super
7
9
  end
8
10
 
9
11
  def check_digit
@@ -1,21 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Valvat
2
4
  module Checksum
3
- ALGORITHMS = {}
4
-
5
- def self.validate(vat)
6
- vat = Valvat(vat)
7
- algo = ALGORITHMS[vat.iso_country_code]
8
- Valvat::Syntax.validate(vat) && !!(algo.nil? || algo.new(vat).validate)
9
- end
10
-
11
5
  class Base
12
- def self.inherited(klass)
13
- ALGORITHMS[klass.name.split(/::/).last] = klass
14
- end
15
-
16
6
  attr_reader :vat
17
7
 
18
- def self.check_digit_length(len=nil)
8
+ def self.check_digit_length(len = nil)
19
9
  @check_digit_length = len if len
20
10
  @check_digit_length || 1
21
11
  end
@@ -39,44 +29,50 @@ class Valvat
39
29
  end
40
30
 
41
31
  def figures_str
42
- str_wo_country[0..-(self.class.check_digit_length+1)]
32
+ str_wo_country[0..-(self.class.check_digit_length + 1)]
43
33
  end
44
34
 
45
35
  def figures
46
- figures_str.split("").map(&:to_i)
36
+ figures_str.split('').map(&:to_i)
47
37
  end
48
38
 
49
39
  def str_wo_country
50
40
  vat.to_s_wo_country
51
41
  end
52
42
 
53
- def sum_figures_by
54
- figures.reverse.each_with_index.map do |fig, i|
55
- yield(fig, i)
56
- end.inject(:+)
43
+ def sum_figures_by(&block)
44
+ figures.reverse.each_with_index.map(&block).inject(:+)
57
45
  end
58
46
 
59
47
  def sum_of_squares(num)
60
- num.to_s.split("").map(&:to_i).inject(:+)
48
+ num.to_s.split('').map(&:to_i).inject(:+)
61
49
  end
62
50
 
63
- def sum_of_figures_for_at_es_it_se(reverse_ints=false)
51
+ def sum_of_figures_for_at_es_it_se(reverse_ints: false)
64
52
  ints = reverse_ints ? [2, 1] : [1, 2]
65
53
  sum_figures_by do |fig, i|
66
- sum_of_squares(fig*(i.modulo(2) == 0 ? ints[0] : ints[1]))
54
+ sum_of_squares(fig * (i.modulo(2).zero? ? ints[0] : ints[1]))
67
55
  end
68
56
  end
69
57
 
70
58
  def sum_of_figues_for_pt_si
71
59
  11 - sum_figures_by do |fig, i|
72
- fig*(i+2)
60
+ fig * (i + 2)
73
61
  end.modulo(11)
74
62
  end
75
63
  end
64
+
65
+ def self.validate(vat)
66
+ vat = Valvat(vat)
67
+ algo = ALGORITHMS[vat.iso_country_code]
68
+ Valvat::Syntax.validate(vat) && !!(algo.nil? || algo.new(vat).validate)
69
+ end
70
+
71
+ ALGORITHMS = Dir[File.join(__dir__, 'checksum', '*.rb')].each_with_object({}) do |path, algos|
72
+ require path.gsub(/\.rb$/, '')
73
+
74
+ classname = File.basename(path, '.rb').upcase
75
+ algos[classname] = ['Valvat', 'Checksum', classname].inject(Object, :const_get)
76
+ end.freeze
76
77
  end
77
78
  end
78
-
79
- Dir[File.join(File.dirname(__FILE__), 'checksum', '*.rb')].each do |path|
80
- # On Ruby 2.1.0 ActiveSupport goes mad if you pass it paths with .rb at the end
81
- require path.gsub(/\.rb$/, '')
82
- end
data/lib/valvat/error.rb CHANGED
@@ -1,9 +1,13 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Valvat
2
4
  Error = Class.new(RuntimeError)
3
5
 
4
6
  class ViesError < Error
5
- def initialize(faultstring='UNKNOWN')
6
- @faultstring = faultstring
7
+ def initialize(faultstring = 'UNKNOWN', exception = nil)
8
+ @faultstring = faultstring || exception.inspect
9
+ @exception = exception
10
+ super(faultstring)
7
11
  end
8
12
 
9
13
  def to_s
@@ -19,6 +23,8 @@ class Valvat
19
23
  ServiceUnavailable = Class.new(ViesMaintenanceError)
20
24
  MemberStateUnavailable = Class.new(ViesMaintenanceError)
21
25
 
26
+ OperationUnknown = Class.new(ViesError)
27
+ HTTPError = Class.new(ViesError)
22
28
  Timeout = Class.new(ViesError)
23
29
  InvalidRequester = Class.new(ViesError)
24
30
  BlockedError = Class.new(ViesError)
data/lib/valvat/local.rb CHANGED
@@ -1,13 +1,15 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Valvat
2
4
  def initialize(raw)
3
- @raw = Valvat::Utils.normalize(raw || "")
5
+ @raw = Valvat::Utils.normalize(raw || '')
4
6
  @vat_country_code, @to_s_wo_country = to_a
5
7
  end
6
8
 
7
9
  attr_reader :raw, :vat_country_code, :to_s_wo_country
8
10
 
9
11
  def blank?
10
- raw.nil? || raw.strip == ""
12
+ raw.nil? || raw.strip == ''
11
13
  end
12
14
 
13
15
  def valid?
@@ -36,11 +38,11 @@ class Valvat
36
38
  end
37
39
 
38
40
  def inspect
39
- "#<Valvat #{[raw, iso_country_code].compact.join(" ")}>"
41
+ "#<Valvat #{[raw, iso_country_code].compact.join(' ')}>"
40
42
  end
41
43
  end
42
44
 
43
- def Valvat(vat)
45
+ def Valvat(vat) # rubocop:disable Naming/MethodName
44
46
  vat.is_a?(Valvat) ? vat : Valvat.new(vat)
45
47
  end
46
48
 
@@ -1,7 +1,8 @@
1
1
  de:
2
2
  errors:
3
3
  messages:
4
- invalid_vat: ist keine gültige %{country_adjective} USt-IdNr.
4
+ invalid_vat: "ist keine gültige %{country_adjective} USt-IdNr."
5
+ vies_down: "Die USt-IdNr. kann nicht überprüft werden: der VIES-Webservice ist nicht verfügbar. Bitte später noch einmal versuchen."
5
6
  valvat:
6
7
  country_adjectives:
7
8
  eu: europäische
@@ -1,7 +1,8 @@
1
1
  en:
2
2
  errors:
3
3
  messages:
4
- invalid_vat: is not a valid %{country_adjective} VAT number
4
+ invalid_vat: "is not a valid %{country_adjective} VAT number"
5
+ vies_down: "Unable to validate your VAT number: the VIES service is down. Please try again later."
5
6
  valvat:
6
7
  country_adjectives:
7
8
  eu: European
@@ -2,6 +2,7 @@ es:
2
2
  errors:
3
3
  messages:
4
4
  invalid_vat: no es un número de IVA intracomunitario %{country_adjective} válido
5
+ vies_down: "No se ha podido validar su número de IVA: El servicio VIES no está disponible, por favor inténtelo más tarde."
5
6
  valvat:
6
7
  country_adjectives:
7
8
  eu: europeo
@@ -2,6 +2,7 @@ fr:
2
2
  errors:
3
3
  messages:
4
4
  invalid_vat: "Numéro de TVA %{country_adjective} invalide"
5
+ vies_down: "Impossible de valider votre numéro de TVA : le service de validation VIES est indisponible. Merci de réessayer ultérieurement."
5
6
  valvat:
6
7
  country_adjectives:
7
8
  eu: intracommunautaire
@@ -2,6 +2,7 @@ hu:
2
2
  errors:
3
3
  messages:
4
4
  invalid_vat: a közösségi adószám %{country_adjective} helytelen
5
+ vies_down: "Az adószám ellenőrzése sikertelen: a VIES szolgáltatás nem elérhető. Kérjük próbálja újra később."
5
6
  valvat:
6
7
  country_adjectives:
7
8
  eu: európai
@@ -2,6 +2,7 @@ nl:
2
2
  errors:
3
3
  messages:
4
4
  invalid_vat: is geen geldig %{country_adjective} btw nummer
5
+ vies_down: "We kunnen uw btw-nummer niet bevestigen: de VIES-service is momenteel niet beschikbaar. Probeer het later opnieuw."
5
6
  valvat:
6
7
  country_adjectives:
7
8
  eu: Europees
@@ -2,6 +2,7 @@ pl:
2
2
  errors:
3
3
  messages:
4
4
  invalid_vat: nie jest prawidłowym %{country_adjective} numerem VAT
5
+ vies_down: "Nie można zweryfikować numeru VAT: serwis VIES nie działa. Spróbuj ponownie później."
5
6
  valvat:
6
7
  country_adjectives:
7
8
  eu: europejskim
@@ -1,30 +1,38 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Valvat
2
4
  class Lookup
3
5
  class Fault < Response
4
- private
6
+ def to_hash
7
+ @to_hash ||= case @raw
8
+ when Savon::HTTPError
9
+ { error: HTTPError.new(nil, @raw) }
10
+ when Savon::UnknownOperationError
11
+ { error: OperationUnknown.new(nil, @raw) }
12
+ else
13
+ fault = @raw.to_hash[:fault][:faultstring]
5
14
 
6
- def self.cleanup(hash)
7
- fault = hash[:fault][:faultstring]
8
- return {valid: false} if fault == "INVALID_INPUT"
9
- {error: fault_to_error(fault)}
15
+ if fault == 'INVALID_INPUT'
16
+ { valid: false }
17
+ else
18
+ error = (FAULTS[fault] || UnknownViesError).new(fault)
19
+ { error: error }
20
+ end
21
+ end
10
22
  end
11
23
 
12
24
  FAULTS = {
13
- "SERVICE_UNAVAILABLE" => ServiceUnavailable,
14
- "MS_UNAVAILABLE" => MemberStateUnavailable,
15
- "INVALID_REQUESTER_INFO" => InvalidRequester,
16
- "TIMEOUT" => Timeout,
17
- "VAT_BLOCKED" => BlockedError,
18
- "IP_BLOCKED" => BlockedError,
19
- "GLOBAL_MAX_CONCURRENT_REQ" => RateLimitError,
20
- "GLOBAL_MAX_CONCURRENT_REQ_TIME" => RateLimitError,
21
- "MS_MAX_CONCURRENT_REQ" => RateLimitError,
22
- "MS_MAX_CONCURRENT_REQ_TIME" => RateLimitError
23
- }
24
-
25
- def self.fault_to_error(fault)
26
- (FAULTS[fault] || UnknownViesError).new(fault)
27
- end
25
+ 'SERVICE_UNAVAILABLE' => ServiceUnavailable,
26
+ 'MS_UNAVAILABLE' => MemberStateUnavailable,
27
+ 'INVALID_REQUESTER_INFO' => InvalidRequester,
28
+ 'TIMEOUT' => Timeout,
29
+ 'VAT_BLOCKED' => BlockedError,
30
+ 'IP_BLOCKED' => BlockedError,
31
+ 'GLOBAL_MAX_CONCURRENT_REQ' => RateLimitError,
32
+ 'GLOBAL_MAX_CONCURRENT_REQ_TIME' => RateLimitError,
33
+ 'MS_MAX_CONCURRENT_REQ' => RateLimitError,
34
+ 'MS_MAX_CONCURRENT_REQ_TIME' => RateLimitError
35
+ }.freeze
28
36
  end
29
37
  end
30
- end
38
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'savon'
2
4
 
3
5
  class Valvat
@@ -12,13 +14,11 @@ class Valvat
12
14
  end
13
15
 
14
16
  def perform
15
- begin
16
- Response.new(
17
- client.call(action, message: message, message_tag: message_tag)
18
- )
19
- rescue Savon::SOAPFault => fault
20
- Fault.new(fault)
21
- end
17
+ Response.new(
18
+ client.call(action, message: message, message_tag: message_tag, soap_action: nil)
19
+ )
20
+ rescue Savon::SOAPFault, Savon::HTTPError, Savon::UnknownOperationError => e
21
+ Fault.new(e)
22
22
  end
23
23
 
24
24
  private
@@ -30,14 +30,10 @@ class Valvat
30
30
  end
31
31
 
32
32
  def message
33
- {
34
- country_code: @vat.vat_country_code,
35
- vat_number: @vat.to_s_wo_country
36
- }.merge(@requester ? {
37
- requester_country_code: @requester.vat_country_code,
38
- requester_vat_number: @requester.to_s_wo_country
39
- } : {}
40
- )
33
+ add_requester({
34
+ country_code: @vat.vat_country_code,
35
+ vat_number: @vat.to_s_wo_country
36
+ })
41
37
  end
42
38
 
43
39
  def message_tag
@@ -47,6 +43,15 @@ class Valvat
47
43
  def action
48
44
  @requester ? :check_vat_approx : :check_vat
49
45
  end
46
+
47
+ def add_requester(message)
48
+ return message unless @requester
49
+
50
+ message[:requester_country_code] = @requester.vat_country_code
51
+ message[:requester_vat_number] = @requester.to_s_wo_country
52
+
53
+ message
54
+ end
50
55
  end
51
56
  end
52
- end
57
+ end