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.



30
31
32
33
34
# File 'lib/Zeta/plugins/weather.rb', line 30

def initialize(*args)
  @api_src = %w{wu noaa darksky owm}
  @store = Persist.new(File.join(Dir.home, '.zeta', 'cache', 'weather.pstore'))
  super
end

Instance Method Details

#almanac(msg, locale) ⇒ Object

?almanac <location>



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
128
129
130
131
# File 'lib/Zeta/plugins/weather.rb', line 99

def almanac(msg, locale)
  autocomplete = JSON.parse(open(URI.encode("http://autocomplete.wunderground.com/aq?query=#{locale}")).read)
  url = URI.encode("http://api.wunderground.com/api/#{Config.secrets[:wunderground]}/almanac/#{autocomplete['RESULTS'][0]['l']}.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

#hurricane(msg) ⇒ Object

?hurricane



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

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>



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/Zeta/plugins/weather.rb', line 61

def set_location(msg, query)
  # Establish source
  src = query[/:\w+/].gsub(/:/, '') if query[/:\w+/]
  query = query.gsub(/:\w+/, '').strip if query

  # Sanity Check
  true_src = @api_src.include?(src) ? src : 'wu'
  data = send("#{true_src}_src", query)

  # Error
  return msg.reply "No results found for #{query}." if data.nil?

  # Store and display general location
  serial_location = "#{query}::#{src}"
  @store[msg.user.to_s] = serial_location unless data.nil?
  msg.reply "Your location is now set to #{data.ac.name}, #{data.ac.c}!"
end

#weather(msg, query = nil) ⇒ Object

?w <location>



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/Zeta/plugins/weather.rb', line 37

def weather(msg, query=nil)
  # Pull data source and scrub query
  # Lookup user from pstore
  if !@store[msg.user.to_s].nil? && query.nil?
    stored_location, stored_source = @store[msg.user.to_s].split('::')
    stored_source = @api_src.include?(stored_source) ? stored_source : 'wu'
    data = send("#{stored_source}_src", stored_location)
    # location = geolookup(@store[msg.user.to_s])
    # data = wunderground_src(stored_location, false)
  elsif query.nil?
    return msg.reply 'No location set. ?setw <location> :(wu|darkscy|noaa|apixu|owm)'
  else
    # data = wu_src(query, true)
    src = query[/:\w+/].gsub(/:/, '') if query[/:\w+/]
    query = query.gsub(/:\w+/, '').strip if query
    true_src = @api_src.include?(src) ? src : 'wu'
    data = send("#{true_src}_src", query)
  end
  return msg.reply "No results found for #{query} with #{true_src} source." if data.nil?
  # return msg.reply 'Problem getting data. Try again later.' if data.nil?
  msg.reply(data.reply)
end