vat-siren-siret 0.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.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.rubocop.yml +7 -0
- data/.travis.yml +10 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +107 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/vat/siren/siret.rb +1 -0
- data/lib/vss.rb +120 -0
- data/vat-siren-siret.gemspec +28 -0
- metadata +127 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: aae81bf8365a335292e5b7f2682698e7c74621f1
|
4
|
+
data.tar.gz: 227938495503bac7ba772247e0e835afab3c315e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cd52c2fd8285ca55c81e5c30b3bc73737c993c8cf7a0e57c24d2ae3012ffc77aa1a2db5ad2b60b38e7d947279c2ddf103143d675b529cb10f172a3be4a445131
|
7
|
+
data.tar.gz: 626b8257f2b9123646779e52b1b5d3743e9f67410253b6d2f2be24d9a340bb47c469b6b0ec5a52aa3bea9e9cb73ac2d20ea97aba109b5734d333e9a754bf4b49
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 MathieuDerelle
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
# vat-siren-siret
|
2
|
+
|
3
|
+
[](https://travis-ci.org/MathieuDerelle/vat-siren-siret)
|
4
|
+
[](https://coveralls.io/github/MathieuDerelle/vat-siren-siret)
|
5
|
+
[](https://badge.fury.io/rb/vat-siren-siret)
|
6
|
+
[](https://github.com/MathieuDerelle/vat-siren-siret/blob/master/LICENSE.txt)
|
7
|
+
[](https://github.com/MathieuDerelle/vat-siren-siret/pulls)
|
8
|
+
|
9
|
+
This library allows to check french VAT, SIRET and SIREN, to generate french VAT, SIRET and SIREN and to generate VAT or SIREN from SIRET, SIREN and VAT.
|
10
|
+
|
11
|
+
It has been largely inspired by the [eponymous JS library](https://github.com/jbdemonte/vat-siren-siret) and [the ruby siret gem](https://github.com/samleb/siret)
|
12
|
+
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem 'vat-siren-siret', '~> 0.1'
|
20
|
+
```
|
21
|
+
|
22
|
+
And then execute:
|
23
|
+
|
24
|
+
$ bundle
|
25
|
+
|
26
|
+
Or install it yourself as:
|
27
|
+
|
28
|
+
$ gem install vat-siren-siret
|
29
|
+
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
```
|
34
|
+
// Check string
|
35
|
+
Vss.siren?('813454717') // true
|
36
|
+
Vss.siret?('81345471700014') // true
|
37
|
+
Vss.vat?('FR42813454717') // true
|
38
|
+
|
39
|
+
// Generate VAT from SIREN / SIRET
|
40
|
+
Vss.to_vat('813454717') // FR42813454717
|
41
|
+
Vss.to_vat('81345471700014') // FR42813454717
|
42
|
+
|
43
|
+
// Generate SIREN from SIRET / VAT
|
44
|
+
Vss.to_siren('81345471700014') // 813454717
|
45
|
+
Vss.to_siren('FR42813454717') // 813454717
|
46
|
+
|
47
|
+
// Generate SIREN / SIRET / VAT
|
48
|
+
Vss.generate_siren // 022676134
|
49
|
+
Vss.generate_siret // 07577658981212
|
50
|
+
Vss.generate_vat // FR60087471512
|
51
|
+
```
|
52
|
+
|
53
|
+
|
54
|
+
## API
|
55
|
+
|
56
|
+
### Vss.siren?(value)
|
57
|
+
|
58
|
+
Evaluate if the `value` is a SIREN and return a `boolean`.
|
59
|
+
|
60
|
+
### Vss.siret?(value)
|
61
|
+
|
62
|
+
Evaluate if the `value` is a SIRET and return a `boolean`.
|
63
|
+
|
64
|
+
### Vss.vat?(value)
|
65
|
+
|
66
|
+
Evaluate if the `value` is a french VAT and return a `boolean`.
|
67
|
+
|
68
|
+
### Vss.to_vat(value)
|
69
|
+
|
70
|
+
Generate the french VAT from a SIREN or a SIRET and return a `string`
|
71
|
+
Return `false` when value is neither a SIREN, SIRET or VAT.
|
72
|
+
Return `value` if it already is a VAT.
|
73
|
+
|
74
|
+
### Vss.to_siren(value)
|
75
|
+
|
76
|
+
Generate the SIREN from a VAT or a SIRET and return a `string`
|
77
|
+
Return `false` when value is neither a SIREN, SIRET or VAT.
|
78
|
+
Return `value` if it already is a SIREN.
|
79
|
+
|
80
|
+
### Vss.generate_siren
|
81
|
+
|
82
|
+
Generate a random SIREN passing the luhn check.
|
83
|
+
|
84
|
+
### Vss.generate_siret
|
85
|
+
|
86
|
+
Generate a random SIRET passing the luhn check.
|
87
|
+
|
88
|
+
### Vss.generate_vat
|
89
|
+
|
90
|
+
Generate a random VAT passing the luhn check.
|
91
|
+
|
92
|
+
|
93
|
+
## Development
|
94
|
+
|
95
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
96
|
+
|
97
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
98
|
+
|
99
|
+
|
100
|
+
## Contributing
|
101
|
+
|
102
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/MathieuDerelle/vat-siren-siret.
|
103
|
+
|
104
|
+
|
105
|
+
## License
|
106
|
+
|
107
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'vss'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'vss'
|
data/lib/vss.rb
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
# French VAT / SIRET / SIREN numbers validation and generation as described here:
|
2
|
+
# https://fr.wikipedia.org/wiki/Syst%C3%A8me_d%27identification_du_r%C3%A9pertoire_des_%C3%A9tablissements#Calcul_et_validit.C3.A9_d.27un_num.C3.A9ro_SIRET
|
3
|
+
module Vss
|
4
|
+
VERSION = '0.1.0'.freeze
|
5
|
+
|
6
|
+
REGEXPS = {
|
7
|
+
SIREN: /^\d{9}$/,
|
8
|
+
SIRET: /^\d{14}$/,
|
9
|
+
VAT: /^FR\d{11}$/,
|
10
|
+
formatSIREN: /(\d\d\d\B)/,
|
11
|
+
formatSIRET: /^(\d{3})(\d{3})(\d{3})(\d{5})$/,
|
12
|
+
formatVAT: /^([A-Z]{2})(\d{2})(\d{3})(\d{3})(\d{3})$/
|
13
|
+
}.freeze
|
14
|
+
|
15
|
+
# Return TRUE if the SIREN is valid
|
16
|
+
def self.siren?(siren)
|
17
|
+
siren.is_a?(String) && match?(REGEXPS[:SIREN], siren) && luhn_valid?(siren)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Return TRUE if the SIRET is valid
|
21
|
+
def self.siret?(siret)
|
22
|
+
siret.is_a?(String) && match?(REGEXPS[:SIRET], siret) && luhn_valid?(siret)
|
23
|
+
end
|
24
|
+
|
25
|
+
# Return TRUE if the VAT is valid
|
26
|
+
def self.vat?(vat)
|
27
|
+
vat.is_a?(String) && match?(REGEXPS[:VAT], vat) && valid_vat?(vat)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Convert a SIREN / SIRET / VAT to a SIREN
|
31
|
+
def self.to_siren(string)
|
32
|
+
return string if siren?(string)
|
33
|
+
return string[0..8] if siret?(string)
|
34
|
+
return string[4..-1] if vat?(string)
|
35
|
+
false
|
36
|
+
end
|
37
|
+
|
38
|
+
# Convert a SIREN / SIRET / VAT to a VAT
|
39
|
+
def self.to_vat(string)
|
40
|
+
return string if vat?(string)
|
41
|
+
siren = siret?(string) ? to_siren(string) : string
|
42
|
+
return false unless siren?(siren)
|
43
|
+
key = vat_key(siren)
|
44
|
+
"FR#{format('%02d', key)}#{siren}"
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.generate_siren
|
48
|
+
complete_for_luhn_checksum(generate_digits(7))
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.generate_siret
|
52
|
+
complete_for_luhn_checksum(generate_digits(12))
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.generate_vat
|
56
|
+
to_vat(generate_siren)
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.format_siren(siren)
|
60
|
+
raise 'Not a valid SIREN' unless siren?(siren)
|
61
|
+
siren.gsub(REGEXPS[:formatSIREN], '\1 ')
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.format_siret(siret)
|
65
|
+
raise 'Not a valid SIRET' unless siret?(siret)
|
66
|
+
siret.gsub(REGEXPS[:formatSIRET], '\1 \2 \3 \4')
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.format_vat(vat)
|
70
|
+
raise 'Not a valid VAT number' unless vat?(vat)
|
71
|
+
vat.gsub(REGEXPS[:formatVAT], '\1 \2 \3 \4 \5')
|
72
|
+
end
|
73
|
+
|
74
|
+
# private methods
|
75
|
+
|
76
|
+
def self.match?(re, str) # :nodoc:
|
77
|
+
return re.match?(str) if re.respond_to?(:match?) # ruby >= 2.4
|
78
|
+
!(re =~ str).nil?
|
79
|
+
end
|
80
|
+
|
81
|
+
def self.luhn_checksum(string) # :nodoc:
|
82
|
+
checksum = 0
|
83
|
+
string.each_char
|
84
|
+
.map(&:to_i)
|
85
|
+
.reverse
|
86
|
+
.each_with_index do |digit, idx|
|
87
|
+
tmp = idx.odd? ? digit * 2 : digit
|
88
|
+
tmp -= 9 if tmp > 9
|
89
|
+
checksum += tmp
|
90
|
+
end
|
91
|
+
checksum
|
92
|
+
end
|
93
|
+
|
94
|
+
# https://fr.wikipedia.org/wiki/Formule_de_Luhn
|
95
|
+
def self.luhn_valid?(string) # :nodoc:
|
96
|
+
(luhn_checksum(string) % 10).zero?
|
97
|
+
end
|
98
|
+
|
99
|
+
def self.valid_vat?(string) # :nodoc:
|
100
|
+
siren = string[4..-1]
|
101
|
+
vat_key(siren) == string[2..3].to_i && luhn_valid?(siren)
|
102
|
+
end
|
103
|
+
|
104
|
+
def self.vat_key(string) # :nodoc:
|
105
|
+
(12 + (3 * (string.to_i % 97))) % 97
|
106
|
+
end
|
107
|
+
|
108
|
+
def self.generate_digits(length)
|
109
|
+
format("%0#{length}d", rand(10**(length - 1)))
|
110
|
+
end
|
111
|
+
|
112
|
+
def self.complete_for_luhn_checksum(string)
|
113
|
+
rest = 10 - luhn_checksum(string) % 10
|
114
|
+
a = rest / 3
|
115
|
+
b = rest > 2 ? rest - 2 * a : rest
|
116
|
+
"#{string}#{a}#{b}"
|
117
|
+
end
|
118
|
+
|
119
|
+
private_class_method :match?, :luhn_checksum, :luhn_valid?, :valid_vat?, :vat_key, :generate_digits, :complete_for_luhn_checksum
|
120
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'vss'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'vat-siren-siret'
|
7
|
+
spec.version = Vss::VERSION
|
8
|
+
spec.authors = ['MathieuDerelle']
|
9
|
+
spec.email = ['mathieu@purchease.com']
|
10
|
+
|
11
|
+
spec.summary = 'Library to check or generate french VAT / SIRET / SIREN'
|
12
|
+
spec.description = 'Transposition in ruby of the eponymous JS library'
|
13
|
+
spec.homepage = 'https://github.com/MathieuDerelle/vat-siren-siret'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = 'exe'
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ['lib']
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.15'
|
24
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
25
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
26
|
+
spec.add_development_dependency 'rubocop'
|
27
|
+
spec.add_development_dependency 'coveralls'
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: vat-siren-siret
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- MathieuDerelle
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-02-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.15'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.15'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: coveralls
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Transposition in ruby of the eponymous JS library
|
84
|
+
email:
|
85
|
+
- mathieu@purchease.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".rubocop.yml"
|
93
|
+
- ".travis.yml"
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE.txt
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- bin/console
|
99
|
+
- bin/setup
|
100
|
+
- lib/vat/siren/siret.rb
|
101
|
+
- lib/vss.rb
|
102
|
+
- vat-siren-siret.gemspec
|
103
|
+
homepage: https://github.com/MathieuDerelle/vat-siren-siret
|
104
|
+
licenses:
|
105
|
+
- MIT
|
106
|
+
metadata: {}
|
107
|
+
post_install_message:
|
108
|
+
rdoc_options: []
|
109
|
+
require_paths:
|
110
|
+
- lib
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 2.4.5.1
|
124
|
+
signing_key:
|
125
|
+
specification_version: 4
|
126
|
+
summary: Library to check or generate french VAT / SIRET / SIREN
|
127
|
+
test_files: []
|