class Weather
Public Instance Methods
current_conditions()
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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_operator()
click to toggle source
# 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)
click to toggle source
def translate(message)
in_language = @bing.detect message @bing.translate message, :from => in_language, :to => "en"
end
# File lib/cogibara/operators/weather.rb, line 15 def location_to_coordinates(location) Geocoder.coordinates(location) end
process(message)
click to toggle source
# 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_forecast()
click to toggle source
# 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_forecast()
click to toggle source
# File lib/cogibara/operators/weather.rb, line 60 def week_forecast say @forecast.daily.summary end