Module: ApiParticulier::Commons::Siret
- Defined in:
- lib/api_particulier/commons/siret.rb
Constant Summary collapse
- LA_POSTE_PATTERN =
/\A356000000\d{5}\z/.freeze
- DIGITS_14 =
/\A\d{14}\z/.freeze
Class Method Summary collapse
Class Method Details
.luhn_checksum(value) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/api_particulier/commons/siret.rb', line 27 def luhn_checksum(value) accum = 0 value.reverse.each_char.map(&:to_i).each_with_index do |digit, index| t = index.even? ? digit : digit * 2 t -= 9 if t >= 10 accum += t end accum end |
.valid?(value) ⇒ Boolean
12 13 14 15 16 17 18 |
# File 'lib/api_particulier/commons/siret.rb', line 12 def valid?(value) return false if value.nil? return false unless value.to_s.match?(DIGITS_14) return true if value.to_s.match?(LA_POSTE_PATTERN) (luhn_checksum(value.to_s) % 10).zero? end |
.validate!(value, parameter:) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/api_particulier/commons/siret.rb', line 20 def validate!(value, parameter:) return if valid?(value) raise InvalidSiretError, "#{parameter.inspect} must be a 14-digit SIRET passing the Luhn checksum (or a La Poste SIRET); got #{value.inspect}" end |