diff --git a/.gitignore b/.gitignore index d87d4be..eba00d5 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ spec/reports test/tmp test/version_tmp tmp +gemfiles/*.lock diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..29bba75 --- /dev/null +++ b/.travis.yml @@ -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 diff --git a/Appraisals b/Appraisals new file mode 100644 index 0000000..bf15ae3 --- /dev/null +++ b/Appraisals @@ -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 diff --git a/Gemfile b/Gemfile index 76065bb..ad09323 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/README.md b/README.md index 417e160..8fa77e4 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/Rakefile b/Rakefile index 2995527..0fa5021 100644 --- a/Rakefile +++ b/Rakefile @@ -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 diff --git a/gemfiles/BIC_rails_3.gemfile b/gemfiles/BIC_rails_3.gemfile new file mode 100644 index 0000000..6fe29d2 --- /dev/null +++ b/gemfiles/BIC_rails_3.gemfile @@ -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=>"../" \ No newline at end of file diff --git a/gemfiles/BIC_rails_4.gemfile b/gemfiles/BIC_rails_4.gemfile new file mode 100644 index 0000000..fb31c50 --- /dev/null +++ b/gemfiles/BIC_rails_4.gemfile @@ -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=>"../" \ No newline at end of file diff --git a/gemfiles/non_BIC.gemfile b/gemfiles/non_BIC.gemfile new file mode 100644 index 0000000..e13f1c8 --- /dev/null +++ b/gemfiles/non_BIC.gemfile @@ -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=>"../" \ No newline at end of file diff --git a/iban_validation.gemspec b/iban_validation.gemspec index deaa745..57e2ead 100644 --- a/iban_validation.gemspec +++ b/iban_validation.gemspec @@ -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 diff --git a/lib/iban_validation.rb b/lib/iban_validation.rb index ff2dbe8..4973c3c 100644 --- a/lib/iban_validation.rb +++ b/lib/iban_validation.rb @@ -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] diff --git a/lib/iban_validation/known_iban_validator.rb b/lib/iban_validation/known_iban_validator.rb new file mode 100644 index 0000000..89b46ed --- /dev/null +++ b/lib/iban_validation/known_iban_validator.rb @@ -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 diff --git a/locales/de.yml b/locales/de.yml new file mode 100644 index 0000000..721ed8a --- /dev/null +++ b/locales/de.yml @@ -0,0 +1,4 @@ +de: + errors: + messages: + unknown_bank: enthaltene Bank ist unbekannt diff --git a/locales/en.yml b/locales/en.yml new file mode 100644 index 0000000..b944fa2 --- /dev/null +++ b/locales/en.yml @@ -0,0 +1,4 @@ +en: + errors: + messages: + unknown_bank: included bank is not known diff --git a/spec/iban_validation/iban_validator_spec.rb b/spec/iban_validation/iban_validator_spec.rb index 238e002..5d244fd 100644 --- a/spec/iban_validation/iban_validator_spec.rb +++ b/spec/iban_validation/iban_validator_spec.rb @@ -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 diff --git a/spec/iban_validation/known_iban_validator_spec.rb b/spec/iban_validation/known_iban_validator_spec.rb new file mode 100644 index 0000000..fccadc2 --- /dev/null +++ b/spec/iban_validation/known_iban_validator_spec.rb @@ -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