orgnummer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b34f213746505325edcccd1c28a656d52389ecf7
4
+ data.tar.gz: 2707c5f86bd4b8eb57b05e457e5afea69a97348e
5
+ SHA512:
6
+ metadata.gz: 4771e3605afc7a8caa2b08f7503ee22a6818287dc801ebc9655af4e972a6317bd66c1cd7d1dcb729b5d7a9876d6a59bc2dac950bff81cd607f4398e565d3bc3d
7
+ data.tar.gz: b9693e692a7854a5eeb01deff0af3975f37a810f271f24c1d88ed2283522acf0426ecc9c6f8f0d0d7d9ae1c152ee9e2e0da25d4350142a9dae5247fd0e51a84b
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1 @@
1
+ ruby-2.1.4
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in orgnummer.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Jonas
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Orgnummer
2
+
3
+ Util for creation and validation of the swedish organisationsnummer
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'orgnummer'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install orgnummer
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,41 @@
1
+ #encoding: utf-8
2
+
3
+ class Orgnummer
4
+ attr_reader :number
5
+
6
+ def initialize(number)
7
+ @number = number.to_s
8
+ end
9
+
10
+ def valid?
11
+ valid = false
12
+
13
+ if !@number.nil? && !@number.empty?
14
+ # hyphen is ok, but remove it to pass validation algorithm
15
+ @number = @number.gsub(/-/, '')
16
+ # strip, we are kind and helpful
17
+ @number = @number.strip
18
+
19
+ if (@number =~/\A\d{10}\z/) == 0
20
+ multiplier = 2
21
+ sum = 0
22
+
23
+ @number[0...9].each_char do |n|
24
+ part = (n.to_i * multiplier).to_s
25
+ sum += part.length == 1 ? part.to_i : part[0].to_i + part[1].to_i
26
+ multiplier = multiplier == 1 ? 2 : 1
27
+ end
28
+
29
+ checksum = (10 - sum.modulo(10)).modulo(10)
30
+
31
+ valid = checksum == @number[-1, 1].to_i
32
+ end
33
+ end
34
+ valid
35
+ end
36
+
37
+ def to_s
38
+ self.valid? ? @number[0..5] + '-' + @number[6..10] : "Not a valid number: #{@number}"
39
+ end
40
+
41
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'orgnummer'
7
+ spec.version = '0.0.1'
8
+ spec.authors = ['Jonas']
9
+ spec.email = ['jonas.lundstrom@mirendo.se']
10
+ spec.description = %q{Create and validate format of the swedish organisationsnummer}
11
+ spec.summary = %q{Util for creation and validation of the swedish organisationsnummer}
12
+ spec.homepage = 'http://www.mirendo.se'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_development_dependency 'bundler', '~> 1.3'
21
+ spec.add_development_dependency 'rake'
22
+ spec.add_development_dependency 'rspec'
23
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+ require 'orgnummer'
3
+
4
+
5
+ describe Orgnummer do
6
+ describe 'initialize' do
7
+ it 'can be intialized with (almost) anything' do
8
+ expect { Orgnummer.new(900101) }.not_to raise_error
9
+ expect { Orgnummer.new(nil) }.not_to raise_error
10
+ expect { Orgnummer.new('abs') }.not_to raise_error
11
+ end
12
+ end
13
+
14
+ describe 'behavior' do
15
+
16
+ let(:short) { Orgnummer.new(12) }
17
+ let(:short_s) { Orgnummer.new('12') }
18
+ let(:bad_end) { Orgnummer.new(5568610828) }
19
+
20
+ let(:ab) { Orgnummer.new(5568610827) }
21
+ let(:ab_s) { Orgnummer.new('5568610827') }
22
+ let(:ab_s_with_hyphen) { Orgnummer.new('556861-0827') }
23
+ let(:ab_s_with_spaces) { Orgnummer.new(' 556861-0827 ') }
24
+
25
+
26
+ describe 'validate' do
27
+
28
+ it 'validates false for malformed numbers' do
29
+ expect(short.valid?).to be false
30
+ expect(short_s.valid?).to be false
31
+ expect(bad_end.valid?).to be false
32
+ end
33
+
34
+ it 'validates true for correct numbers' do
35
+ expect(ab.valid?).to be true
36
+ expect(ab_s.valid?).to be true
37
+ expect(ab_s_with_hyphen.valid?).to be true
38
+ expect(ab_s_with_spaces.valid?).to be true
39
+ end
40
+ end
41
+
42
+ describe 'to string' do
43
+ it 'adds the hyphen' do
44
+ expect(ab.to_s).to eq '556861-0827'
45
+ expect(ab_s.to_s).to eq '556861-0827'
46
+ end
47
+
48
+ it 'gives a hint of validity' do
49
+ expect(short.to_s).to eq 'Not a valid number: 12'
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,4 @@
1
+ $: << File.join(File.dirname(__FILE__), '..', 'lib')
2
+
3
+ RSpec.configure do |config|
4
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: orgnummer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jonas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-20 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Create and validate format of the swedish organisationsnummer
56
+ email:
57
+ - jonas.lundstrom@mirendo.se
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".ruby-version"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/orgnummer.rb
69
+ - orgnummer.gemspec
70
+ - spec/lib/orgnummer_spec.rb
71
+ - spec/spec_helper.rb
72
+ homepage: http://www.mirendo.se
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.4.6
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Util for creation and validation of the swedish organisationsnummer
96
+ test_files:
97
+ - spec/lib/orgnummer_spec.rb
98
+ - spec/spec_helper.rb