Skip to content

Commit

Permalink
added IBAN/BIC validation, kontoapi-ruby >= 0.2.0 from here
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanmartin committed Apr 8, 2012
1 parent f29d436 commit 54440d6
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
8 changes: 4 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -82,15 +82,15 @@ 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

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)
18 changes: 18 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.3
0.2.0
12 changes: 6 additions & 6 deletions kontoapi-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]"
s.extra_rdoc_files = [
"LICENSE",
Expand Down Expand Up @@ -51,22 +51,22 @@ Gem::Specification.new do |s|

if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<rails>, [">= 3.0.0"])
s.add_runtime_dependency(%q<kontoapi-ruby>, [">= 0"])
s.add_runtime_dependency(%q<kontoapi-ruby>, [">= 0.2.0"])
s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
s.add_development_dependency(%q<rcov>, [">= 0"])
else
s.add_dependency(%q<rails>, [">= 3.0.0"])
s.add_dependency(%q<kontoapi-ruby>, [">= 0"])
s.add_dependency(%q<kontoapi-ruby>, [">= 0.2.0"])
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
s.add_dependency(%q<rcov>, [">= 0"])
end
else
s.add_dependency(%q<rails>, [">= 3.0.0"])
s.add_dependency(%q<kontoapi-ruby>, [">= 0"])
s.add_dependency(%q<kontoapi-ruby>, [">= 0.2.0"])
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
Expand Down
40 changes: 40 additions & 0 deletions lib/kontoapi-rails/orm/active_record_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/kontoapi-rails/validators/bank_account_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 54440d6

Please sign in to comment.