Skip to content

Instantly share code, notes, and snippets.

@tallakt
Last active March 3, 2017 09:30
Show Gist options
  • Save tallakt/7b1919e16735df1e3f293cf3c0efb775 to your computer and use it in GitHub Desktop.
Save tallakt/7b1919e16735df1e3f293cf3c0efb775 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'net/http'
require 'json'
def get_json(url)
uri = URI(url)
JSON.parse(Net::HTTP.get uri)
end
def get_asset_eur(asset)
uri = URI("https://api.cryptonator.com/api/ticker/#{asset}-EUR")
get_json(uri)["ticker"]["price"].to_f
end
def get_eur_to_fiat(fiat)
case fiat
when "EUR"
1.0
else
fixer = get_json "http://api.fixer.io/latest?symbols=#{fiat}"
fixer["rates"][fiat].to_f
end
end
#
# main script
#
from_asset = ARGV.shift.upcase
to_fiat = ARGV.shift.upcase
amount = ARGV.shift.to_f
converted = amount * get_asset_eur(from_asset) * get_eur_to_fiat(to_fiat)
puts "#{amount} #{from_asset} is #{converted} #{to_fiat}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment