From f753c8d47e147b5645ea1585552e511af5e16622 Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Wed, 1 Jul 2015 11:32:03 +0100 Subject: Initial commit --- Gemfile | 4 ++++ blaweather.rb | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 Gemfile create mode 100644 blaweather.rb diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..483961a --- /dev/null +++ b/Gemfile @@ -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 -- cgit v1.2.3