Skip to content

Instantly share code, notes, and snippets.

@s5csimmons
Last active May 7, 2016 06:50
Show Gist options
  • Save s5csimmons/527a6e02499adffd25f9 to your computer and use it in GitHub Desktop.
Save s5csimmons/527a6e02499adffd25f9 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/ruby
#Changelog:
#v1.2
#Added the following options and functionality:
# -h: There's now a help screen to show you how to use the command
# -n: View current weather, a.k.a. the old output.
# -t: View the forecast for tomorrow.
#v1.1
#We remove the unnecessary "stats" variable and merge it into one query.
#We also do the formatting at the beginning at on the same line making for shorter,
#easier to read code.
require 'open-uri'
require 'json'
if ARGV.empty? || ARGV[0] == "-h"
puts "
Simple tool that gathers either the current conditions or tomorrow's weather from Weather Underground.
Only the first argument is recognized, anything additional will be ignored.
Usage:
-h: Prints this help dialog.
-n: See the current weather conditions.
-t: See tomorrow's forecast.
"
elsif ARGV[0] == "-t"
open("http://api.wunderground.com/api/APIKEY/forecast/bestfct:1/q/CA/San_Francisco.json") do |website|
forecast = JSON.parse(website.read)["forecast"]["txt_forecast"]["forecastday"]
puts "Tomorrow's forecast: %s" % forecast[2]['fcttext']
end
elsif ARGV[0] == "-n"
open("http://api.wunderground.com/api/APIKEY/conditions/bestfct:1/q/CA/San_Francisco.json") do |website|
now = JSON.parse(website.read)["current_observation"]
current = now.values_at(*%w(weather temp_f relative_humidity wind_mph feelslike_f)).map! {
|element| element.is_a?(String) ? element.downcase : element
}
puts "It's currently %s and %1.1f degrees with %s humidity.
Winds are currently %1.1f mph and it feels like %1.1f degrees." % current
end
else
puts "
Invalid Argument: %s
Usage: weather.rb [-hnt]
" % ARGV[0]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment