Class: Weather

Inherits:
Object
  • Object
show all
Includes:
Cinch::Plugin
Defined in:
lib/rateless_bot/plugins/weather.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Weather

Returns a new instance of Weather.



11
12
13
14
# File 'lib/rateless_bot/plugins/weather.rb', line 11

def initialize(*args)
  super
  @options = { units: 'metric', APPID: config[:api_key] }
end

Instance Method Details

#execute(m) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/rateless_bot/plugins/weather.rb', line 18

def execute(m)
  city = m.message.split('!weather ')[1]

  current_weather = OpenWeather::Current.city(city, @options)

  m.reply make_current_weather_str(current_weather)
end

#helpObject



5
6
7
# File 'lib/rateless_bot/plugins/weather.rb', line 5

def help
  '!weather CITY/ZIP - gets the current temperature for a given city/zip'
end

#make_current_weather_str(current_weather) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rateless_bot/plugins/weather.rb', line 26

def make_current_weather_str(current_weather)
  if current_weather['cod'] == '404'
    return 'Error: City not found'
  elsif current_weather['cod'] != 200 # number is not a string; report bug
    return "Error: response code #{current_weather['cod']}"
  end

  city = current_weather['name']
  country = current_weather['sys']['country']
  humidity = current_weather['main']['humidity']
  temp_celsius = current_weather['main']['temp'].round(1)
  temp_fahrenheit = (temp_celsius * 1.8 + 32).round(1)
  weather_description = current_weather['weather'][0]['description']

  s = "Weather for #{city} (#{country}): "
  s += "#{temp_celsius}°C (#{temp_fahrenheit}°F); #{humidity}% humidity; "
  s += "#{weather_description}"
end