coinmon 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 55dccea8c5926dc63fc75d9fd6087cbdfd2e6642
4
+ data.tar.gz: 6378fa57d25c876b3470513b03632cc2cf9449a7
5
+ SHA512:
6
+ metadata.gz: b29f1d8f336c78b295088e9cf59e519f14ee601795c8f3520c68398acef6c035ff7902cf4bf618062207d78a52f0ba8c8df54ef52a25f6b5054c687ec37de9ba
7
+ data.tar.gz: ccc4dae231f952526bcc4d72a53194518761d1415d28faa1a9f45e84a0ae4f1c950e2daa5c808bb498b5cc51bff18cf4f53b47b163c83f3128df7a0c845aa96e
data/bin/coinmon ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ %w(yaml coinmon coinmon/tablifier).each { |dep| require dep }
3
+
4
+ portfolio = YAML.load_file ARGV[0]
5
+ data = Coinmon.run(portfolio, ARGV[1])
6
+ puts Tablifier.run data
data/lib/coinmon.rb ADDED
@@ -0,0 +1,53 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'coinmon/tablifier'
4
+
5
+ class Coinmon
6
+ class << self
7
+ def run(portfolio, currency)
8
+ conversion_rate = load_json(
9
+ "https://api.fixer.io/latest?base=#{currency}&symbols=USD"
10
+ ).dig('rates', 'USD')
11
+
12
+ rates = load_rates portfolio.keys
13
+ portfolio.each do |symbol, data|
14
+ amount = data[:amount]
15
+ investment = data[:investment]
16
+ rate_gbp = rates[symbol] / conversion_rate
17
+ price = amount * rate_gbp
18
+ data.merge!(
19
+ paid_per_unit: investment / amount,
20
+ price_per_unit: rate_gbp,
21
+ price: price,
22
+ earnings: price - investment
23
+ )
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ API_URL ||= 'http://coincap.io'.freeze
30
+
31
+ def load_json(url)
32
+ JSON.parse Net::HTTP.get URI url
33
+ end
34
+
35
+ def load_coin_price(symbol)
36
+ load_json("#{API_URL}/page/#{symbol.upcase}").fetch('price_usd', 0)
37
+ end
38
+
39
+ def load_rates(symbols)
40
+ symbols.map { |symbol| spawn_worker symbol }
41
+ .map { |thread| thread.join[:r] }
42
+ .each_with_object({}) do |data, ret|
43
+ ret[data[:symbol]] = data[:price_usd]
44
+ end
45
+ end
46
+
47
+ def spawn_worker(symbol)
48
+ Thread.new do
49
+ Thread.current[:r] = { symbol: symbol, price_usd: load_coin_price(symbol) }
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,58 @@
1
+ require 'tty-table'
2
+
3
+ class Tablifier
4
+ class << self
5
+ HEADER ||= [
6
+ 'Name',
7
+ 'Paid per Unit',
8
+ 'Price per Unit',
9
+ 'Investment',
10
+ 'Value',
11
+ 'ROI'
12
+ ].freeze
13
+
14
+ def run(data)
15
+ total_investment, total_value, total_earnings = 0, 0, 0
16
+ table = TTY::Table.new(header: HEADER) do |t|
17
+ data.each do |symbol, data|
18
+ investment = data[:investment]
19
+ value = data[:price]
20
+ earnings = data[:earnings]
21
+ total_investment += investment
22
+ total_value += value
23
+ total_earnings += earnings
24
+
25
+ t << [
26
+ symbol.upcase,
27
+ data[:paid_per_unit].round(2),
28
+ data[:price_per_unit].round(2),
29
+ investment.round(2),
30
+ value.round(2),
31
+ earnings.round(2)
32
+ ]
33
+ end
34
+ t << [
35
+ 'Total',
36
+ 'NA',
37
+ 'NA',
38
+ total_investment.round(2),
39
+ total_value.round(2),
40
+ total_earnings.round(2)
41
+ ]
42
+ end
43
+
44
+ p = Pastel.new
45
+ filter = proc do |val, row, col|
46
+ if [0, table.rows.length].include? row
47
+ p.white.on_bright_black val
48
+ elsif row > 0 && col == 5
49
+ val.to_i > 0 ? p.black.on_green(val) : p.black.on_red(val)
50
+ else
51
+ val
52
+ end
53
+ end
54
+
55
+ table.render :ascii, padding: [0, 2, 0, 2], filter: filter
56
+ end
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: coinmon
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Pierre Martin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-12-23 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Coin monitoring made easy.
14
+ email: hickscorp@gmail.com
15
+ executables:
16
+ - coinmon
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - bin/coinmon
21
+ - lib/coinmon.rb
22
+ - lib/coinmon/tablifier.rb
23
+ homepage: http://github.com/hickscorp/coinmon
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.6.10
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: Coin monitoring made easy.
47
+ test_files: []