class Lita::Handlers::YourWeather

Constants

URL

Variables

Public Instance Methods

get_location(location) click to toggle source

Get location

# File lib/lita/handlers/your_weather.rb, line 50
def get_location(location)
  location = config.default_location if location.eql? ''
  location
end
request(url, location) click to toggle source

Request method

# File lib/lita/handlers/your_weather.rb, line 56
def request(url, location)
  http_response = http.get(URL + url, q: location)
  JSON.parse(http_response.body)
end
weather(response) click to toggle source

Current Weather

# File lib/lita/handlers/your_weather.rb, line 19
def weather(response)
  location = get_location(response.matches[0][0])

  if response.message.body.include? 'weather f'
    data = request('/forecast.json?key=' + config.api_key + '&days=7', location)
  elsif response.message.body.include? 'weather i'
    if location.downcase.include? 'edmonton,alberta'
      response.reply('https://ssl.eas.ualberta.ca/sitecore/camera/live/camera-ne.jpg')
      response.reply('https://ssl.eas.ualberta.ca/sitecore/camera/live/camera-nw.jpg')
      response.reply('https://ssl.eas.ualberta.ca/sitecore/camera/live/camera-w.jpg')
    end
  else
    data = request('/current.json?key=' + config.api_key, location)
  end

  unless data.nil?
    return response.reply('Error: ' + data['error']['message']) if data['error']

    if response.message.body.include? 'weather f'
      response.reply('The forecast for ' + data['location']['name'] + ', ' + data['location']['region'] + ', ' + data['location']['country'] + ':')
      data['forecast']['forecastday'].each do |object|
        response.reply('On ' + object['date'] + ' forecasted is a high of ' + object['day']['maxtemp_c'].to_s + "\xC2\xB0" + 'C' + ' and a low of ' + object['day']['mintemp_c'].to_s + "\xC2\xB0" + 'C It is expected to be ' + object['day']['condition']['text'])
      end
    else
      response.reply('http:' + data['current']['condition']['icon'])
      response.reply(data['location']['name'] + ', ' + data['location']['region'] + ', ' + data['location']['country'] + ' currently is ' + data['current']['condition']['text'] + ' and ' + data['current']['temp_c'].to_s + "\xC2\xB0" + 'C. It feels like ' + data['current']['feelslike_c'].to_s + "\xC2\xB0" + 'C')
    end
  end
end