Skip to content

Instantly share code, notes, and snippets.

@dan-c-underwood
Last active August 29, 2015 14:22
Show Gist options
  • Save dan-c-underwood/814336f119c258fee961 to your computer and use it in GitHub Desktop.
Save dan-c-underwood/814336f119c258fee961 to your computer and use it in GitHub Desktop.
Forecast.io based script designed for Today-Scripts. Powered by Forecast.
require "json"
require "net/http"
require "yaml"
require "colorize"
require "forecast_io"
def format_temp(temp)
temp_i = temp.to_i
temp = temp + "Β°C"
case
when temp_i <= 0
temp.colorize(:blue)
when temp_i < 5
temp.colorize(:light_blue)
when temp_i < 10
temp.colorize(:cyan)
when temp_i < 15
temp.colorize(:light_cyan)
when temp_i < 20
temp.colorize(:yellow)
when temp_i < 25
temp.colorize(:light_yellow)
when temp_i < 30
temp.colorize(:light_red)
when temp_i >= 30
temp.colorize(:red)
else
temp
end
end
def to_celsius(temp)
((temp - 32) * 5.0 / 9.0).round(1)
end
config = YAML.load_file(ARGV[0])
ForecastIO.configure do |configuration|
configuration.api_key = config["api_key"]
end
lat = ARGV[1]
lon = ARGV[2]
forecast = ForecastIO.forecast(lat, lon, params: { exclude: "minutely,hourly,daily,alerts,flags"})
weatherDesc = forecast.currently.summary
weatherIcon = case forecast.currently.icon
when "clear-day" then "β˜€οΈ"
when "clear-night" then "πŸŒ™"
when "rain" then "πŸ’§"
when "snow", "sleet" then "❄️"
when "wind" then "πŸ’¨"
when "fog" then "🌁"
when "cloudy" then "☁️"
when "partly-cloudy-day", "partly-cloudy-night" then "⛅️"
when "thunderstorm" then "⚑️"
else "🌈"
end
precip = (forecast.currently.precipProbability * 100).round(1).to_s + "%"
temp = format_temp(to_celsius(forecast.currently.temperature).to_s)
tempFeel = format_temp(to_celsius(forecast.currently.apparentTemperature).to_s)
puts "Weather".bold.colorize(:magenta) + ": #{weatherDesc} #{weatherIcon}"
puts "Precipitation".bold.colorize(:cyan) + ": #{precip}"
puts ""
puts "Temperature".bold.colorize(:red) + ": #{temp}, " +
"Feels".bold.colorize(:yellow) + ": #{tempFeel}"
puts "Powered by Forecast".bold.colorize(:blue)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment