diff options
author | Joe Robinson <joe@lc8n.com> | 2015-07-01 11:32:03 +0100 |
---|---|---|
committer | Joe Robinson <joe@lc8n.com> | 2015-07-01 11:32:03 +0100 |
commit | f753c8d47e147b5645ea1585552e511af5e16622 (patch) | |
tree | f4e854acaff93601acc143a105935ad6cfe501a4 |
Initial commit
-rw-r--r-- | Gemfile | 4 | ||||
-rw-r--r-- | blaweather.rb | 65 |
2 files changed, 69 insertions, 0 deletions
@@ -0,0 +1,4 @@ +source 'https://rubygems.org' + +gem "rest-client" +gem "json" diff --git a/blaweather.rb b/blaweather.rb new file mode 100644 index 0000000..e539e6b --- /dev/null +++ b/blaweather.rb @@ -0,0 +1,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 |