holdings 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a912cbcba7d8206d0797c667020ff31f679b927b
4
+ data.tar.gz: fb8f58695d71f8511da5140b8f4171b5efcdce75
5
+ SHA512:
6
+ metadata.gz: 0301841f719bbd87558ab1b7371ea4c33c13bc7a8e6bbf6ea06cf5ba71961e7b6cf922751b1c90f92c1f75ee284960fd018d8bbb11761ce0457946392fb17c96
7
+ data.tar.gz: 10dc4d967dd09deb0cb79d090c28c71d39c2408233b5e2b197bbfee0e893cb38f15d8bdae4e8d36cf767a86f2d8794d4844db78c2328cf0af74f0b62386b1600
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in holdings.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Luke Chadwick
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,33 @@
1
+ # Holdings
2
+
3
+ Holding is a simple gem to keep track of your holdings in a given currency/stock and convert back and
4
+ forward between the quote currency and base currency at a given exchange rate.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'holdings'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install holdings
19
+
20
+ ## Usage
21
+
22
+ ```
23
+ acc = Holdings::Simple.new(['aud_usd'], { usd: 100.0 })
24
+ acc.usd_to_aud(100.0, 1.0)
25
+ acc.aud_bal # 99.8 because of built in fee calculation
26
+ ```
27
+ ## Contributing
28
+
29
+ 1. Fork it ( http://github.com/<my-github-username>/holdings/fork )
30
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
31
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
32
+ 4. Push to the branch (`git push origin my-new-feature`)
33
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/holdings.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'holdings/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "holdings"
8
+ spec.version = Holdings::VERSION
9
+ spec.authors = ["Luke Chadwick"]
10
+ spec.email = ["me@vertis.io"]
11
+ spec.summary = %q{Record holdings such as currency and convert/trade between holdings}
12
+ spec.description = %q{Record holdings such as currency and convert/trade between holdings}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency 'coveralls'
25
+ end
data/lib/holdings.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'holdings/version'
2
+ require 'holdings/simple'
3
+
4
+ module Holdings
5
+ end
@@ -0,0 +1,86 @@
1
+ module Holdings
2
+ class Simple
3
+ attr_accessor :fee
4
+
5
+ def self.legacy_new(ltc_val, btc_val, usd_val, rur_val=0)
6
+ @ltc_bal = ltc_val
7
+ @btc_bal = btc_val
8
+ @usd_bal = usd_val
9
+ @rur_bal = rur_val
10
+ acc = self.new(%w(btc_usd ltc_usd ltc_btc btc_rur usd_rur), {ltc: ltc_val, btc: btc_val, usd: usd_val, rur: rur_val })
11
+ end
12
+
13
+ def initialize(pairs, balances)
14
+ @fee = 0.2
15
+
16
+ pairs.each do |pair|
17
+ base_currency, quote_currency = pair.split("_")
18
+ define_singleton_method "#{quote_currency}_to_#{base_currency}", lambda { |exchange_rate, amount, fee=0.2|
19
+ sell(base_currency, quote_currency, exchange_rate, amount, fee)
20
+ }
21
+
22
+ define_singleton_method "#{base_currency}_to_#{quote_currency}", lambda { |exchange_rate, amount, fee=0.2|
23
+ buy(base_currency, quote_currency, exchange_rate, amount, fee)
24
+ }
25
+ end
26
+
27
+ @balances = balances
28
+
29
+ currencies = pairs.join("_").split("_").uniq
30
+ currencies.each do |currency|
31
+ define_singleton_method "#{currency}_bal", lambda { get(currency) }
32
+ @balances[currency.to_sym] ||= 0.0
33
+ end
34
+ end
35
+
36
+ def set(currency, value)
37
+ @balances[currency.to_sym] = value
38
+ end
39
+
40
+ def get(currency)
41
+ @balances[currency.to_sym]
42
+ end
43
+
44
+ def credit(currency, value)
45
+ @balances[currency.to_sym] += value
46
+ end
47
+
48
+ def debit(currency, value)
49
+ @balances[currency.to_sym] -= value
50
+ end
51
+
52
+ def largest_balance
53
+ inverted_balances = @balances.invert
54
+ highest = inverted_balances.keys.sort.last
55
+ inverted_balances[highest].to_s
56
+ end
57
+
58
+ private
59
+
60
+ # Buy the quote currency for the base currency
61
+ def buy(base_currency, quote_currency, exchange_rate, amount, fee=0.2)
62
+ debit(base_currency, amount)
63
+ quote_amount_before_fee = amount * exchange_rate
64
+ quote_amount_after_fee = quote_amount_before_fee - calculate_fee(fee, quote_amount_before_fee)
65
+ credit(quote_currency, quote_amount_after_fee)
66
+ puts "#{base_currency}#{amount} @ #{quote_currency}#{exchange_rate} -> USD#{quote_amount_after_fee}" if ENV['DEBUG']
67
+ end
68
+
69
+ # Sell the quote currency for the base currency
70
+ def sell(base_currency, quote_currency, exchange_rate, amount, fee=0.2)
71
+ debit(quote_currency, amount)
72
+ base_amount_before_fee = amount / exchange_rate
73
+ base_amount_after_fee = base_amount_before_fee - calculate_fee(fee, base_amount_before_fee)
74
+ credit(base_currency, base_amount_after_fee)
75
+ puts "#{base_currency}#{amount} @ #{quote_currency}#{exchange_rate} -> USD#{base_amount_after_fee}" if ENV['DEBUG']
76
+ end
77
+
78
+ def has_enough_money?(balance, amount)
79
+ balance >= amount
80
+ end
81
+
82
+ def calculate_fee(fee, value)
83
+ (value.to_f * fee) / 100
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,3 @@
1
+ module Holdings
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ describe Holdings::Simple do
4
+ describe '#usd_to_btc' do
5
+ it "should move the balance to the purchased currency" do
6
+ acc = Holdings::Simple.new(['btc_usd'], { usd: 100.0 })
7
+ acc.usd_to_btc(100.0, 100.0)
8
+ acc.btc_bal.should == 0.998
9
+ end
10
+ end
11
+
12
+ describe '#btc_to_usd' do
13
+ it "should move the balance to the purchased currency" do
14
+ acc = Holdings::Simple.new(['btc_usd'], { btc: 1.0 })
15
+ acc.btc_to_usd(100.0, 1.0)
16
+ acc.usd_bal.should == 99.8
17
+ end
18
+ end
19
+
20
+ describe '#usd_to_ltc' do
21
+ it "should move the balance to the purchased currency" do
22
+ acc = Holdings::Simple.new(['ltc_usd'], { usd: 100.0 })
23
+ acc.usd_to_ltc(2.0, 100.0)
24
+ acc.ltc_bal.should == 49.9
25
+ end
26
+ end
27
+
28
+ describe '#ltc_to_usd' do
29
+ it "should move the balance to the purchased currency" do
30
+ acc = Holdings::Simple.new(['ltc_usd'], { ltc: 100.0 })
31
+ acc.ltc_to_usd(2.0, 50.0)
32
+ acc.usd_bal.should == 99.8
33
+ end
34
+ end
35
+
36
+ describe '#btc_to_ltc' do
37
+ it "should move the balance to the purchased currency" do
38
+ acc = Holdings::Simple.new(['ltc_btc'], { btc: 1.0 })
39
+ acc.btc_to_ltc(0.025, 1.0)
40
+ acc.ltc_bal.should == 39.92
41
+ end
42
+ end
43
+
44
+ describe '#ltc_to_btc' do
45
+ it "should move the balance to the purchased currency" do
46
+ acc = Holdings::Simple.new(['ltc_btc'], { ltc: 40.0 })
47
+ acc.ltc_to_btc(0.025, 40.0)
48
+ acc.btc_bal.should == 0.998
49
+ end
50
+ end
51
+
52
+ describe '#largest_balance' do
53
+ acc = Holdings::Simple.new(['btc_usd'], { usd: 100.0 })
54
+ acc.largest_balance.should == 'usd'
55
+ end
56
+
57
+ describe '#usd_bal' do
58
+ acc = Holdings::Simple.new(['btc_usd'], { usd: 100.0 })
59
+ acc.usd_bal.should == 100.0
60
+ end
61
+ end
@@ -0,0 +1,9 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+
4
+ $:.push File.expand_path("../lib", __FILE__)
5
+ require "holdings"
6
+
7
+
8
+ RSpec.configure do |config|
9
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: holdings
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Luke Chadwick
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-11 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.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: coveralls
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Record holdings such as currency and convert/trade between holdings
70
+ email:
71
+ - me@vertis.io
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - holdings.gemspec
82
+ - lib/holdings.rb
83
+ - lib/holdings/simple.rb
84
+ - lib/holdings/version.rb
85
+ - spec/holdings/simple_spec.rb
86
+ - spec/spec_helper.rb
87
+ homepage: ''
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.0.14
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Record holdings such as currency and convert/trade between holdings
111
+ test_files:
112
+ - spec/holdings/simple_spec.rb
113
+ - spec/spec_helper.rb