uy-rut 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ *.gem
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in uy-rut.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,27 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ uy-rut (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.2.4)
10
+ rake (10.1.0)
11
+ rspec (2.14.1)
12
+ rspec-core (~> 2.14.0)
13
+ rspec-expectations (~> 2.14.0)
14
+ rspec-mocks (~> 2.14.0)
15
+ rspec-core (2.14.5)
16
+ rspec-expectations (2.14.2)
17
+ diff-lcs (>= 1.1.3, < 2.0)
18
+ rspec-mocks (2.14.3)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ bundler (~> 1.3)
25
+ rake
26
+ rspec (~> 2.14.1)
27
+ uy-rut!
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Alfonso Cora
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.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Uy::Rut
2
+
3
+ This gem includes the necessary helper methods to validate Uruguayan company identification numbers ("Registro Único Tributario" or RUT).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'uy-rut'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install uy-rut
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ require 'uy/rut'
23
+
24
+ # to validate a number:
25
+ Uy::Rut.new('098699850070').valid?
26
+ # => true
27
+
28
+ # to generate a valid number for testing purposes:
29
+ Uy::Rut.new.rut
30
+ # => "051125770027"
31
+ ```
32
+
33
+ ## Contributing
34
+
35
+ If you want to contribute to this project, just fork it, make your changes, run the tests and create a pull request. Also, feel free to report issues on the [issues section](issues).
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'rspec/core/rake_task'
3
+
4
+ task default: :spec
5
+
6
+ RSpec::Core::RakeTask.new do |task|
7
+ task.rspec_opts = ["-c", "-f progress"]
8
+ task.pattern = 'spec/**/*_spec.rb'
9
+ end
data/lib/uy/rut.rb ADDED
@@ -0,0 +1,62 @@
1
+ module Uy
2
+ class Rut
3
+ FACTORS = [4,3,2,9,8,7,6,5,4,3,2]
4
+
5
+ attr_accessor :rut
6
+
7
+ def initialize(rut=nil)
8
+ @rut = rut || Rut.generate
9
+ end
10
+
11
+ def is_valid?
12
+ return false unless @rut =~ /\d{12}/
13
+ return false unless (1..21) === @rut[0..1].to_i
14
+ (2..7).each {|pos| return false unless (1..9) === @rut[pos].to_i}
15
+ return false unless @rut[8..9] == '00'
16
+
17
+ begin
18
+ return false unless Rut.calculate_check_digit(@rut) == @rut[11].to_i
19
+ rescue
20
+ return false
21
+ end
22
+
23
+ true
24
+ end
25
+
26
+ alias valid? is_valid?
27
+
28
+ protected
29
+
30
+ def self.calculate_check_digit(rut)
31
+ rut = rut.length == 12 ? rut.chop : rut
32
+ checksum =
33
+ rut
34
+ .chars
35
+ .to_a
36
+ .map(&:to_i)
37
+ .zip(FACTORS)
38
+ .map {|d| d.reduce(&:*)}
39
+ .reduce(:+)
40
+ check_digit = 11 - (checksum % 11)
41
+ check_digit = 0 if check_digit == 11
42
+ fail if check_digit == 10
43
+ check_digit
44
+ end
45
+
46
+ def self.generate
47
+ rut = sprintf('%02d', rand(1..21))
48
+ 6.times { rut += rand(1..9).to_s }
49
+ rut += '00'
50
+ rut += rand(0..9).to_s
51
+
52
+ begin
53
+ rut += calculate_check_digit(rut).to_s
54
+ rescue
55
+ # bad checksum, retry
56
+ return Rut.generate
57
+ end
58
+
59
+ rut
60
+ end
61
+ end
62
+ end
data/spec/rut_spec.rb ADDED
@@ -0,0 +1,25 @@
1
+ require 'uy/rut'
2
+
3
+ describe Uy::Rut do
4
+ describe 'validations' do
5
+ it { expect(Uy::Rut.new('')).not_to be_valid }
6
+ it { expect(Uy::Rut.new('asdf')).not_to be_valid }
7
+ it { expect(Uy::Rut.new('11222222008')).not_to be_valid }
8
+ it { expect(Uy::Rut.new('873332220087')).not_to be_valid }
9
+ it { expect(Uy::Rut.new('010111110022')).not_to be_valid }
10
+ it { expect(Uy::Rut.new('011011110029')).not_to be_valid }
11
+ it { expect(Uy::Rut.new('011101110028')).not_to be_valid }
12
+ it { expect(Uy::Rut.new('011110110027')).not_to be_valid }
13
+ it { expect(Uy::Rut.new('011111010026')).not_to be_valid }
14
+ it { expect(Uy::Rut.new('011111100025')).not_to be_valid }
15
+ it { expect(Uy::Rut.new('024445558811')).not_to be_valid }
16
+ it { expect(Uy::Rut.new('035557770088')).not_to be_valid }
17
+ it { expect(Uy::Rut.new('219223180010')).to be_valid }
18
+ end
19
+
20
+ describe 'generation' do
21
+ it 'generates valid numbers' do
22
+ 100.times { expect(Uy::Rut.new).to be_valid }
23
+ end
24
+ end
25
+ end
data/uy-rut.gemspec ADDED
@@ -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 = "uy-rut"
7
+ spec.version = '0.0.1'
8
+ spec.authors = ["Alfonso Cora"]
9
+ spec.email = ["acora6@gmail.com"]
10
+ spec.description = %q{Includes helper methods to validate Uruguayan company identification numbers (RUT)}
11
+ spec.summary = %q{Uy::Rut is a class that allows to validate RUT numbers or generate fake ones for testing purposes}
12
+ spec.homepage = ""
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 "rspec", "~> 2.14.1"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: uy-rut
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alfonso Cora
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 2.14.1
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.14.1
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Includes helper methods to validate Uruguayan company identification
63
+ numbers (RUT)
64
+ email:
65
+ - acora6@gmail.com
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - .gitignore
71
+ - .rspec
72
+ - Gemfile
73
+ - Gemfile.lock
74
+ - LICENSE.txt
75
+ - README.md
76
+ - Rakefile
77
+ - lib/uy/rut.rb
78
+ - spec/rut_spec.rb
79
+ - uy-rut.gemspec
80
+ homepage: ''
81
+ licenses:
82
+ - MIT
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 1.8.25
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: Uy::Rut is a class that allows to validate RUT numbers or generate fake ones
105
+ for testing purposes
106
+ test_files:
107
+ - spec/rut_spec.rb
108
+ has_rdoc: