Class: Plugins::Weather

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

Overview

Forecast is a Cinch plugin for getting the weather forecast.

Author:

Instance Method Summary collapse

Methods included from Cinch::Plugin

#check?, #log2chan

Constructor Details

#initialize(*args) ⇒ Weather

Returns a new instance of Weather.



28
29
30
31
# File 'lib/Zeta/plugins/weather.rb', line 28

def initialize(*args)
  @store = Persist.new( File.join(Dir.home, '.zeta', 'cache', 'weather.pstore') )
  super
end

Instance Method Details

#almanac(msg, locale) ⇒ Object

?almanac <location>



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/Zeta/plugins/weather.rb', line 96

def almanac(msg,locale)
  url = URI.encode "http://api.wunderground.com/api/#{Config.secrets[:wunderground]}/almanac/q/#{locale}.json"
  location = JSON.parse(
       # RestClient.get(url)
       open(url).read
   )
  return msg.reply "No results found for #{query}." if location.nil?

  time = Time.now()

  data = OpenStruct.new(
      date: time.strftime('%B, %d %Y (%A) '),
      airport: location['almanac']['airport_code'],
      high_norm_f: location['almanac']['temp_high']['normal']['F'],
      high_norm_c: location['almanac']['temp_high']['normal']['C'],
      high_record_y: location['almanac']['temp_high']['recordyear'],
      high_record_f: location['almanac']['temp_high']['record']['F'],
      high_record_c: location['almanac']['temp_high']['normal']['C'],
      low_norm_f: location['almanac']['temp_low']['normal']['F'],
      low_norm_c: location['almanac']['temp_low']['normal']['C'],
      low_record_y: location['almanac']['temp_low']['recordyear'],
      low_record_f: location['almanac']['temp_low']['record']['F'],
      low_record_c: location['almanac']['temp_low']['normal']['C'],
  )

  reply_msg = "∴ Almanac #{data.date} ≈ Airport #{data.airport} " \
          "≈ Normal #{data.high_norm_f} F (#{data.high_norm_c} C) | #{data.low_norm_f} F (#{data.low_norm_c} C) " \
          "≈ High #{data.high_record_f} F (#{data.high_record_c} C) [#{data.high_record_y}] " \
          "≈ Low #{data.low_record_f} F (#{data.low_record_c} C) [#{data.low_record_y}] ∴"

  msg.reply(reply_msg)
end

#forecast(msg, query) ⇒ Object

?forecast <location>



34
35
36
37
38
39
40
41
42
# File 'lib/Zeta/plugins/weather.rb', line 34

def forecast(msg, query)
  location = geolookup(query)
  return msg.reply "No results found for #{query}." if location.nil?

  data = get_conditions(location)
  return msg.reply 'Problem getting data. Try again later.' if data.nil?

  msg.user.msg(weather_summary(data))
end

#geolookup(locale) ⇒ Object

-private



131
132
133
134
135
136
137
138
139
140
# File 'lib/Zeta/plugins/weather.rb', line 131

def geolookup(locale)
  url = URI.encode "http://api.wunderground.com/api/#{Config.secrets[:wunderground]}/geolookup/q/#{locale}.json"
  location = JSON.parse(
      # RestClient.get(url).force_encoding("UTF-8")
      open(url).read
  )
  location['location']['l']
rescue
  nil
end

#get_conditions(location) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/Zeta/plugins/weather.rb', line 142

def get_conditions(location)
  data = JSON.parse(
      open("http://api.wunderground.com/api/#{Config.secrets[:wunderground]}/alerts/conditions#{location}.json").read
      #RestClient.get("http://api.wunderground.com/api/#{Zsec.wunderground}/conditions#{location}.json")
  )
  current = data['current_observation']
  alerts = data['alerts'].empty? ? 'none' : data['alerts'].map { |l| l['type'] }.join(',')
  location_data = current['display_location']

  OpenStruct.new(
      county: location_data['full'],
      country: location_data['country'],

      lat: location_data['latitude'],
      lng: location_data['longitude'],

      observation_time: current['observation_time'],
      weather: current['weather'],
      temp_fahrenheit: current['temp_f'],
      temp_celcius: current['temp_c'],
      temperature: current['temperature_string'],
      relative_humidity: current['relative_humidity'],
      feels_like: current['feelslike_string'],
      uv_level: current['UV'],

      wind: current['wind_string'],
      wind_direction: current['wind_dir'],
      wind_degrees: current['wind_degrees'],
      wind_mph: current['wind_mph'],
      wind_gust_mph: current['wind_gust_mph'],
      wind_kph: current['wind_kph'],
      pressure_in: current['pressure_in'],
      pressure_mb: current['pressure_mb'],

      alerts: alerts,

      forecast_url: current['forecast_url']
  )
rescue
  nil
end

#hurricane(msg) ⇒ Object

?hurricane



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/Zeta/plugins/weather.rb', line 77

def hurricane(msg)
  url = URI.encode "http://api.wunderground.com/api/#{Zsec.wunderground}/currenthurricane/view.json"
  location = JSON.parse(
       # RestClient.get(url)
       open(url).read
   )
  return msg.reply "No results found for #{query}." if location.nil?
  reply_msg = "#{location['currenthurricane'][0]['stormInfo']['stormName_Nice']} " \
              "(#{location['currenthurricane'][0]['stormInfo']['stormNumber']}) "\
              "≈ Category #{location['currenthurricane'][0]['Current']['SaffirSimpsonCategory']} " \
              "≈ Wind #{location['currenthurricane'][0]['Current']['WindSpeed']['Mph']} mph " \
              "(#{location['currenthurricane'][0]['Current']['WindSpeed']['Kph']} kph) " \
              "≈ Wind Gust #{location['currenthurricane'][0]['Current']['WindGust']['Mph']} mph " \
              "(#{location['currenthurricane'][0]['Current']['WindGust']['Kph']} kph) " \
              "#{location['currenthurricane'][0]['Current']['Time']['pretty']}"
  msg.reply(reply_msg)
end

#set_location(msg, query) ⇒ Object

?setw <location>



68
69
70
71
72
73
74
# File 'lib/Zeta/plugins/weather.rb', line 68

def set_location(msg,query)
  location = geolookup(query)
  return msg.reply "No results found for #{query}." if location.nil?
  @store[msg.user.to_s] = query unless location.nil?
  data = get_conditions(location)
  msg.reply "Your location is now set to #{data.county}, #{data.country}!"
end

#weather(msg, query = nil) ⇒ Object

?w <location>



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/Zeta/plugins/weather.rb', line 45

def weather(msg, query=nil)
  if !@store[msg.user.to_s].nil? && query.nil?
    location = geolookup(@store[msg.user.to_s])
  elsif query.nil?
    return msg.reply 'No location set. ?setw <location>'
  else
    location = geolookup(query)
  end
  return msg.reply "No results found for #{query}." if location.nil?

  data = get_conditions(location)
  return msg.reply 'Problem getting data. Try again later.' if data.nil?

  #[ Clarkston, WA, United States | Cloudy | Temp: 34 F (1 C) | Humidity: 73% | Winds: 8 mph ]
  reply_data = "#{data.county}, #{data.country} " \
              "#{data.weather} #{data.temperature} " \
              "≈ Humidity: #{data.relative_humidity} " \
              "≈ Pressure: #{data.pressure_in} psi (#{data.pressure_mb} mmHg) " \
              "≈ Wind: #{data.wind} ≈ Alerts: #{data.alerts}"
  msg.reply(reply_data)
end

#weather_summary(data) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/Zeta/plugins/weather.rb', line 184

def weather_summary(data)
  %Q{
      Forecast for: #{data.county}, #{data.country}
      Latitude: #{data.lat}, Longitude: #{data.lng}
      Weather is #{data.weather}, #{data.feels_like}
      UV: #{data.uv_level}, Humidity: #{data.relative_humidity}
      Wind: #{data.wind}
      Direction: #{data.wind_direction}, Degrees: #{data.wind_degrees},
      #{data.observation_time}
      More Info: #{data.forecast_url}}
rescue
  'Problem fetching the weather summary. Try again later.'
end