summaryrefslogtreecommitdiff
path: root/blaweather.rb
diff options
context:
space:
mode:
Diffstat (limited to 'blaweather.rb')
-rw-r--r--blaweather.rb65
1 files changed, 65 insertions, 0 deletions
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