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.



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

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

#set_location(msg, query) ⇒ Object

?setw <location>



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

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 : 'darksky'
  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>



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

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 : 'darksky'
    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> :(darksky|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 : 'darksky'
    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