Skip to content

Instantly share code, notes, and snippets.

@citizen428

citizen428/bx.rb Secret

Created Aug 31, 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'
unless ARGV.count == 0 || ARGV.count == 1
puts "Usage #{$0} [--mappings]"
exit
end
FILTER_KEYS = %w(1 21 26)
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
if ARGV[0] == '--mappings'
data.each do |key, crypto|
puts "#{key.rjust(2)}: #{crypto['primary_currency']}/#{crypto['secondary_currency']}"
end
exit
end
cryptos = FILTER_KEYS.empty? ? data.keys : FILTER_KEYS
cryptos.each do |key|
crypto = data[key]
symbol = CURRENCY_SYMBOLS.fetch(crypto['primary_currency'])
puts "#{crypto['secondary_currency']}: #{symbol}#{crypto['last_price']}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment