Skip to content
Permalink
master
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
executable file 39 lines (30 sloc) 1001 Bytes
#!/usr/bin/env ruby
# frozen_string_literal: true
# forecast.rb
# Author: William Woodruff
# ------------------------
# Fetches a weather forecast for a given query from Wunderground.
# Requires the "wunderground" gem.
# ------------------------
# This code is licensed by William Woodruff under the MIT License.
# http://opensource.org/licenses/MIT
begin
require "wunderground"
rescue LoadError
abort "I need the 'wunderground' gem."
end
KEY = ENV["WUNDERGROUND_API_KEY"]
abort("I need a $WUNDERGROUND_API_KEY in your environment.") unless KEY
wunder = Wunderground.new(KEY)
query = ARGV.join(" ")
abort("I need a weather query.") if query.empty?
begin
forecast = wunder.forecast_for(query)["forecast"]
# even period numbers are day forecasts, odds are night forecasts
days = forecast["txt_forecast"]["forecastday"].select { |w| w["period"].even? }
rescue StandardError => e
puts "Something blew up: #{e}."
end
days.each do |day|
puts "#{day["title"]} - #{day["fcttext"]}"
end