This is a VERY quick and dirty script I threw together to see if the conditions were right to go skating on a local pond (based on a friends best guess).
Simply, it checks an online weather service for the overnight temperatures of the past 10 days and if every night maintained temperatures below freezing, tells you to go skating. Otherwise, no skating for you.
require 'rubygems'
require 'simplehttp'
require 'time'
class TimedWeather
attr_accessor :date_time, :temp
def initialize(date_time_string, temp_string)
@date_time = DateTime.strptime(date_time_string, '%Y-%m-%d %H:%M:%S')
@temp = temp_string.to_f
end
def to_s
"[(#{@date_time})-(#{@temp})"
end
end
def build_daily_url(date)
"http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=KMOSTLOU11&day=#{date.day}&year=#{date.year}&month=#{date.month}&format=1"
end
def find_timed_weather_for_date(date = Date.today)
response = SimpleHttp.get(build_daily_url(date))
records = response.gsub(/\n<br>/, '').gsub(/<br>/, '').split(/\n/)[2...-1]
records.map do |text_record|
split = text_record.split(/,/)
TimedWeather.new(split[0], split[1])
end
end
def conducive_day?(daily_temps)
not daily_temps.select { |time| conducive_time?(time) }.map { |temp| conducive_temp?(temp) }.include?(false)
end
def conducive_time?(time)
time.date_time.hour > 17 or time.date_time.hour < 6
end
def conducive_temp?(temp)
temp.temp < 32.0
end
conducive = true
(0..9).each do |offset|
if not conducive_day?(find_timed_weather_for_date(Date.today - offset))
conducive = false
break
end
end
if conducive
puts "GO SKATE! ...... maybe"
else
puts "NO SKATING TODAY!"
end
No comments:
Post a Comment