summaryrefslogtreecommitdiff
path: root/blaweather.rb
blob: e539e6ba29b3de038fc226f1a213f4257f8dc386 (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
require 'rest-client'
require 'json'

VERSION = "2.0"

def get_location location
    url = "https://maps.googleapis.com/maps/api/geocode/json?address="
    api_key = "AIzaSyCUJgcjfq20vONzLuNrSuVgT4nuMvz7d2c"
    response = RestClient.get(url+location+"&key="+api_key)
    @data = JSON.parse response
    location = @data['results'][0]['geometry']['location']
    return location
end

def get_weather lat, long
    url = "https://api.forecast.io/forecast/"
    api_key = "f1ce0bcdc3d972bc0529bb353f8ec02a"
    response = RestClient.get(url+api_key+"/"+lat.to_s+","+long.to_s+"?units=si")
    @data = JSON.parse response
    return @data
end

def build_forecast forecast
    cur = forecast['currently']
    cur_weather = cur['summary']
    cur_temp = cur['temperature']
    min = forecast['minutely']
    hour = forecast['hourly']
    day = forecast['daily']
    max_temp = day['data'][0]['temperatureMax']
    min_temp = day['data'][0]['temperatureMin']
    day_forecast = forecast['daily']['summary']
    
    output = cur_weather+". Cur: "+cur_temp.to_s+"°C. Max: "+max_temp.to_s+"°C. Min: "+min_temp.to_s+"°C "
    unless min.nil?    
        output += min['summary'] + " "
    end
    output += hour['summary']+" "+day['summary']
end
   
begin
    args = ARGV.dup
    if args.empty?
       input = STDIN.gets
    else
        input = ""
        args.each do |arg|
            if input != ""
            input = input + " " + arg
            else
                input = arg
            end
        end
    end
    input.strip!
    if input == "-v" or input ==  "--version"
        puts "blaweather v"+VERSION
        exit
    end

    input = CGI.escape(input)
    location = get_location(input)
    forecast = get_weather(location['lat'], location['lng'])
    puts build_forecast(forecast)
end