Class: Cinch::Plugins::Weather

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

Defined Under Namespace

Classes: Wx

Constant Summary collapse

MAPPING =

Format mapping for metric and imperial values

{
  metric: {t: "c", d: "km", s: "kph", p: %w{metric mm}},
  imperial: {t: "f", d: "mi", s: "mph", p: %w{in in}}
}
MOON_PHASES =

Moon phases based on the age of the moon.

[
  'New Moon ๐ŸŒš',
  'Waxing Crescent ๐ŸŒ’',
  'First Quarter ๐ŸŒ“',
  'Waxing Gibbous ๐ŸŒ”',
  'Full Moon ๐ŸŒ',
  'Waning Gibbous ๐ŸŒ–',
  'Third Quarter ๐ŸŒ—',
  'Waning Crescent ๐ŸŒ˜'
]
UNICODE_ICONS =

Fancy unicode icons!

{
  chanceflurries:'๐ŸŒจโ“',
  chancerain:'๐ŸŒฆโ“',
  chancesleet:'๐ŸŒจ๐ŸŒฆโ“',
  chancesnow:'โ˜ƒ๏ธโ“',
  chancetstorms:'โ›ˆโ“',
  clear:'๐ŸŒ™',
  cloudy:'โ˜๏ธ',
  flurries:'๐ŸŒจ',
  fog:'๐ŸŒซ',
  hazy:'๐ŸŒซ',
  mostlycloudy:'๐ŸŒฅ',
  mostlysunny:'๐ŸŒค',
  partlycloudy:'โ›…',
  partlysunny:'โ›…',
  sleet:'๐ŸŒจ๐ŸŒฆ',
  rain:'โ˜”๏ธ',
  snow:'โ˜ƒ๏ธ',
  sunny:'๐ŸŒž',
  tstorms:'โ›ˆ',
  unknown:'โ˜„๏ธ'
}

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Weather


58
59
60
61
62
63
# File 'lib/cinch/plugins/weather.rb', line 58

def initialize(*args)
  super
  Geocoder.configure do |c|
    c.timeout = 30
  end
end

Instance Method Details

#execute_forecast(m, switches, query) ⇒ Object


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/cinch/plugins/weather.rb', line 94

def execute_forecast(m, switches, query)
  # Switch handling
  metric = true
  switches.chars {|c|
    case c.intern
    when :i then metric = false
    end
  } unless switches.nil?

  # Fetching weather conditions!
  geo = Geocoder.search(query)
  if !geo.empty?
    if geo.count == 1
      m.user.msg fetch_forecast(geo.first.coordinates * ',', metric)
    else
      list = geo.each_with_object([]) {|addr, memo| memo << addr.formatted_address }
      m.user.notice "#{Format(:green,:bold,"Whoops!")} ยท It appears that your search was a bit too broad. Did you mean: #{list * '; '}"
    end
  else
    m.user.notice "#{Format(:green,:bold,"Whoops!")} ยท Your search has not returned any results."
  end
rescue => e
  errmsg = e.message.respond_to?(:description) ? e.message.description : e.message.to_s
  m.user.notice "#{Format(:red,:bold,"Uhoh!")} ยท #{errmsg}"
end

#execute_weather(m, switches, query) ⇒ Object


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cinch/plugins/weather.rb', line 66

def execute_weather(m, switches, query)
  # Switch handling
  metric, simple = true, false
  switches.chars {|c|
    case c.intern
    when :i then metric = false
    when :s then simple = true
    end
  } unless switches.nil?

  # Fetching weather conditions!
  geo = Geocoder.search(query)
  if !geo.empty?
    if geo.count == 1
      m.reply fetch_conditions(geo.first.coordinates * ',', metric, simple)
    else
      list = geo.each_with_object([]) {|addr, memo| memo << addr.formatted_address }
      m.user.notice "#{Format(:green,:bold,"Whoops!")} ยท It appears that your search was a bit too broad. Did you mean: #{list * '; '}"
    end
  else
    m.user.notice "#{Format(:green,:bold,"Whoops!")} ยท Your search has not returned any results."
  end
rescue => e
  errmsg = e.message.kind_of?(Hashie::Mash) ? e.message.description : e.message.to_s
  m.user.notice "#{Format(:red,:bold,"Uhoh!")} ยท #{errmsg}"
end