Class: Weather

Inherits:
Cogibara::OperatorBase show all
Defined in:
lib/cogibara/operators/weather.rb

Instance Attribute Summary

Attributes inherited from Cogibara::OperatorBase

#clientID, #message_structure, #message_text, #operator_config

Instance Method Summary collapse

Methods inherited from Cogibara::OperatorBase

#confirm, #initialize, #process_file, #receive_message, #say

Constructor Details

This class inherits a constructor from Cogibara::OperatorBase

Instance Method Details

#current_conditionsObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/cogibara/operators/weather.rb', line 42

def current_conditions
  # if @forecast.minutely
  #   say "now: #{@forecast.minutely.summary}"
  # end
  curr_summary = " Currently " + @forecast.currently.summary + ", "
  curr_cond = "#{@forecast.currently.temperature.round} degrees, cloud cover #{(@forecast.currently.cloudCover * 100).round}% "
  next_cond =  @forecast.minutely ? @forecast.minutely.summary : ""
  # next_cond =  @forecast.minutely ? "Then " + @forecast.minutely.summary : ""
  curr_summary + curr_cond + "then, " + next_cond
end

#get_forecast_period(message) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cogibara/operators/weather.rb', line 28

def get_forecast_period(message)
  info = message.structure[:entities]

  if info[:daterange]
    start_d = Date.parse(info[:daterange][0][:start])
    end_d = Date.parse(info[:daterange][0][:end])
    start_d..end_d
  elsif info[:time]
    info[:time]=="tomorrow" ? Date.today + 1 : Date.today
  else
    nil
  end
end

#get_location(message) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/cogibara/operators/weather.rb', line 19

def get_location(message)
  info = message.structure
  if info[:entities][:location]
    location_to_coordinates(info[:entities][:location][0])
  else
    [43.092, -89.369]
  end
end

#initialize_operatorObject



6
7
8
# File 'lib/cogibara/operators/weather.rb', line 6

def initialize_operator
  Forecast::IO.api_key = self.operator_config["API_KEY"]
end

#location_to_coordinates(location) ⇒ Object

def translate(message)

in_language = @bing.detect message
@bing.translate message, :from => in_language, :to => "en"

end



15
16
17
# File 'lib/cogibara/operators/weather.rb', line 15

def location_to_coordinates(location)
  Geocoder.coordinates(location)
end

#process(message) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cogibara/operators/weather.rb', line 64

def process(message)
  return nil if message.text.split.include?("cool")

  @loc = get_location(message)
  @date = get_forecast_period(message)
  @forecast = Forecast::IO.forecast(@loc[0],@loc[1])

  unless @date
    current_conditions
  else
    if @date.is_a? Range
      if @date.first == Date.today || @date.first == Date.today+1
        todays_forecast
      else
        week_forecast
      end
    else
      if @date == Date.today || @date == Date.today + 1
        todays_forecast
      else
        week_forecast
      end
    end
  end
end

#todays_forecastObject



53
54
55
56
57
58
# File 'lib/cogibara/operators/weather.rb', line 53

def todays_forecast
  temps = @forecast.hourly.data[0..18].map{|hr| hr.temperature}
  max_t = temps.each_with_index.max
  min_t = temps.each_with_index.min
  @forecast.hourly.summary + " The high temperature will be #{max_t[0].round} degrees, in #{max_t[1]} hours, and the low will be #{min_t[0].round} degrees, in #{min_t[1]} hours"
end

#week_forecastObject



60
61
62
# File 'lib/cogibara/operators/weather.rb', line 60

def week_forecast
  say @forecast.daily.summary
end