summaryrefslogtreecommitdiff
path: root/blaunits.rb
diff options
context:
space:
mode:
authorJoe Robinson <joe@lc8n.com>2020-04-28 12:27:41 +0100
committerJoe Robinson <joe@lc8n.com>2020-04-28 12:27:41 +0100
commit4cf73204a1349693e09d5b579aa3109b812d7469 (patch)
tree11706a717da0b5aec2a2f75d900d3a4b13d5f8d7 /blaunits.rb
parent8d92794568083022ae402968664098662237eb72 (diff)
Use new CoinMarketCap API for cryptocurrency
Diffstat (limited to 'blaunits.rb')
-rwxr-xr-xblaunits.rb42
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