Skip to content

Instantly share code, notes, and snippets.

@janklimo
Forked from citizen428/bx.rb
Last active Sep 2, 2017
Embed
What would you like to do?
bx.in.th API
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'net/http'
require 'json'
require 'yaml'
unless ARGV.count == 0 || ARGV.count == 1
puts "Usage #{$0} [--mappings]"
exit
end
MAPPINGS = {
'1': 'BTC',
'21': 'ETH',
'22': 'DASH',
'25': 'XRP',
'26': 'OMG'
}
CURRENCY_SYMBOLS = { 'THB' => '฿', 'BTC' => '₿' }
bx_response = Net::HTTP.get_response(URI('https://bx.in.th/api/'))
data = JSON.parse(bx_response.body).sort_by { |k, _| k.to_i }.to_h
portfolio = YAML.load_file('portfolio.yml')
if ARGV[0] == '--mappings'
data.each do |key, crypto|
puts "#{key.rjust(2)}: #{crypto['primary_currency']}/#{crypto['secondary_currency']}"
end
exit
end
balance = 0
cryptos = MAPPINGS.empty? ? data.keys : MAPPINGS.keys
puts "Coin\tLast Price\tAmount\tNet Value"
cryptos.each do |key|
next unless portfolio[MAPPINGS[key]]
crypto = data[key.to_s]
symbol = CURRENCY_SYMBOLS.fetch(crypto['primary_currency'])
bid = crypto['orderbook']['bids']['highbid'].to_f
amount = portfolio[MAPPINGS[key]].to_f
crypto_value = amount * bid
puts "#{crypto['secondary_currency']}\t#{symbol}#{crypto['last_price'].to_i}" \
"\t\t#{amount.round(3)}\t฿#{crypto_value.to_i}"
balance += crypto_value
end
puts "Balance: ฿#{balance.to_i}"
# How many coins do you own?
OMG: 1
ETH: 2
BTC: 3
DASH: 4
@janklimo
Copy link
Author

janklimo commented Sep 2, 2017

Sample output:
screen shot 2017-09-02 at 3 29 16 pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment