summaryrefslogtreecommitdiff
path: root/blaunits.rb
blob: 2245dffece8eb9c70b7c1923a4c0dd039a9ea23e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/ruby

require 'net/http'
require 'json'

VERSION = "2.3.0"
BINARY_NAME = "units"

begin
    def get_crypto_price(currency_name, convert_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
        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.to_s
    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 words.length == 3 && mid == 1
        num = 1
        from = words[0]
        to = words[2]
    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