spanish_ccc_validator 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format nested
2
+ --color
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem 'activerecord', '~> 2.1'
data/README.rdoc ADDED
@@ -0,0 +1,33 @@
1
+ = Simple Spanish Account Number Validator
2
+
3
+
4
+ == SYNOPSIS
5
+
6
+ This gem provides a simple way to validate spanish bank account numbers.
7
+
8
+
9
+ == INSTALL
10
+
11
+ ==== Directly:
12
+
13
+ gem install spanish_ccc_validator
14
+
15
+ ==== With bundler:
16
+ In your Gemfile
17
+ gem "spanish_ccc_validator"
18
+ Then exec
19
+ bundle install
20
+
21
+ == USAGE
22
+ It provides only a fancy method
23
+ validate_spanish_ccc :account_number
24
+
25
+ It can be mixed with all options you're used to
26
+ validate_spanish_ccc :origin_account_number, :destination_account_number,
27
+ :message => "seems incorrect",
28
+ :if => lambda { |sell| sell.payment_method == 'transfer' }
29
+
30
+ == TODO
31
+ * Write specs
32
+
33
+ Copyright (c) 2012 Miguel Camba
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ require "spanish_ccc_validator/version"
2
+ require "spanish_ccc_validator/custom_validator"
3
+
4
+ module SpanishCccValidator
5
+
6
+ def self.included(base)
7
+ base.extend ClassMethods
8
+ end
9
+
10
+ module ClassMethods
11
+ def validate_spanish_ccc(*args)
12
+ config = { :message => 'is invalid', :on => :save }
13
+ config.update(args.pop) if args.last.is_a?(Hash)
14
+
15
+ validates_each(args, config) do |record, arg, value|
16
+ error_msg = config[:message].is_a?(Proc) ? config[:message].call : config[:message]
17
+ record.errors.add(arg, error_msg) unless CustomValidator.validate(value)
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ class ActiveRecord::Base
24
+ include SpanishCccValidator
25
+ end
@@ -0,0 +1,27 @@
1
+ module SpanishCccValidator
2
+ module CustomValidator
3
+ # Gets a string and extracts the number from it
4
+ # Example: canonize("1234-5678-90-3344556677") returns "12345678903344556677"
5
+ def self.canonize(str)
6
+ str.gsub(/\D/,'')
7
+ end
8
+
9
+ # Main algorithm
10
+ def self.calculate_digit(ary)
11
+ key = [1,2,4,8,5,10,9,7,3,6]
12
+ sumatory = 0
13
+ key.each_with_index { |number, index| sumatory += number * ary[index] }
14
+ result = 11 - (sumatory % 11)
15
+ result = 1 if result == 10
16
+ result = 0 if result == 11
17
+ result
18
+ end
19
+
20
+ # Validates size and checks control-digits corelation
21
+ def self.validate(str)
22
+ ary = canonize(str).split('').map(&:to_i)
23
+ return false unless ary.size == 20
24
+ (calculate_digit([0,0] + ary[0..7]) == ary[8]) && (calculate_digit(ary[10..19]) == ary[9])
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module SpanishCccValidator
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "spanish_ccc_validator/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "spanish_ccc_validator"
7
+ s.version = SpanishCccValidator::VERSION
8
+ s.authors = ["miguel.camba"]
9
+ s.email = ["miguel.camba@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Spanish Account Number Validator}
12
+ s.description = %q{Provide a simple way to validate spanish ccc numbers}
13
+
14
+ s.rubyforge_project = "spanish_ccc_validator"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
@@ -0,0 +1,31 @@
1
+ require File.expand_path("./spec") + '/spec_helper.rb'
2
+
3
+ describe SpanishCccValidator do
4
+ describe SpanishCccValidator::CustomValidator do
5
+ subject{ SpanishCccValidator::CustomValidator }
6
+ describe '#canonize' do
7
+ it "cleans the string till it is just numbers" do
8
+ subject.canonize('1234-5678-91-1245678901').should == '12345678911245678901'
9
+ subject.canonize('Nº 1234/5678/91/1245678901').should == '12345678911245678901'
10
+ end
11
+ end
12
+
13
+ describe '#calculate_digit' do
14
+ it 'calculates que control digit for any array of 10 integers' do
15
+ subject.calculate_digit([0,0,2,0,9,6,0,4,7,5]).should == 6
16
+ subject.calculate_digit([0,2,0,0,0,2,4,0,7,3]).should == 0
17
+ subject.calculate_digit([1,2,3,4,5,6,7,8,9,9]).should_not == 9
18
+ end
19
+ end
20
+
21
+ describe '#validate' do
22
+ it 'should return true it 8th digit validates bank and office and 9th digit validates account' do
23
+ subject.validate('2096/0475/60/0200024073').should be_true
24
+ subject.validate('296/0475/60/020002073').should be_false
25
+ subject.validate('0002-4073-40-0200024073').should be_true
26
+ subject.validate('0002-4073-40-0201024073').should be_false
27
+ subject.validate('Cuenta: 00024073400200024073').should be_true
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,4 @@
1
+ require 'rspec'
2
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
3
+ require 'active_record'
4
+ require 'spanish_ccc_validator'
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spanish_ccc_validator
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - miguel.camba
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-01-18 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: Provide a simple way to validate spanish ccc numbers
36
+ email:
37
+ - miguel.camba@gmail.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - .gitignore
46
+ - .rspec
47
+ - Gemfile
48
+ - README.rdoc
49
+ - Rakefile
50
+ - lib/spanish_ccc_validator.rb
51
+ - lib/spanish_ccc_validator/custom_validator.rb
52
+ - lib/spanish_ccc_validator/version.rb
53
+ - spanish_ccc_validator.gemspec
54
+ - spec/spanish_ccc_validator_spec.rb
55
+ - spec/spec_helper.rb
56
+ has_rdoc: true
57
+ homepage: ""
58
+ licenses: []
59
+
60
+ post_install_message:
61
+ rdoc_options: []
62
+
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ hash: 3
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ requirements: []
84
+
85
+ rubyforge_project: spanish_ccc_validator
86
+ rubygems_version: 1.6.2
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Spanish Account Number Validator
90
+ test_files:
91
+ - spec/spanish_ccc_validator_spec.rb
92
+ - spec/spec_helper.rb