Bankster::Shared
Bankster::Transaction
provides some resources to transmit a bankster payment transaction.
Bankster::Transaction
: A PORO that acts as a model.Bankster::Representers::TransactionRepresenter
: A grape representer that converts aBankster::Transaction
to json and vice versa.schemas/transaction_schema
: A json schmema, that can validate a bankster transaction.factories/transaction_factory
: A factory that provides mocked bankster transactions.
Installation
Add this line to your application's Gemfile:
gem 'bankster_shared'
And then execute:
$ bundle
Or install it yourself as:
$ gem install bankster_shared
Usage
Model
The Bankster::Transaction
model has the following attributes accessible:
- name
- iban
- bic
- purpose
- valutation_date
- booking_date
- type
Representers
Given the bankster json api response for a transaction:
api_response = {
"name":"Max Mustermann",
...
"amount":{"cents":"123","currency":"EUR"}
}
The representer can fill a Bankster::Transaction
object from the json:
# initialize a transaction object
transaction = Bankster::Transaction.new
# extend it with the representer
transaction.extend(Bankster::Representers::TransactionRepresenter)
# fill it from the json response
transaction.from_json(api_response)
transaction.amount # => Money.new(123, "EUR")
In the other direction, the representer works the same. Given a Bankster::Transaction
object, you can
extend it with the representer and call #to_json
in order to get a json that matches our schema.
# extend the transaction with its representer
transaction.extend(Bankster::Representers::TransactionRepresenter)
# dump the json
puts transaction.to_json
# =>
{"purpose"=>"my purpose", "name"=>"Max Mustermann", "iban"=>"DE123", "bic"=>"ASD", "valutation_date"=>"2015-10-26", "booking_date"=>"2015-10-26", "type"=>"credit_tansfer", "amount"=>{"cents"=>"123", "currency"=>"EUR"}}
Schemas
This gem provides a json schema that can be used somewhere else. E.g. if you want to speciy that some json string matches the transaction schema:
Utilize the json-schema-matchers gem to write rspecs.
Add the following to your spec/spec_helper.rb
:
bankster-transaction_dir = Gem::Specification.find_by_name('bankster-transaction').gem_dir
Dir[File.join(bankster-transaction_dir, "/lib/schemas/*.json")].each do |file|
config.json_schemas[File.basename(file, ".json").to_sym] = file
end
And now, you can specify sth. to match one of the schemas that reside at lib/schemas/
:
it 'matches the transaction schema' do
expect(some_json).to match_json_schema(:transaction_schema)
end
Factories
If you intent to use one of the factories that are provided by this gem, just load them in your spec/spec_helper
:
bankster-transaction_dir = Gem::Specification.find_by_name('bankster-transaction').gem_dir
Dir.glob(File.join(bankster-transaction_dir, "spec/factories/*.rb")).each { |f| require f }
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake rspec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
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.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/bankster/bankster_shared.