From cf342a229a59176e5e7502123b9e97423aadae31 Mon Sep 17 00:00:00 2001 From: Jan Schwenzien Date: Wed, 8 Mar 2017 17:16:59 +0100 Subject: [PATCH] 0.3.1 added validates_iban_and_bic --- README.markdown | 9 +++ Rakefile | 6 +- VERSION | 2 +- kontoapi-rails.gemspec | 68 +++++++++---------- .../orm/active_record_extension.rb | 33 ++++++++- 5 files changed, 77 insertions(+), 41 deletions(-) diff --git a/README.markdown b/README.markdown index a5a2ee5..c44234f 100644 --- a/README.markdown +++ b/README.markdown @@ -65,6 +65,15 @@ Then, in one of the models you want to validate bank account data with: # :fail <-- throw a validation error # :retry <-- (not supported yet) validates_bic :bic + + # Combined IBAN & BIC validation + # Check if the given BIC exists, the IBAN is valid and if they match + # Takes any of the following options (the defaults are shown here): + # :allow_nil => true, <-- don't validate if both are nil + # :on_timeout => :ignore <-- do nothing if a timeout occurs, others: + # :fail <-- throw a validation error + # :retry <-- (not supported yet) + validates_iban_and_bic :iban, :bic end diff --git a/Rakefile b/Rakefile index 3c51cc3..a0e808e 100644 --- a/Rakefile +++ b/Rakefile @@ -18,11 +18,7 @@ Jeweler::Tasks.new do |gem| gem.summary = %Q{A wrapper for the Konto API (https://www.kontoapi.de/) providing model validation.} gem.description = %Q{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.} gem.email = "jan@schwenzien.org" - gem.authors = ["Jan Schwenzien"] - # Include your dependencies below. Runtime dependencies are required when using your gem, - # and development dependencies are only needed for development (ie running rake tasks, tests, etc) - # gem.add_runtime_dependency 'jabber4r', '> 0.1' - # gem.add_development_dependency 'rspec', '> 1.2.3' + gem.authors = ["Jan Schwenzien", "Jiayi Zheng"] end Jeweler::RubygemsDotOrgTasks.new diff --git a/VERSION b/VERSION index 9325c3c..a2268e2 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.3.0 \ No newline at end of file +0.3.1 \ No newline at end of file diff --git a/kontoapi-rails.gemspec b/kontoapi-rails.gemspec index bebc226..43d93d6 100644 --- a/kontoapi-rails.gemspec +++ b/kontoapi-rails.gemspec @@ -2,18 +2,18 @@ # DO NOT EDIT THIS FILE DIRECTLY # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec' # -*- encoding: utf-8 -*- -# stub: kontoapi-rails 0.3.0 ruby lib +# stub: kontoapi-rails 0.3.1 ruby lib Gem::Specification.new do |s| - s.name = "kontoapi-rails".freeze - s.version = "0.3.0" + s.name = "kontoapi-rails" + s.version = "0.3.1" - s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version= - s.require_paths = ["lib".freeze] - s.authors = ["Jan Schwenzien".freeze] - s.date = "2016-12-01" - 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.".freeze - s.email = "jan@schwenzien.org".freeze + s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= + s.require_paths = ["lib"] + s.authors = ["Jan Schwenzien", "Jiayi Zheng"] + s.date = "2017-03-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." + s.email = "jan@schwenzien.org" s.extra_rdoc_files = [ "LICENSE", "README.markdown" @@ -38,39 +38,39 @@ Gem::Specification.new do |s| "spec/kontoapi-rails_spec.rb", "spec/spec_helper.rb" ] - s.homepage = "http://github.com/GeneralScripting/kontoapi-rails".freeze - s.licenses = ["MIT".freeze] - s.rubygems_version = "2.6.7".freeze - s.summary = "A wrapper for the Konto API (https://www.kontoapi.de/) providing model validation.".freeze + s.homepage = "http://github.com/GeneralScripting/kontoapi-rails" + s.licenses = ["MIT"] + s.rubygems_version = "2.4.8" + s.summary = "A wrapper for the Konto API (https://www.kontoapi.de/) providing model validation." if s.respond_to? :specification_version then s.specification_version = 4 if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then - s.add_runtime_dependency(%q.freeze, [">= 3.0.0"]) - s.add_runtime_dependency(%q.freeze, [">= 0.2.0"]) - s.add_development_dependency(%q.freeze, [">= 2.3.0"]) - s.add_development_dependency(%q.freeze, [">= 1.1.0"]) - s.add_development_dependency(%q.freeze, [">= 1.5.2"]) - s.add_development_dependency(%q.freeze, [">= 0"]) - s.add_development_dependency(%q.freeze, [">= 0"]) + s.add_runtime_dependency(%q, [">= 3.0.0"]) + s.add_runtime_dependency(%q, [">= 0.2.0"]) + s.add_development_dependency(%q, [">= 2.3.0"]) + s.add_development_dependency(%q, [">= 1.1.0"]) + s.add_development_dependency(%q, [">= 1.5.2"]) + s.add_development_dependency(%q, [">= 0"]) + s.add_development_dependency(%q, [">= 0"]) else - s.add_dependency(%q.freeze, [">= 3.0.0"]) - s.add_dependency(%q.freeze, [">= 0.2.0"]) - s.add_dependency(%q.freeze, [">= 2.3.0"]) - s.add_dependency(%q.freeze, [">= 1.1.0"]) - s.add_dependency(%q.freeze, [">= 1.5.2"]) - s.add_dependency(%q.freeze, [">= 0"]) - s.add_dependency(%q.freeze, [">= 0"]) + s.add_dependency(%q, [">= 3.0.0"]) + s.add_dependency(%q, [">= 0.2.0"]) + s.add_dependency(%q, [">= 2.3.0"]) + s.add_dependency(%q, [">= 1.1.0"]) + s.add_dependency(%q, [">= 1.5.2"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) end else - s.add_dependency(%q.freeze, [">= 3.0.0"]) - s.add_dependency(%q.freeze, [">= 0.2.0"]) - s.add_dependency(%q.freeze, [">= 2.3.0"]) - s.add_dependency(%q.freeze, [">= 1.1.0"]) - s.add_dependency(%q.freeze, [">= 1.5.2"]) - s.add_dependency(%q.freeze, [">= 0"]) - s.add_dependency(%q.freeze, [">= 0"]) + s.add_dependency(%q, [">= 3.0.0"]) + s.add_dependency(%q, [">= 0.2.0"]) + s.add_dependency(%q, [">= 2.3.0"]) + s.add_dependency(%q, [">= 1.1.0"]) + s.add_dependency(%q, [">= 1.5.2"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) end end diff --git a/lib/kontoapi-rails/orm/active_record_extension.rb b/lib/kontoapi-rails/orm/active_record_extension.rb index 7504458..15e5d91 100644 --- a/lib/kontoapi-rails/orm/active_record_extension.rb +++ b/lib/kontoapi-rails/orm/active_record_extension.rb @@ -80,7 +80,7 @@ def validates_bic(field, options={}) return true if respond_to?(:"encrypted_#{field}_changed?") && !send(:"encrypted_#{field}_changed?") return true if options[:allow_nil] && value.nil? begin - errors.add(field, :invalid) unless KontoAPI::valid?( :bic => send(field) ) + errors.add(field, :invalid) unless KontoAPI::valid?( :bic => value ) rescue Timeout::Error => ex case options[:on_timeout] when :fail @@ -92,6 +92,37 @@ def validates_bic(field, options={}) end validate :bic_validation end + + def validates_iban_and_bic(iban_field, bic_field, options={}) + return if KontoAPI::Config.disable_for.include?(::Rails.env) + options.symbolize_keys! + options.reverse_merge!( :allow_nil => true, :on_timeout => :ignore ) + define_method :iban_and_bic_validation do + iban_value = send(iban_field) + bic_value = send(bic_field) + return true if [iban_field, bic_field].all? do |field| + respond_to?(:"#{field}_changed?") && !send(:"#{field}_changed?") + end + return true if [iban_field, bic_field].all? do |field| + respond_to?(:"encrypted_#{field}_changed?") && !send(:"encrypted_#{field}_changed?") + end + return true if options[:allow_nil] && iban_value.nil? && bic_value.nil? + begin + unless KontoAPI::valid?( :iban => iban_value, :bic => bic_value ) + errors.add(iban_field, :invalid) + errors.add(bic_field, :invalid) + end + rescue Timeout::Error => ex + case options[:on_timeout] + when :fail + errors.add(field, :timeout) + when :ignore + # nop + end + end + end + validate :iban_and_bic_validation + end end end