lendmarket 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +32 -0
- data/Rakefile +2 -0
- data/bin/lendmarket +38 -0
- data/data/market.csv +8 -0
- data/lendmarket.gemspec +24 -0
- data/lib/lendmarket/csv_tools.rb +14 -0
- data/lib/lendmarket/exceptions.rb +10 -0
- data/lib/lendmarket/formulas.rb +39 -0
- data/lib/lendmarket/version.rb +3 -0
- data/lib/lendmarket.rb +75 -0
- data/spec/csv_to_hash_spec.rb +21 -0
- data/spec/lendmarket_spec.rb +81 -0
- data/spec/spec_helper.rb +10 -0
- metadata +107 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1926912dc91c179a7e7a5f6555db75f9545bdece
|
4
|
+
data.tar.gz: 55da7b97250421df1ded582ca55abb60d3c4a5cd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b44b63893c7ea702d1509bb2136854bf36e67dbe871f9eba00e6b566b9d3e29657a3e1663c5419856ae53a945af05acce382728cd1bfc63615d95fc4aff22d75
|
7
|
+
data.tar.gz: ab8bde85183e038c9bf64c6af947a4ddf5b27f8fa0e6d2f9bba366f1844bc26557c9ebcbad5adbb203f274cb7d8b1ca6f0b4ba8488467b72fe1a7680bb175ff5
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Valters Krontals
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# Lendmarket
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'lendmarket'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install lendmarket
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
$ lendmarket [market.csv] [loan_amount]
|
24
|
+
|
25
|
+
|
26
|
+
## Contributing
|
27
|
+
|
28
|
+
1. Fork it ( https://github.com/[my-github-username]/lendmarket/fork )
|
29
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
30
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
31
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
32
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/lendmarket
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'lendmarket'
|
4
|
+
require 'csv_tools'
|
5
|
+
|
6
|
+
TERM = 36
|
7
|
+
class NoMarketFile < StandardError; end
|
8
|
+
|
9
|
+
begin
|
10
|
+
raise NoMarketFile if ARGV[0].nil?
|
11
|
+
|
12
|
+
csv_data = File.read(ARGV[0])
|
13
|
+
markets_hash = Lendmarket::CSVTools.csv_to_hash csv_data
|
14
|
+
quote = Lendmarket::Quote.new({ amount: ARGV[1].to_i, term: TERM, markets: markets_hash }).calc
|
15
|
+
|
16
|
+
puts "Requested amount: £%d" % quote[:requested_amount]
|
17
|
+
puts "Rate: %.1f%" % quote[:rate]
|
18
|
+
puts "Monthly repayment: £%.2f" % quote[:monthly_repayment]
|
19
|
+
puts "Total repayment: £%.2f" % quote[:total_repayment]
|
20
|
+
|
21
|
+
rescue Errno::ENOENT
|
22
|
+
puts 'Markets file not found, check the path and try again'
|
23
|
+
|
24
|
+
rescue Lendmarket::NotEnoughMoney
|
25
|
+
puts 'Sorry, it is not possible to provide a quote at this time'
|
26
|
+
|
27
|
+
rescue Lendmarket::InvalidAmount
|
28
|
+
valid_amount_txt = Lendmarket::Quote::VALID_AMOUNT.minmax.join(' and ')
|
29
|
+
puts 'The seleced loan amount is not valid'
|
30
|
+
puts "You must choose an amount between #{ valid_amount_txt } in 100 increments"
|
31
|
+
|
32
|
+
rescue NoMarketFile
|
33
|
+
puts 'You must provide a markets file'
|
34
|
+
|
35
|
+
ensure
|
36
|
+
puts "\n\nusage: lendmarket [market.csv] [loan_amount]"
|
37
|
+
|
38
|
+
end
|
data/data/market.csv
ADDED
data/lendmarket.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'lendmarket/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "lendmarket"
|
8
|
+
spec.version = Lendmarket::VERSION
|
9
|
+
spec.authors = ["Valters Krontals"]
|
10
|
+
spec.email = ["v.krontals@gmail.com"]
|
11
|
+
spec.summary = %q{Command line tool to get the best rate}
|
12
|
+
spec.description = %q{Get the best rate from lenders}
|
13
|
+
spec.homepage = "https://github.com/vkrontals/lendmarket"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = ['lendmarket']
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib", "lib/lendmarket"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec", "~> 3.3"
|
24
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'csv'
|
2
|
+
|
3
|
+
module Lendmarket
|
4
|
+
module CSVTools
|
5
|
+
|
6
|
+
def self.csv_to_hash(csv_data)
|
7
|
+
csv_data = CSV.parse(csv_data, { converters: :numeric })
|
8
|
+
keys = csv_data.shift.map{ |x| x.downcase.to_sym }
|
9
|
+
csv_data.map {|a| Hash[ keys.zip(a.map{ |x| x })] }
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Excel
|
2
|
+
module Formulas
|
3
|
+
def pmt(rate, nper, pv, fv=0, type=0)
|
4
|
+
((-pv * pvif(rate, nper) - fv ) / ((1.0 + rate * type) * fvifa(rate, nper)))
|
5
|
+
end
|
6
|
+
|
7
|
+
def ipmt(rate, per, nper, pv, fv=0, type=0)
|
8
|
+
p = pmt(rate, nper, pv, fv, 0);
|
9
|
+
ip = -(pv * pow1p(rate, per - 1) * rate + p * pow1pm1(rate, per - 1))
|
10
|
+
(type == 0) ? ip : ip / (1 + rate)
|
11
|
+
end
|
12
|
+
|
13
|
+
def ppmt(rate, per, nper, pv, fv=0, type=0)
|
14
|
+
p = pmt(rate, nper, pv, fv, type)
|
15
|
+
ip = ipmt(rate, per, nper, pv, fv, type)
|
16
|
+
p - ip
|
17
|
+
end
|
18
|
+
|
19
|
+
protected
|
20
|
+
|
21
|
+
def pow1pm1(x, y)
|
22
|
+
(x <= -1) ? ((1 + x) ** y) - 1 : Math.exp(y * Math.log(1.0 + x)) - 1
|
23
|
+
end
|
24
|
+
|
25
|
+
def pow1p(x, y)
|
26
|
+
(x.abs > 0.5) ? ((1 + x) ** y) : Math.exp(y * Math.log(1.0 + x))
|
27
|
+
end
|
28
|
+
|
29
|
+
def pvif(rate, nper)
|
30
|
+
pow1p(rate, nper)
|
31
|
+
end
|
32
|
+
|
33
|
+
def fvifa(rate, nper)
|
34
|
+
(rate == 0) ? nper : pow1pm1(rate, nper) / rate
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
data/lib/lendmarket.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require "lendmarket/version"
|
2
|
+
require "lendmarket/exceptions"
|
3
|
+
require "lendmarket/formulas"
|
4
|
+
|
5
|
+
module Lendmarket
|
6
|
+
class Quote
|
7
|
+
include Excel::Formulas
|
8
|
+
attr_reader :amount, :markets, :term
|
9
|
+
|
10
|
+
VALID_AMOUNT = (1000..15000).step(100).to_a
|
11
|
+
Market = Struct.new( :rate, :available)
|
12
|
+
|
13
|
+
def initialize(args)
|
14
|
+
raise Lendmarket::InvalidAmount unless VALID_AMOUNT.include?(args[:amount])
|
15
|
+
|
16
|
+
@amount = args[:amount]
|
17
|
+
@markets = marketify args[:markets]
|
18
|
+
@term = args[:term]
|
19
|
+
raise Lendmarket::NotEnoughMoney if amount > money_available
|
20
|
+
end
|
21
|
+
|
22
|
+
def calc
|
23
|
+
{
|
24
|
+
requested_amount: amount,
|
25
|
+
rate: (rate * 100).round(1),
|
26
|
+
monthly_repayment: monthly_repayment.round(2),
|
27
|
+
total_repayment: total_repayment.round(2)
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def total_repayment
|
34
|
+
monthly_repayment * term
|
35
|
+
end
|
36
|
+
|
37
|
+
def rate
|
38
|
+
val = sorted_markets
|
39
|
+
.reduce([]) do |result, market|
|
40
|
+
result << market if money_available(result) < amount
|
41
|
+
result
|
42
|
+
end
|
43
|
+
|
44
|
+
val.reduce(0) { |sum, n| sum + n.rate } / val.length
|
45
|
+
end
|
46
|
+
|
47
|
+
def money_available(markets_array = markets)
|
48
|
+
markets_array.reduce(0) { |sum, n| sum + n.available }
|
49
|
+
end
|
50
|
+
|
51
|
+
def monthly_repayment
|
52
|
+
pmt(monthly_rate, term, -amount)
|
53
|
+
end
|
54
|
+
|
55
|
+
def monthly_rate
|
56
|
+
(1 + rate) ** (1 / 12.0) - 1
|
57
|
+
end
|
58
|
+
|
59
|
+
def marketify(markets_array)
|
60
|
+
markets_array.reduce([]) do |result, lender|
|
61
|
+
(lender[:available].to_i / 10).times do
|
62
|
+
result << Market.new(lender[:rate], 10)
|
63
|
+
end
|
64
|
+
|
65
|
+
result
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def sorted_markets
|
70
|
+
markets.sort_by { |x| x[:rate] }
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'csv_tools'
|
3
|
+
|
4
|
+
describe Lendmarket::CSVTools do
|
5
|
+
let(:csv_data) {
|
6
|
+
<<HERE
|
7
|
+
Lender,Rate,Available
|
8
|
+
Bob,0.075,640
|
9
|
+
Jane,0.069,480
|
10
|
+
HERE
|
11
|
+
}
|
12
|
+
describe '#csv_to_hash' do
|
13
|
+
it 'converts a csv file to hash' do
|
14
|
+
expect(Lendmarket::CSVTools.csv_to_hash(csv_data)).to eq(
|
15
|
+
[{ lender: 'Bob', rate: 0.075, available: 640 },
|
16
|
+
{ lender: 'Jane', rate: 0.069, available: 480 }])
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Lendmarket::Quote do
|
4
|
+
|
5
|
+
let (:markets) {
|
6
|
+
[
|
7
|
+
{ lender: 'Bob' , rate: 0.075, available: 640 },
|
8
|
+
{ lender: 'Jane' , rate: 0.069, available: 480 },
|
9
|
+
{ lender: 'Fred' , rate: 0.071, available: 520 },
|
10
|
+
{ lender: 'Mary' , rate: 0.104, available: 170 },
|
11
|
+
{ lender: 'John' , rate: 0.081, available: 320 },
|
12
|
+
{ lender: 'Dave' , rate: 0.074, available: 140 },
|
13
|
+
{ lender: 'Angela', rate: 0.071, available: 60 }
|
14
|
+
]
|
15
|
+
}
|
16
|
+
|
17
|
+
describe "#new" do
|
18
|
+
|
19
|
+
it "raises exception if not enough money is available" do
|
20
|
+
expect { Lendmarket::Quote.new({ amount: 15000, term: 36, markets: markets }) }
|
21
|
+
.to raise_error Lendmarket::NotEnoughMoney
|
22
|
+
end
|
23
|
+
|
24
|
+
it "raises exception if amount is invalid" do
|
25
|
+
[50, 110, -100, 16000, 'test', '@£$@'].each do |amount|
|
26
|
+
expect { Lendmarket::Quote.new({ amount: amount, term: 36, markets: markets }) }
|
27
|
+
.to raise_error Lendmarket::InvalidAmount
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "#total_repayment" do
|
35
|
+
let(:quote) { Lendmarket::Quote.new({ amount: 1000, term: 36, markets: markets }) }
|
36
|
+
|
37
|
+
it { expect(quote.send :total_repayment).to be_within(0.01).of(1108.10) }
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "#rate" do
|
42
|
+
let(:quote) { Lendmarket::Quote.new({ amount: 1000, term: 36, markets: markets }) }
|
43
|
+
|
44
|
+
it { expect(quote.send :rate).to be_within(0.01).of(0.07) }
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "#monthly_repayment" do
|
48
|
+
let(:quote) { Lendmarket::Quote.new({ amount: 1000, term: 36, markets: markets }) }
|
49
|
+
|
50
|
+
it { expect(quote.send :monthly_repayment).to be_within(0.01).of(30.78) }
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "#monthly_rate" do
|
54
|
+
let(:quote) { Lendmarket::Quote.new({ amount: 1000, term: 36, markets: markets }) }
|
55
|
+
|
56
|
+
it { expect(quote.send :monthly_rate).to be_within(0.001).of(0.005) }
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "#marketify" do
|
60
|
+
let(:quote) { Lendmarket::Quote.new({ amount: 1000, term: 36, markets: markets }) }
|
61
|
+
|
62
|
+
it 'returns an array of markets' do
|
63
|
+
marketified = quote.send:marketify, markets
|
64
|
+
expect(marketified.length).to be(233)
|
65
|
+
|
66
|
+
marketified.each do |m|
|
67
|
+
expect(m.respond_to?(:rate)).to be true
|
68
|
+
expect(m.respond_to?(:available)).to be true
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
describe "#money_available" do
|
76
|
+
let(:quote) { Lendmarket::Quote.new({ amount: 1000, term: 36, markets: markets }) }
|
77
|
+
|
78
|
+
it { expect(quote.send :money_available).to eq 2330 }
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lendmarket
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Valters Krontals
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.3'
|
55
|
+
description: Get the best rate from lenders
|
56
|
+
email:
|
57
|
+
- v.krontals@gmail.com
|
58
|
+
executables:
|
59
|
+
- lendmarket
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bin/lendmarket
|
69
|
+
- data/market.csv
|
70
|
+
- lendmarket.gemspec
|
71
|
+
- lib/lendmarket.rb
|
72
|
+
- lib/lendmarket/csv_tools.rb
|
73
|
+
- lib/lendmarket/exceptions.rb
|
74
|
+
- lib/lendmarket/formulas.rb
|
75
|
+
- lib/lendmarket/version.rb
|
76
|
+
- spec/csv_to_hash_spec.rb
|
77
|
+
- spec/lendmarket_spec.rb
|
78
|
+
- spec/spec_helper.rb
|
79
|
+
homepage: https://github.com/vkrontals/lendmarket
|
80
|
+
licenses:
|
81
|
+
- MIT
|
82
|
+
metadata: {}
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
- lib/lendmarket
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.2.2
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: Command line tool to get the best rate
|
104
|
+
test_files:
|
105
|
+
- spec/csv_to_hash_spec.rb
|
106
|
+
- spec/lendmarket_spec.rb
|
107
|
+
- spec/spec_helper.rb
|