#!/usr/bin/ruby require 'net/http' require 'json' VERSION = "2.2.4" 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 + "'" 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 end return price end args = ARGV.dup if args.empty? input = STDIN.gets else input = "" args.each do |arg| if input != "" input = input + " " + arg.dup else input = arg.dup end end end input.strip! if input == "-v" or input == "--version" puts "blaconvert v"+VERSION exit end mid = 0 from = "" to = "" num = 0 words = input.split(" ") for i in 0..words.length-1 if words[i] == "in" or words[i] == "to" mid = i end end if words.length == 3 && mid == 0 num = Float(words[0]) from = words[1] to = words[2] elsif words.length == 2 && mid == 0 num = 1 from = words[0] to = words[1] elsif mid > 0 for i in 1..mid-1 from += words[i] end for i in mid+1..words.length-1 to += words[i] end num = Float(words[0]) else puts "Invalid input! Try !convert [amount] [from-unit] to [to-unit] e.g !convert 2 cm to inches" exit end if from.upcase == "F" && to.upcase == "C" temp = (num - 32) / 1.8 puts "#{num} F = #{temp} C" elsif from.upcase == "C" && to.upcase == "F" temp = (num * 1.8) + 32 puts "#{num} C = #{temp} F" else out = %x(#{BINARY_NAME} -t1 #{from} #{to}) outs = out.split(" ") if outs[0] == "Unknown" from.upcase! out = %x(#{BINARY_NAME} -t1 #{from} #{to}) outs = out.split(" ") if outs[0] == "Unknown" to.upcase! out = %x(#{BINARY_NAME} -t1 #{from} #{to}) outs = out.split(" ") if outs[0] == "Unknown" out = "Error: " + out units_failed = true elsif outs[0] == "conformability" out = "Error: Incompatible units. Can't convert from #{from} to #{to}" units_failed = true end elsif outs[0] == "conformability" out = "Error: Incompatible units. Can't convert from #{from} to #{to}" units_failed = true end elsif outs[0] == "conformability" out = "Error: Incompatible units. Can't convert from #{from} to #{to}" units_failed = true end if units_failed units_error = out out = get_crypto_price(from, to) if out.split(" ")[0] == "Error:" reverse_out = get_crypto_price(to, from) if reverse_out.split(" ")[0] == "Error:" if units_error.split(" ")[1] == "Incompatible" puts units_error else puts out end exit else out = 1/Float(reverse_out) end end end puts "#{num} #{from} = #{num * Float(out)} #{to}" end end