diff options
Diffstat (limited to 'blaunits.rb')
-rwxr-xr-x | blaunits.rb | 42 |
1 files changed, 17 insertions, 25 deletions
diff --git a/blaunits.rb b/blaunits.rb index be077e8..a047a66 100755 --- a/blaunits.rb +++ b/blaunits.rb @@ -3,37 +3,29 @@ require 'net/http' require 'json' -VERSION = "2.2.4" +VERSION = "2.3.0" BINARY_NAME = "units" begin - def get_crypto_id(currency_name) - url = 'https://api.coinmarketcap.com/v1/ticker/?limit=0' - uri = URI(url) - response = Net::HTTP.get(uri) - currencies = JSON.parse(response) - currencies.each do |currency| - if currency["id"].upcase == currency_name || currency["name"].upcase == currency_name || currency["symbol"].upcase == currency_name - return currency["id"] - end - end - return "" - end - def get_crypto_price(currency_name, convert_name) - from_name = get_crypto_id(currency_name) - if from_name == "" - return "Error: Unknown unit '" + currency_name + "'" + api_key = "55c16465-a562-4e8e-8631-416bbfd7ed82" + uri = URI("https://pro-api.coinmarketcap.com/v1/tools/price-conversion?symbol=" + currency_name + "&convert=" + convert_name + "&amount=1" ) + response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http| + request = Net::HTTP::Get.new(uri) + request["X-CMC_PRO_API_KEY"] = api_key + request["Accept"] = "application/json" + http.request(request) end - url = 'https://api.coinmarketcap.com/v1/ticker/' + from_name + '/?convert=' + convert_name - uri = URI(url) - response = Net::HTTP.get(uri) - currencies = JSON.parse(response) - price = currencies[0]["price_" + convert_name.downcase] - if !price - return "Error: Incompatible units. Can't convert " + currency_name + " to " + convert_name + result = JSON.parse(response.body) + if result["status"]["error_code"] == 0 + price = result["data"]["quote"][convert_name.upcase]["price"] + if !price + return "Error: Incompatible units. Can't convert " + currency_name + " to " + convert_name + end + else + return "Error: " + result["status"]["error_message"] end - return price + return price.to_s end args = ARGV.dup |