Skip to content

Instantly share code, notes, and snippets.

@citizen428

citizen428/bx.rb Secret

Created August 31, 2017 09:46
Show Gist options
  • Save citizen428/d017a07b1b764f3f1e8c84eb4d533761 to your computer and use it in GitHub Desktop.
Save citizen428/d017a07b1b764f3f1e8c84eb4d533761 to your computer and use it in GitHub Desktop.
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