Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ spec/reports
test/tmp
test/version_tmp
tmp
gemfiles/*.lock
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: ruby
rvm:
- 1.9.3
- 2.0.0
- ruby-head
- jruby-19mode
- jruby-head
- rbx-2.1.1
script: rspec
gemfile:
- gemfiles/non_BIC.gemfile
- gemfiles/BIC_rails_4.gemfile
- gemfiles/BIC_rails_3.gemfile
12 changes: 12 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
appraise 'non-BIC' do
end

appraise 'BIC rails 3' do
gem 'activemodel', '~>3.2.0'
gem 'banking_data', '~>0.5.0'
end

appraise 'BIC rails 4' do
gem 'activemodel', '~>4.0.0'
gem 'banking_data', '~>0.5.0'
end
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
source 'https://rubygems.org'

gem 'iban-tools', github: 'opahk/iban-tools', branch: 'banking_data'

# Specify your gem's dependencies in iban_validation.gemspec
gemspec
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,26 @@ Or install it yourself as:

## Usage

In your rails model
### Basic IBAN validator (format only)

In your rails model:

validates :iban_attribute, iban: true

This validates the format of the IBAN based on the specification for each
country.

### Known IBAN validator

In your rails model:

validates :iban_attribute, known_iban: true

This version additionally validates whether the bank code part of the IBAN
(Bankleitzahl, Bank Clearing Nummer) is known by comparing it to a list of
known bank codes. This list is provided by the `banking_data` gem and currently
only comprises DE, AT and CH.

## Contributing

1. Fork it
Expand Down
11 changes: 11 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
require "bundler/gem_tasks"
require 'appraisal'

gem 'rspec', '>= 1.2.4'
require 'rspec/core/rake_task'

task :default => :core

RSpec::Core::RakeTask.new(:core) do |spec|
spec.pattern = 'spec/**/*_spec.rb'
spec.rspec_opts = ['--backtrace']
end
9 changes: 9 additions & 0 deletions gemfiles/BIC_rails_3.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "iban-tools", :github=>"opahk/iban-tools", :branch=>"banking_data"
gem "activemodel", "~>3.2.0"
gem "banking_data", "~>0.5.0"

gemspec :path=>"../"
9 changes: 9 additions & 0 deletions gemfiles/BIC_rails_4.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "iban-tools", :github=>"opahk/iban-tools", :branch=>"banking_data"
gem "activemodel", "~>4.0.0"
gem "banking_data", "~>0.5.0"

gemspec :path=>"../"
7 changes: 7 additions & 0 deletions gemfiles/non_BIC.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "iban-tools", :github=>"opahk/iban-tools", :branch=>"banking_data"

gemspec :path=>"../"
8 changes: 7 additions & 1 deletion iban_validation.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ Gem::Specification.new do |gem|
gem.require_paths = ["lib"]

gem.add_dependency 'activemodel'
gem.add_dependency 'iban-tools'
#gem.add_dependency 'iban-tools'
gem.add_development_dependency 'rspec'
gem.add_development_dependency 'coveralls'
gem.add_development_dependency 'appraisal'
gem.add_development_dependency 'pry'
gem.add_development_dependency 'rubocop'

if ENV['RUBY_VERSION'] =~ /rbx/
gem.add_dependency 'rubysl'
gem.add_development_dependency 'rubinius-coverage'
end
end
3 changes: 3 additions & 0 deletions lib/iban_validation.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
require 'active_model'
require 'iban-tools'
require 'iban_validation/iban_validator'
require 'iban_validation/known_iban_validator'
require 'iban_validation/version'

module IbanValidation
end

ActiveModel::Validations.send(:include, IbanValidation)
I18n.load_path += Dir[File.expand_path(File.join(File.dirname(__FILE__),
'../locales', '*.yml')).to_s]
13 changes: 13 additions & 0 deletions lib/iban_validation/known_iban_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module IbanValidation
class KnownIbanValidator < ActiveModel::EachValidator
def validate_each(record, attribute, values)
if IBANTools::IBAN.valid? values
if IBANTools::IBAN.new(values).to_bic.nil?
record.errors.add attribute, :unknown_bank
end
else
record.errors.add attribute, :invalid
end
end
end
end
4 changes: 4 additions & 0 deletions locales/de.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
de:
errors:
messages:
unknown_bank: enthaltene Bank ist unbekannt
4 changes: 4 additions & 0 deletions locales/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
en:
errors:
messages:
unknown_bank: included bank is not known
7 changes: 6 additions & 1 deletion spec/iban_validation/iban_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ def initialize(iban)
expect(model).to be_invalid
end

it 'is invalid' do
it 'is valid' do
model = Model.new 'GB82WEST12345698765432'
expect(model).to be_valid
end

it 'is valid' do
model = Model.new 'DE89370400440532013000'
expect(model).to be_valid
end
end
end
41 changes: 41 additions & 0 deletions spec/iban_validation/known_iban_validator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'spec_helper'

module IbanValidation
describe KnownIbanValidator do
begin
require 'banking_data'

class KnownModel
include ActiveModel::Validations
attr_accessor :iban
validates :iban, known_iban: true

def initialize(iban)
@iban = iban
end
end

it 'is invalid' do
model = KnownModel.new 'DE'
expect(model).to be_invalid
end

it 'is invalid' do
model = KnownModel.new 'GB82WEST12345698765432'
expect(model).to be_invalid
end

it 'is valid' do
model = KnownModel.new 'DE89370400440532013000'
expect(model).to be_valid
end

it 'is valid' do
model = KnownModel.new 'DE15763500000036109724'
expect(model).to be_valid
end
rescue LoadError
pending
end
end
end