siret 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/LICENSE +19 -0
- data/README +4 -0
- data/Rakefile +40 -0
- data/VERSION +1 -0
- data/lib/siret.rb +51 -0
- data/spec/siret_spec.rb +45 -0
- metadata +62 -0
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2010 Samuel Lebeau
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require "rake"
|
2
|
+
|
3
|
+
begin
|
4
|
+
require "jeweler"
|
5
|
+
Jeweler::Tasks.new do |gemspec|
|
6
|
+
gemspec.name = "siret"
|
7
|
+
gemspec.summary = "Generate and validate French SIRET numbers (enterprises identifier)"
|
8
|
+
gemspec.email = "samuel.lebeau@gmail.com"
|
9
|
+
gemspec.homepage = "http://github.com/samleb/siret"
|
10
|
+
gemspec.author = "Samuel Lebeau"
|
11
|
+
end
|
12
|
+
rescue LoadError
|
13
|
+
puts "Jeweler not available. Install it with: sudo gem install jeweler"
|
14
|
+
end
|
15
|
+
|
16
|
+
begin
|
17
|
+
require "spec/rake/spectask"
|
18
|
+
Spec::Rake::SpecTask.new
|
19
|
+
rescue LoadError
|
20
|
+
desc "Run specs"
|
21
|
+
task :spec do
|
22
|
+
abort "RSpec is not available. Install it with: sudo gem install spec"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
desc "Analyze code coverage with specs"
|
27
|
+
begin
|
28
|
+
require "rcov/rcovtask"
|
29
|
+
Rcov::RcovTask.new("coverage") do |test|
|
30
|
+
test.libs << "spec"
|
31
|
+
test.test_files = FileList["spec/siret_spec.rb"]
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
rescue LoadError
|
35
|
+
task :coverage do
|
36
|
+
abort "RCov is not available. Install it with: sudo gem install rcov"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
task :default => :coverage
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/siret.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# French SIRET numbers validation and generation as described here:
|
2
|
+
# http://fr.wikipedia.org/wiki/Système_d’identification_du_répertoire_des_établissements#Calcul_et_validit.C3.A9_d.27un_num.C3.A9ro_SIRET
|
3
|
+
module Siret
|
4
|
+
extend self
|
5
|
+
|
6
|
+
REGEXP = /^\d{14}$/
|
7
|
+
SIREN_PATTERN = "%012d".freeze
|
8
|
+
SIREN_MAX = 10 ** 11
|
9
|
+
|
10
|
+
def checksum(siret)
|
11
|
+
result = 0
|
12
|
+
digits(siret).each_slice(2) do |a, b|
|
13
|
+
result += sum(digits(a * 2)) + b
|
14
|
+
end
|
15
|
+
result
|
16
|
+
end
|
17
|
+
|
18
|
+
def valid?(siret)
|
19
|
+
if siret.to_s =~ REGEXP
|
20
|
+
(checksum(siret) % 10).zero?
|
21
|
+
else
|
22
|
+
false
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def generate
|
27
|
+
siren = generate_siren
|
28
|
+
nic = generate_nic(siren)
|
29
|
+
"#{siren}#{nic}"
|
30
|
+
end
|
31
|
+
|
32
|
+
def generate_siren
|
33
|
+
SIREN_PATTERN % rand(SIREN_MAX)
|
34
|
+
end
|
35
|
+
|
36
|
+
def generate_nic(siren)
|
37
|
+
rest = 10 - checksum(siren) % 10
|
38
|
+
a = rest / 3
|
39
|
+
b = rest > 2 ? rest - 2 * a : rest
|
40
|
+
"#{a}#{b}"
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
def digits(number)
|
45
|
+
number.to_s.split("").collect { |digit| digit.to_i }
|
46
|
+
end
|
47
|
+
|
48
|
+
def sum(array)
|
49
|
+
array.inject(0) { |result, n| result + n }
|
50
|
+
end
|
51
|
+
end
|
data/spec/siret_spec.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'spec'
|
2
|
+
require 'spec/autorun'
|
3
|
+
|
4
|
+
require File.dirname(__FILE__) + '/../lib/siret'
|
5
|
+
|
6
|
+
describe Siret do
|
7
|
+
describe ".checksum" do
|
8
|
+
it "should convert the given argument to a string" do
|
9
|
+
(arg = mock).should_receive(:to_s).and_return ""
|
10
|
+
Siret.checksum(arg)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should compute Luhn SIRET checksum" do
|
14
|
+
Siret.checksum(73282932000074).should == 50
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe ".valid?" do
|
19
|
+
it "should convert the given argument to a string" do
|
20
|
+
(arg = mock).should_receive(:to_s).and_return ""
|
21
|
+
Siret.valid?(arg)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should return `true` given a valid SIRET number" do
|
25
|
+
Siret.valid?(73282932000074).should be_true
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should return `false` given an invalid SIRET number" do
|
29
|
+
Siret.valid?(0).should be_false
|
30
|
+
Siret.valid?("a" * 14).should be_false
|
31
|
+
Siret.valid?(73282932000075).should be_false
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe ".generate" do
|
36
|
+
it "should return a valid SIRET number" do
|
37
|
+
Siret.valid?(Siret.generate).should be_true
|
38
|
+
end
|
39
|
+
|
40
|
+
# If this example fails, go play lottery, or not...
|
41
|
+
it "should generate different (random) SIRET numbers" do
|
42
|
+
Siret.generate.should_not == Siret.generate
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: siret
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Samuel Lebeau
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-25 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: samuel.lebeau@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README
|
25
|
+
files:
|
26
|
+
- .gitignore
|
27
|
+
- LICENSE
|
28
|
+
- README
|
29
|
+
- Rakefile
|
30
|
+
- VERSION
|
31
|
+
- lib/siret.rb
|
32
|
+
- spec/siret_spec.rb
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: http://github.com/samleb/siret
|
35
|
+
licenses: []
|
36
|
+
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options:
|
39
|
+
- --charset=UTF-8
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
version:
|
54
|
+
requirements: []
|
55
|
+
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 1.3.5
|
58
|
+
signing_key:
|
59
|
+
specification_version: 3
|
60
|
+
summary: Generate and validate French SIRET numbers (enterprises identifier)
|
61
|
+
test_files:
|
62
|
+
- spec/siret_spec.rb
|