Skip to content

Instantly share code, notes, and snippets.

@zentrification
Created April 27, 2021 18:53
Show Gist options
  • Save zentrification/0758390cff9801a5ff7677a77657795e to your computer and use it in GitHub Desktop.
Save zentrification/0758390cff9801a5ff7677a77657795e to your computer and use it in GitHub Desktop.
require 'httparty'
@coins = { :bitcoin => "BTC", :ethereum => "ETH" }
@currency = 'USD'
@api_key = 'XXXX'
@request_url = "https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest?slug=#{@coins.keys.join(",")}"
@headers = {
"Accepts" => "application/json",
"X-CMC_PRO_API_KEY" => @api_key
}
begin
response = HTTParty.get(@request_url, headers: @headers)
rescue StandardError
print 'Failed request!'
response = false
end
if response
response['data'].each do |current_coin|
icon = (@coins[current_coin[1]['slug'].to_sym]).to_s
value = current_coin[1].dig('quote', @currency, 'price')&.round(0)
print "#{icon}: $#{value} "
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment