README.md in czech_bank_account-0.1.1 vs README.md in czech_bank_account-1.0.0

- old
+ new

@@ -1,10 +1,9 @@ -# CzechBankAccount +# CzechBankAccount [![Build Status](https://www.travis-ci.org/Masa331/czech_bank_account.svg?branch=master)](https://www.travis-ci.org/Masa331/czech_bank_account) -Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/czech_bank_account`. To experiment with that code, run `bin/console` for an interactive prompt. +Czech bank accounts validations with official Czech National Bank algorithm. -TODO: Delete this and the text above, and describe your gem ## Installation Add this line to your application's Gemfile: @@ -18,22 +17,79 @@ Or install it yourself as: $ gem install czech_bank_account + ## Usage -TODO: Write usage instructions here +Initialize `CzechBankAccount::Account` and call `validate` on it: +``` +number = '36-6420840257' # bank account number with optional prefix but without bank code +bank_code = '0301' # separate bank code +account = CzechBankAccount::Account.new(number, bank_code) -## Development +account.validate # returns array with error symbols +#=> [:prefix_doesnt_pass_checksum, :unknown_bank_code] +``` -After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment. +Or use a shortcut which returns errors directly: +``` +CzechBankAccount.validate('35-6420840257', '9999') # returns array with error symbols +#=> [:unknown_bank_code] +``` -To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). +### Possible error symbols + +1. `:number_is_empty` +2. `:bank_code_is_empty` +3. `:number_includes_not_allowed_characters` +4. `:number_prefix_is_over_length_limit` +5. `:number_is_over_or_under_length_limit` +6. `:prefix_doesnt_pass_checksum` +7. `:number_doesnt_pass_checksum` +8. `:unknown_bank_code` + + +### Usage in Rails + +Prepare your own custom validation as per [Rails guide](https://guides.rubyonrails.org/active_record_validations.html#performing-custom-validations) + + +For example ActiveModel::Validator could look like following: +``` +# app/validators/czech_bank_account_validator.rb +class CzechBankAccountValidator < ActiveModel::Validator + def validate(record) + errors = CzechBankAccount.validate(record.account_number, record.bank_code) + errors.each { |e| record.errors.add(:base, e) } + end +end +``` + +Then in model: +``` +class Invoice < ApplicationRecord + validates_with CzechBankAccountValidator +end +``` + + ## Contributing -Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/czech_bank_account. +Bug reports and pull requests are welcome on GitHub at https://github.com/Masa331/czech_bank_account. + ## License The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). + + +## Links + + +### Official docs for account validation algorithm + +* http://www.cnb.cz/miranda2/export/sites/www.cnb.cz/cs/platebni_styk/pravni_predpisy/download/vyhl_169_2011.pdf +* http://www.cnb.cz/miranda2/export/sites/www.cnb.cz/cs/platebni_styk/ucty_kody_bank/download/kody_bank_CR.pdf +* http://www.cnb.cz/cs/platebni_styk/ucty_kody_bank/index.html