Class: Moneypenny::Responders::Weather

Inherits:
Object
  • Object
show all
Defined in:
lib/moneypenny/responders/weather.rb

Class Method Summary collapse

Class Method Details

.helpObject



8
9
10
# File 'lib/moneypenny/responders/weather.rb', line 8

def self.help
  [ 'weather in Seattle?', 'returns the current weather in Seattle' ]
end

.respond(message) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/moneypenny/responders/weather.rb', line 12

def self.respond(message)
  if message.downcase.include?('weather') && (query = message.match(/in\ (.+)(\?|)\z/i))
    doc = Nokogiri::HTML open("http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=#{CGI::escape query[1]}")
    location    = doc.xpath('//current_observation/display_location/full').text.gsub(',', '')
    weather     = doc.xpath('//current_observation/weather').text.downcase.strip
    temperature = doc.xpath('//current_observation/temp_f').text.downcase.strip
    wind        = doc.xpath('//current_observation/wind_string').text.downcase.strip
    visibility  = doc.xpath('//current_observation/visibility_mi').text.downcase.strip
    "It's #{temperature}° in #{location}, #{weather}, wind #{wind}, #{visibility} miles visibility."
  else
    false
  end
end