https://github.com/mrexox/commissioner
Ruby gem for calculating commissions and exchanging amounts easily
https://github.com/mrexox/commissioner
commission exchange gem money ruby
Last synced: about 2 months ago
JSON representation
Ruby gem for calculating commissions and exchanging amounts easily
- Host: GitHub
- URL: https://github.com/mrexox/commissioner
- Owner: mrexox
- License: mit
- Created: 2020-12-12T14:47:59.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2020-12-13T17:12:41.000Z (over 5 years ago)
- Last Synced: 2024-04-26T15:43:28.483Z (about 2 years ago)
- Topics: commission, exchange, gem, money, ruby
- Language: Ruby
- Homepage:
- Size: 38.1 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Commissioner Guy
[](https://travis-ci.org/mrexox/commissioner)
[](https://badge.fury.io/rb/commissioner-guy)
Calculates charged and received amounts based on provided one. Calls your exchanger if needed. Applies commissions in order that you define.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'commissioner-guy', require: 'commissioner'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install commissioner-guy
## Usage
### First, configure the gem
```ruby
require 'commissioner'
Commissioner.configure do |config|
# If you exchange money provider the lambda that receives
# from - string
# to - string
# amount - Money instance
# 'amount' might be in 'from' or 'to' currency
# Must return either just exchanged amount or [amount, exchanged_rate]
config.exchanger = ->(from, to, amount) { ... }
# When applying commission the amount might be rounded. This setting
# defined the rounding mode. Available modes are:
# - :up
# - :down
# - :half_up
# - :half_even
# See BigDecimal rounding modes for more
config.rouding_mode = :up
end
```
### Explicit call
```ruby
calculation = Commissioner.calculate(
charged_amount: 100,
charged_currency: 'EUR',
received_currency: 'USD',
commission: 10, # %
exchange_commission: 15 # %
)
calculation.charged_amount # #
calculation.received_amount # #
calculation.fee # #
calculation.exchange_fee #
calculation.exchange_rate # 1 (only if config.exchanger returns it)
```
### As a mixin
```ruby
class MyCalculator
include Commissiner::Mixin
def call(params)
calculate(params)
end
end
```
#### Specify order
You can specify in which order calculation will be applied. The steps are:
- exchange
- commission
- exchange_commission
`exchange` - calls exchanger if currencies of received and charged amounts differ
`commission` - applies typical commission operation
`exchange_commission` - applies commission for exchange (in charged currency if ordered before `exchange` or in received currency if ordered after `exchange`)
```ruby
class MyCalculator
include Commissioner::Mixin
# Default order is:
# - :commission
# - :exchange
# - :exchange_commission
Order[
:commission,
:exchange_commission,
:exchange
]
end
MyCalculator.new.calculate(
received_amount: 100,
received_currency: 'EUR',
charged_currency: 'USD',
commission: 10,
exchange_commission: 15
).to_h
# =>
# {
# :received_amount => #,
# :charged_amount => #,
# :fee => #,
# :exchange_fee => #,
# :exchange_rate => 1
# }
# Changing the order
class MyCalculator
Order[
:exchange_commission,
:exchange,
:commission
]
end
MyCalculator.new.calculate(
received_amount: 100,
received_currency: 'EUR',
charged_currency: 'USD',
commission: 10,
exchange_commission: 15
).to_h
# =>
# {
# :received_amount => #,
# :charged_amount => #,
# :fee => #,
# :exchange_fee => #,
# :exchange_rate => 1
# }
```
## Development
- [x] Custom exchanger
- [x] If exchanging is not needed, it is not executed
- [x] Commission for operation
- [x] Commission for exchange
- [x] No matter whether received or charged amount is provided, the calculation result is the same
- [x] User-defined order of commissions aplying and exchanging
- [ ] Custom commissions and exchanges in order
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/mrexox/commissioner.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).