class Thundersnow::Weather

Public Class Methods

new(location) click to toggle source
# File lib/thundersnow/weather.rb, line 6
def initialize(location)
  @xml = get_weather_xml(location)
end

Public Instance Methods

city() click to toggle source
# File lib/thundersnow/weather.rb, line 42
def city
  @city ||= read_attr(@xml, '//forecast_information/city')
end
condition() click to toggle source
# File lib/thundersnow/weather.rb, line 53
def condition
  read_attr(current_conditions, 'condition')
end
current() click to toggle source
# File lib/thundersnow/weather.rb, line 20
def current
  values = ["Current Conditions for #{city}"]
  values << condition
  values << temperatures
  values << wind
  values << humidity
  values.join("\n")
end
current_conditions() click to toggle source
# File lib/thundersnow/weather.rb, line 77
def current_conditions
  @current_conditions ||= @xml.xpath('//current_conditions')
end
day_of_week(day) click to toggle source
# File lib/thundersnow/weather.rb, line 46
def day_of_week(day)
  day_of_week = read_attr(day, 'day_of_week')
  return "Today" if day_of_week == today
  return "Tomorrow" if day_of_week == tomorrow
  day_of_week
end
forecast() click to toggle source
# File lib/thundersnow/weather.rb, line 29
def forecast
  values = ["Weather Forecast for #{city}"]

  @xml.xpath('//forecast_conditions').each do |day|
    values << "Forecast for #{day_of_week(day)}"
    values << "High: #{read_attr(day, 'high')+deg_symbol}F / Low: #{read_attr(day, 'low')+deg_symbol}F"
    values << "Conditions: #{read_attr(day, 'condition')}"
    values << "-------------------------"
  end

  values.join("\n")
end
get_weather_xml(location) click to toggle source
# File lib/thundersnow/weather.rb, line 10
def get_weather_xml(location)
  uri = URI.encode "http://www.google.com/ig/api?weather=#{location}"
  Nokogiri::XML(open(uri))
end
humidity() click to toggle source
# File lib/thundersnow/weather.rb, line 65
def humidity
  read_attr(current_conditions, 'humidity')
end
temp_c() click to toggle source
# File lib/thundersnow/weather.rb, line 73
def temp_c
  read_attr(current_conditions, 'temp_c')
end
temp_f() click to toggle source
# File lib/thundersnow/weather.rb, line 69
def temp_f
  read_attr(current_conditions, 'temp_f')
end
temperatures() click to toggle source
# File lib/thundersnow/weather.rb, line 57
def temperatures
  "#{temp_f} #{deg_symbol}F / #{temp_c} #{deg_symbol}C"
end
valid?() click to toggle source

Invalid locations contain problem_cause xml tag

# File lib/thundersnow/weather.rb, line 16
def valid?
  @xml.xpath('//weather/problem_cause').size == 0
end
wind() click to toggle source
# File lib/thundersnow/weather.rb, line 61
def wind
  read_attr(current_conditions, 'wind_condition')
end

Private Instance Methods

deg_symbol() click to toggle source
# File lib/thundersnow/weather.rb, line 95
def deg_symbol
  HTMLEntities.new.decode("&deg;")
end
read_attr(root, node) click to toggle source
# File lib/thundersnow/weather.rb, line 83
def read_attr(root, node)
  root.xpath(node).attribute('data').to_s
end
today() click to toggle source
# File lib/thundersnow/weather.rb, line 87
def today
  Time.now.strftime('%a')
end
tomorrow() click to toggle source
# File lib/thundersnow/weather.rb, line 91
def tomorrow
  (Time.now + 86400).strftime('%a')
end