From 54440d6191655a672c91aadb0c02eca9e32abb30 Mon Sep 17 00:00:00 2001 From: Jan Schwenzien Date: Sun, 8 Apr 2012 18:40:07 +0200 Subject: [PATCH] added IBAN/BIC validation, kontoapi-ruby >= 0.2.0 from here --- Gemfile | 2 +- Gemfile.lock | 8 ++-- README.markdown | 18 +++++++++ VERSION | 2 +- kontoapi-rails.gemspec | 12 +++--- .../orm/active_record_extension.rb | 40 +++++++++++++++++++ .../validators/bank_account_validator.rb | 2 +- 7 files changed, 71 insertions(+), 13 deletions(-) diff --git a/Gemfile b/Gemfile index a081077..4488a96 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,7 @@ source "http://rubygems.org" gem "rails", ">= 3.0.0" -gem 'kontoapi-ruby' +gem 'kontoapi-ruby', ">= 0.2.0" group :development do gem "rspec", "~> 2.3.0" diff --git a/Gemfile.lock b/Gemfile.lock index 5768d3d..e9da158 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -28,7 +28,7 @@ GEM activemodel (= 3.0.7) activesupport (= 3.0.7) activesupport (3.0.7) - addressable (2.2.5) + addressable (2.2.7) arel (2.0.9) builder (2.1.2) diff-lcs (1.1.2) @@ -40,7 +40,7 @@ GEM bundler (~> 1.0.0) git (>= 1.2.5) rake - kontoapi-ruby (0.1.0) + kontoapi-ruby (0.2.0) addressable yajl-ruby mail (2.2.17) @@ -82,7 +82,7 @@ GEM treetop (1.4.9) polyglot (>= 0.3.1) tzinfo (0.3.26) - yajl-ruby (0.8.2) + yajl-ruby (1.1.0) PLATFORMS ruby @@ -90,7 +90,7 @@ PLATFORMS DEPENDENCIES bundler (~> 1.0.0) jeweler (~> 1.5.2) - kontoapi-ruby + kontoapi-ruby (>= 0.2.0) rails (>= 3.0.0) rcov rspec (~> 2.3.0) diff --git a/README.markdown b/README.markdown index f1fa5c3..dff1675 100644 --- a/README.markdown +++ b/README.markdown @@ -43,6 +43,24 @@ Then, in one of the models you want to validate bank account data with: # :invalid <-- if it is invalid # :timeout <-- if :on_timeout is set to :fail and the api call timed out validates_account_data + + # IBAN validation + # Check if the given IBAN is valid + # Takes any of the following options (the defaults are shown here): + # :allow_nil => true, <-- don't validate if nil + # :on_timeout => :ignore <-- do nothing if a timeout occurs, others: + # :fail <-- throw a validation error + # :retry <-- (not supported yet) + validates_iban :iban + + # BIC validation + # Check if the given BIC exists + # Takes any of the following options (the defaults are shown here): + # :allow_nil => true, <-- don't validate if nil + # :on_timeout => :ignore <-- do nothing if a timeout occurs, others: + # :fail <-- throw a validation error + # :retry <-- (not supported yet) + validates_bic :bic end diff --git a/VERSION b/VERSION index 6812f81..341cf11 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.0.3 \ No newline at end of file +0.2.0 \ No newline at end of file diff --git a/kontoapi-rails.gemspec b/kontoapi-rails.gemspec index 9b84b56..f5f0335 100644 --- a/kontoapi-rails.gemspec +++ b/kontoapi-rails.gemspec @@ -5,12 +5,12 @@ Gem::Specification.new do |s| s.name = "kontoapi-rails" - s.version = "0.0.3" + s.version = "0.2.0" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Jan Schwenzien"] - s.date = "2012-04-05" - s.description = "This library is a wrapper for the Konto API (https://www.kontoapi.de/). It provides a validation method for models that checks if a given account number and bank code represent a valid combination." + s.date = "2012-04-08" + s.description = "This library is a wrapper for the Konto API (https://www.kontoapi.de/). It provides a validation method for models that checks if a given account number and bank code represent a valid combination, autocompletes bank names and validates IBAN and BIC codes." s.email = "jan@schwenzien.org" s.extra_rdoc_files = [ "LICENSE", @@ -51,14 +51,14 @@ Gem::Specification.new do |s| if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then s.add_runtime_dependency(%q, [">= 3.0.0"]) - s.add_runtime_dependency(%q, [">= 0"]) + s.add_runtime_dependency(%q, [">= 0.2.0"]) s.add_development_dependency(%q, ["~> 2.3.0"]) s.add_development_dependency(%q, ["~> 1.0.0"]) s.add_development_dependency(%q, ["~> 1.5.2"]) s.add_development_dependency(%q, [">= 0"]) else s.add_dependency(%q, [">= 3.0.0"]) - s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0.2.0"]) s.add_dependency(%q, ["~> 2.3.0"]) s.add_dependency(%q, ["~> 1.0.0"]) s.add_dependency(%q, ["~> 1.5.2"]) @@ -66,7 +66,7 @@ Gem::Specification.new do |s| end else s.add_dependency(%q, [">= 3.0.0"]) - s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0.2.0"]) s.add_dependency(%q, ["~> 2.3.0"]) s.add_dependency(%q, ["~> 1.0.0"]) s.add_dependency(%q, ["~> 1.5.2"]) diff --git a/lib/kontoapi-rails/orm/active_record_extension.rb b/lib/kontoapi-rails/orm/active_record_extension.rb index 32b9ea0..d9c10ce 100644 --- a/lib/kontoapi-rails/orm/active_record_extension.rb +++ b/lib/kontoapi-rails/orm/active_record_extension.rb @@ -42,6 +42,46 @@ def autocomplete_bank_name(options={}) end before_save :autocomplete_bank_name end + + def validates_iban(field, options={}) + options.symbolize_keys! + options.reverse_merge!( :allow_nil => true, :on_timeout => :ignore ) + define_method :iban_validation do + value = send(field) + return true if options[:allow_nil] && value.nil? + begin + record.errors.add(field, :invalid) unless KontoAPI::valid?( :iban => value ) + rescue Timeout::Error => ex + case options[:on_timeout] + when :fail + record.errors.add(field, :timeout) + when :ignore + # nop + end + end + end + validate :iban_validation + end + + def validates_bic(field, options={}) + options.symbolize_keys! + options.reverse_merge!( :allow_nil => true, :on_timeout => :ignore ) + define_method :bic_validation do + value = send(field) + return true if options[:allow_nil] && value.nil? + begin + record.errors.add(field, :invalid) unless KontoAPI::valid?( :bic => send(field) ) + rescue Timeout::Error => ex + case options[:on_timeout] + when :fail + record.errors.add(field, :timeout) + when :ignore + # nop + end + end + end + validate :bic_validation + end end end diff --git a/lib/kontoapi-rails/validators/bank_account_validator.rb b/lib/kontoapi-rails/validators/bank_account_validator.rb index c8e8fab..38070c7 100644 --- a/lib/kontoapi-rails/validators/bank_account_validator.rb +++ b/lib/kontoapi-rails/validators/bank_account_validator.rb @@ -7,7 +7,7 @@ def validate(record) account_number = record.send(:"#{options[:account_number_field]}") bank_code = record.send(:"#{options[:bank_code_field]}") return true if options[:allow_nil] && (account_number.nil? || bank_code.nil?) - record.errors.add(:"#{options[:account_number_field]}", :invalid) unless KontoAPI::valid?(account_number, bank_code) + record.errors.add(:"#{options[:account_number_field]}", :invalid) unless KontoAPI::valid?( :ktn => account_number, :blz => bank_code ) rescue Timeout::Error => ex case options[:on_timeout] when :fail