commissioner-guy 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f6067d96597146d7a831da0b17b7ddd5b1c2a3f33b41763d5ba093fd42e60021
4
+ data.tar.gz: 5db9d89fd1598f2d2a281164634c38f4d91b9c7f3fe8ff0f3caf0eaee737a3d0
5
+ SHA512:
6
+ metadata.gz: 339d2995163a048e22bc76fc7e17128011eba644cb98f9eafbc0a63cf8be0e1eef060782d36615769747117a427e1e67d9a4c94162012b1174abe2170de6bb77
7
+ data.tar.gz: 5cc1573af34d93762a262534980b26b6141474b403eeaa3b306a32152cb0ab7d765293699398d3d7d93c40fe0a83a33eb9fbf3223c4f146ad4aac89a4926a460
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.irbrc ADDED
@@ -0,0 +1,4 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'commissioner'
4
+ Commissioner.configure {|c| c.exchanger = ->(from, to, amount) { [Money.from_amount(amount.to_f, to), 1] } }
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1 @@
1
+ 2.6.6
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ before_install: gem install bundler -v 1.17.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in exchanger.gemspec
6
+ gemspec
@@ -0,0 +1,55 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ commissioner (0.1.0)
5
+ dry-auto_inject (~> 0.6)
6
+ dry-configurable
7
+ money (~> 6.13.8)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ concurrent-ruby (1.1.7)
13
+ diff-lcs (1.4.4)
14
+ dry-auto_inject (0.7.0)
15
+ dry-container (>= 0.3.4)
16
+ dry-configurable (0.11.6)
17
+ concurrent-ruby (~> 1.0)
18
+ dry-core (~> 0.4, >= 0.4.7)
19
+ dry-equalizer (~> 0.2)
20
+ dry-container (0.7.2)
21
+ concurrent-ruby (~> 1.0)
22
+ dry-configurable (~> 0.1, >= 0.1.3)
23
+ dry-core (0.4.10)
24
+ concurrent-ruby (~> 1.0)
25
+ dry-equalizer (0.3.0)
26
+ i18n (1.8.5)
27
+ concurrent-ruby (~> 1.0)
28
+ money (6.13.8)
29
+ i18n (>= 0.6.4, <= 2)
30
+ rake (10.5.0)
31
+ rspec (3.10.0)
32
+ rspec-core (~> 3.10.0)
33
+ rspec-expectations (~> 3.10.0)
34
+ rspec-mocks (~> 3.10.0)
35
+ rspec-core (3.10.0)
36
+ rspec-support (~> 3.10.0)
37
+ rspec-expectations (3.10.0)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.10.0)
40
+ rspec-mocks (3.10.0)
41
+ diff-lcs (>= 1.2.0, < 2.0)
42
+ rspec-support (~> 3.10.0)
43
+ rspec-support (3.10.0)
44
+
45
+ PLATFORMS
46
+ ruby
47
+
48
+ DEPENDENCIES
49
+ bundler (~> 1.17)
50
+ commissioner!
51
+ rake (~> 10.0)
52
+ rspec (~> 3.0)
53
+
54
+ BUNDLED WITH
55
+ 1.17.3
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Valentine Kiselev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,90 @@
1
+ # Commissioner
2
+
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'commissioner'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install easy_commissioner
19
+
20
+ ## Usage
21
+
22
+ ### First, configure the gem
23
+
24
+ ```ruby
25
+ Commissioner.configure do |config|
26
+ # If you exchange money provider the lambda that receives
27
+ # from - string
28
+ # to - string
29
+ # amount - Money instance
30
+ # 'amount' might be in 'from' or 'to' currency
31
+ # Must return either just exchanged amount or [amount, exchanged_rate]
32
+ config.exchanger = ->(from, to, amount) { ... }
33
+
34
+ # When applying commission the amount might be rounded. This setting
35
+ # defined the rounding mode. Available modes are:
36
+ # - :up
37
+ # - :down
38
+ # - :half_up
39
+ # - :half_even
40
+ # See BigDecimal rounding modes for more
41
+ config.rouding_mode = :up
42
+ end
43
+ ```
44
+
45
+ ### Explicit call
46
+
47
+ ```ruby
48
+ calculation = Commissioner.calculate(
49
+ charged_amount: 100,
50
+ charged_currency: 'EUR',
51
+ received_currency: 'USD',
52
+ commission: 10, # %
53
+ exchange_commission: 15 # %
54
+ )
55
+
56
+ calculation.charged_amount # #<Money fractional:10000 currency:EUR>
57
+ calculation.received_amount # #<Money fractional:7650 currency:USD>
58
+ calculation.fee # #<Money fractional:1000 currency:EUR>
59
+ calculation.exchange_fee # <Money fractional:1350 currency:USD>
60
+ calculation.exchange_rate # 1 (only if config.exchanger returns it)
61
+ ```
62
+
63
+ ### As a mixin
64
+
65
+ ```ruby
66
+ class MyCalculator
67
+ include Commissiner::Mixin
68
+
69
+ def call(params)
70
+ calculate(params)
71
+ end
72
+ end
73
+ ```
74
+
75
+ ## Development
76
+
77
+ - [x] Custom exchanger
78
+ - [x] If exchanging is not needed, it is not executed
79
+ - [x] Commission for operation
80
+ - [x] Commission for exchange
81
+ - [x] No matter whether received or charged amount is provided, the calculation result is the same
82
+ - [ ] User-defined order of commissions aplying
83
+
84
+ ## Contributing
85
+
86
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mrexox/commissioner.
87
+
88
+ ## License
89
+
90
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
8
+ task :build do
9
+ sh 'gem build commissioner.gemspec'
10
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "exchanger"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,41 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'commissioner/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'commissioner-guy'
7
+ spec.version = Commissioner::VERSION
8
+ spec.authors = ['Valentine Kiselev']
9
+ spec.email = ['mrexox@outlook.com']
10
+
11
+ spec.summary = %q{A simple gem to calculate money based on provided amounts.}
12
+ spec.description = %q{A simple gem to calculate money based on provided amounts.}
13
+ spec.homepage = 'https://github.com/mrexox/commissioner'
14
+ spec.license = 'MIT'
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['homepage_uri'] = spec.homepage
20
+ spec.metadata['source_code_uri'] = 'https://github.com/mrexox/commissioner'
21
+ else
22
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
23
+ 'public gem pushes.'
24
+ end
25
+
26
+ # Specify which files should be added to the gem when it is released.
27
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
28
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
29
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
30
+ end
31
+
32
+ spec.require_paths = ['lib']
33
+
34
+ spec.add_runtime_dependency 'dry-configurable', '~> 0.11'
35
+ spec.add_runtime_dependency 'dry-auto_inject', '~> 0.6'
36
+ spec.add_runtime_dependency 'money', '~> 6.13.8'
37
+
38
+ spec.add_development_dependency 'bundler', '~> 1.17'
39
+ spec.add_development_dependency 'rake', '~> 10.0'
40
+ spec.add_development_dependency 'rspec', '~> 3.0'
41
+ end
@@ -0,0 +1,16 @@
1
+ require 'commissioner/version'
2
+ require 'commissioner/calculator'
3
+ require 'commissioner/mixin'
4
+ require 'dry-configurable'
5
+
6
+ module Commissioner
7
+ extend Dry::Configurable
8
+
9
+ # A lambda that accepts from_currency, to_currency, amount
10
+ setting :exchanger, reader: true # arity = 3
11
+ setting :rounding_mode, :half_up # possible values: :up, :down, :half_up, :half_even
12
+
13
+ def self.calculate(params)
14
+ Calculator.new(params, config: self.config).calculate
15
+ end
16
+ end
@@ -0,0 +1,131 @@
1
+ require 'money'
2
+
3
+ module Commissioner
4
+ class Calculator
5
+ AmountUnknown = Class.new(StandardError)
6
+ CommissionerArityInvalid = Class.new(StandardError)
7
+
8
+ HELP_MESSAGE = 'You must provider either charged_amount (with charged_currency) or received_amount (with received_currency). If none or both are non-zero, the service cannot know how to handle this.'.freeze
9
+ DEFAULT_ORDER = [
10
+ :reduce_commission,
11
+ :exchange,
12
+ :reduce_exchange_commission
13
+ ].freeze
14
+
15
+ private_constant :HELP_MESSAGE
16
+
17
+
18
+ def initialize(params, config:, order: DEFAULT_ORDER)
19
+ @charged_amount = guess_amount(params[:charged_amount], params[:charged_currency])
20
+ @received_amount = guess_amount(params[:received_amount], params[:received_currency])
21
+
22
+ @exchange_commission = params[:exchange_commission] || 0
23
+ @commission = params[:commission] || 0
24
+
25
+ unless config.exchanger.is_a?(Proc) && config.exchanger.arity == 3
26
+ raise CommissionerArityInvalid.new("'exchanger' setting must be a lambda with arity of 3")
27
+ end
28
+
29
+ @exchanger = config.exchanger
30
+ @rounding_mode =
31
+ case config.rounding_mode
32
+ when :up
33
+ BigDecimal::ROUND_UP
34
+ when :down
35
+ BigDecimal::ROUND_DOWN
36
+ when :half_even
37
+ BigDecimal::ROUND_HALF_EVEN
38
+ else
39
+ BigDecimal::ROUND_HALF_UP
40
+ end
41
+ @order = order
42
+ end
43
+
44
+ def calculate
45
+ if !empty?(@received_amount) && empty?(@charged_amount)
46
+ @charged_amount = calculate_for_received
47
+ elsif !empty?(@charged_amount) && empty?(@received_amount)
48
+ @received_amount = calculate_for_charged
49
+ else
50
+ raise AmountUnknown.new(HELP_MESSAGE)
51
+ end
52
+
53
+ OpenStruct.new(
54
+ received_amount: @received_amount,
55
+ charged_amount: @charged_amount,
56
+ fee: @fee,
57
+ exchange_fee: @exchange_fee,
58
+ exchange_rate: @exchange_rate
59
+ )
60
+ end
61
+
62
+ private
63
+
64
+ attr_reader :exchange_commission, :commission, :exchanger
65
+
66
+ def empty?(amount)
67
+ amount.nil? || amount.zero?
68
+ end
69
+
70
+ def calculate_for_received
71
+ amount = @received_amount
72
+
73
+ amount, @exchange_fee = add_commission(amount, exchange_commission)
74
+
75
+ amount, @exchange_rate = exchange(amount) if @charged_amount.currency != @received_amount.currency
76
+
77
+ amount, @fee = add_commission(amount, commission)
78
+
79
+ amount
80
+ end
81
+
82
+ def calculate_for_charged
83
+ amount = @charged_amount
84
+
85
+ amount, @fee = reduce_commission(amount, commission)
86
+
87
+ amount, @exchange_rate = exchange(amount) if @charged_amount.currency != @received_amount.currency
88
+
89
+ amount, @exchange_fee = reduce_commission(amount, exchange_commission)
90
+
91
+ amount
92
+ end
93
+
94
+ def guess_amount(amount, currency)
95
+ case amount
96
+ when Money
97
+ amount
98
+ when Numeric
99
+ Money.from_amount(amount, currency) if currency.is_a?(String)
100
+ when NilClass
101
+ Money.new(0, currency) if currency.is_a?(String)
102
+ end
103
+ end
104
+
105
+ def reduce_commission(amount, commission)
106
+ return 0 unless commission
107
+ fee = round(amount.to_f * commission / 100, amount.currency.to_s)
108
+ amount -= fee
109
+
110
+ [amount, fee]
111
+ end
112
+
113
+ def add_commission(amount, commission)
114
+ return 0 unless commission
115
+ fee = round(amount.to_f * commission / (100 - commission), amount.currency.to_s)
116
+ amount += fee
117
+
118
+ [amount, fee]
119
+ end
120
+
121
+ def round(decimal, currency)
122
+ Money.with_rounding_mode(@rounding_mode) do
123
+ Money.from_amount(decimal, currency)
124
+ end
125
+ end
126
+
127
+ def exchange(amount)
128
+ exchanger.call(@charged_amount.currency.to_s, @received_amount.currency.to_s, amount)
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,16 @@
1
+ module Commissioner
2
+ module Mixin
3
+ # TODO: Allow configuring order of operations
4
+ # Order = ->(*order) { @@order = order }
5
+
6
+ # @@order = [
7
+ # :commission,
8
+ # :exchange,
9
+ # :exchange_commission
10
+ # ]
11
+
12
+ def calculate(params)
13
+ Calculator.new(params, config: Commissioner.config, order: @@order).calculate
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module Commissioner
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: commissioner-guy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Valentine Kiselev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-12-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-configurable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.11'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: dry-auto_inject
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: money
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 6.13.8
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 6.13.8
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.17'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.17'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ description: A simple gem to calculate money based on provided amounts.
98
+ email:
99
+ - mrexox@outlook.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".irbrc"
106
+ - ".rspec"
107
+ - ".ruby-version"
108
+ - ".travis.yml"
109
+ - Gemfile
110
+ - Gemfile.lock
111
+ - LICENSE.txt
112
+ - README.md
113
+ - Rakefile
114
+ - bin/console
115
+ - bin/setup
116
+ - commissioner.gemspec
117
+ - lib/commissioner.rb
118
+ - lib/commissioner/calculator.rb
119
+ - lib/commissioner/mixin.rb
120
+ - lib/commissioner/version.rb
121
+ homepage: https://github.com/mrexox/commissioner
122
+ licenses:
123
+ - MIT
124
+ metadata:
125
+ homepage_uri: https://github.com/mrexox/commissioner
126
+ source_code_uri: https://github.com/mrexox/commissioner
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubygems_version: 3.0.3
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: A simple gem to calculate money based on provided amounts.
146
+ test_files: []