Skip to content

Commit

Permalink
Fixing up tests as I rip out lambda.
Browse files Browse the repository at this point in the history
  • Loading branch information
onewheelskyward committed Jan 3, 2019
1 parent eb1b631 commit cbc3dbd
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 105 deletions.
4 changes: 4 additions & 0 deletions lib/lita/handlers/forecasts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def emoji_rain_forecast(rain_chances)
"1hr #{precip_type} probability #{(Time.now).strftime('%H:%M').to_s}|#{str}|#{(Time.now + 3600).strftime('%H:%M').to_s} max #{(max.to_f * 100).round(2)}%"
end

###
# Define rubydoc
# rain_chances: Expects this to be an array of chances of rain.
#
def ansi_rain_forecast(rain_chances, precip_types = [])
(str, precip_type) = do_the_rain_chance_thing(rain_chances, ansi_chars, precip_types)
max = rain_chances.max
Expand Down
137 changes: 79 additions & 58 deletions lib/lita/handlers/irc_handlers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ module IrcHandlers
#-# Handlers
def handle_irc_forecast(response)
location = geo_lookup(response.user.name, response.match_data[1])
# forecast = get_forecast_io_results(response.user.name, location)
uri = config.lambda_uri + "?loc=#{location.latitude},#{location.longitude}"
Lita.logger.info(uri)
forecast = JSON.parse(RestClient.get(uri))
forecast = get_forecast_io_results(response.user.name, location)

reply = ''

Expand All @@ -21,56 +18,58 @@ def handle_irc_forecast(response)

def handle_irc_ansirain(response)
location = geo_lookup(response.user.name, response.match_data[1])
Lita.logger.debug(location.inspect)
# forecast = get_forecast_io_results(response.user.name, location)
uri = config.lambda_uri + "/minutely/probability?loc=#{location.latitude},#{location.longitude}"
Lita.logger.debug(uri)
forecast = JSON.parse(RestClient.get(uri))
precip_types = JSON.parse(RestClient.get(config.lambda_uri + "/minutely/precip-type?loc=#{location.latitude},#{location.longitude}"))
Lita.logger.debug(forecast)
Lita.logger.debug(precip_types)

reply = ''

if location.location_name
reply = location.location_name + ' '
end

reply += ansi_rain_forecast(forecast, precip_types)
begin
forecast = get_forecast_io_results(response.user.name, location)

rain_chances = forecast['minutely']['data'].map{|i| i['precipProbability']}
precip_types = forecast['minutely']['data'].map{|i| i['precipType']}

reply += ansi_rain_forecast(rain_chances, precip_types)
rescue NoMethodError => e
reply += 'No minute-by-minute data available.'
end

response.reply reply
end

def handle_irc_ascii_rain(response)
location = geo_lookup(response.user.name, response.match_data[1])
uri = config.lambda_uri + "/minutely/probability?loc=#{location.latitude},#{location.longitude}"
Lita.logger.debug(uri)
forecast = JSON.parse(RestClient.get(uri))
forecast = get_forecast_io_results(response.user.name, location)
response.reply location.location_name + ' ' + ascii_rain_forecast(forecast)
end

def handle_irc_emojirain(response)
location = geo_lookup(response.user.name, response.match_data[1])
uri = config.lambda_uri + "/minutely/probability/condensed?loc=#{location.latitude},#{location.longitude}"
Lita.logger.debug(uri)
begin
forecast = JSON.parse(RestClient.get(uri))
rescue JSON::ParserError => e
forecast = ''
forecast = get_forecast_io_results(response.user.name, location)

probs = forecast['minutely']['data'].map{|i| i['precipProbability']}
new_probs = []
prob = 0
probs.each_with_index do |p, i|
prob += p.to_f
if i % 4 == 0
new_probs.push(p / 4)
prob = 0
end
end
response.reply location.location_name + ' ' + emoji_rain_forecast(forecast)

response.reply location.location_name + ' ' + emoji_rain_forecast(new_probs)
end

def handle_irc_all_the_things(response)
location = geo_lookup(response.user.name, response.match_data[1])
uri = config.lambda_uri + "?loc=#{location.latitude},#{location.longitude}"
Lita.logger.debug(uri)
forecast = JSON.parse(RestClient.get(uri))
# forecast = get_forecast_io_results(response.user.name, location)
forecast = get_forecast_io_results(response.user.name, location)

response.reply location.location_name + ' ' + forecast_text(forecast)
response.reply location.location_name + ' ' + ansi_rain_forecast(forecast)
response.reply location.location_name + ' ' + ansi_rain_intensity_forecast(forecast)
response.reply location.location_name + ' ' + ansi_temp_forecast(forecast)
response.reply location.location_name + ' ' + ansi_rain_forecast(forecast['minutely']['data'].map{|i| i['precipProbability']})
response.reply location.location_name + ' ' + ansi_rain_intensity_forecast(forecast['minutely']['data'].map{|i| i['precipIntensity']})
response.reply location.location_name + ' ' + ansi_temp_forecast(forecast['hourly']['data'].map {|i| i['temperature']})
response.reply location.location_name + ' ' + ansi_wind_direction_forecast(forecast)
response.reply location.location_name + ' ' + do_the_sun_thing(forecast, ansi_chars)
response.reply location.location_name + ' ' + do_the_cloud_thing(forecast, ansi_chars)
Expand All @@ -80,63 +79,63 @@ def handle_irc_all_the_things(response)

def handle_irc_ansirain_intensity(response)
location = geo_lookup(response.user.name, response.match_data[1])
uri = config.lambda_uri + "/minutely/intensity?loc=#{location.latitude},#{location.longitude}"
Lita.logger.debug(uri)
forecast = JSON.parse(RestClient.get(uri))
response.reply location.location_name + ' ' + ansi_rain_intensity_forecast(forecast)
forecast = get_forecast_io_results(response.user.name, location)

reply = ''
if location.location_name
reply = location.location_name + ' '
end

begin
rain_intensities = forecast['minutely']['data'].map{|i| i['precipIntensity']}
reply += ansi_rain_intensity_forecast(rain_intensities)
rescue NoMethodError => e
reply += 'No minute-by-minute data available.'
end

response.reply reply
end

def handle_irc_ansitemp(response)
location = geo_lookup(response.user.name, response.match_data[1])
uri = config.lambda_uri + "/hourly/temp?loc=#{location.latitude},#{location.longitude}"
Lita.logger.debug(uri)
hourly_temps = JSON.parse(RestClient.get(uri))
forecast = get_forecast_io_results(response.user.name, location)
hourly_temps = forecast['hourly']['data'].map {|i| i['temperature']}
response.reply location.location_name + ' ' + ansi_temp_forecast(hourly_temps)
end

def handle_irc_ansilampd1(response)
hourly_temps = [69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69, 69]
response.reply 'lampd1\'s house ' + ansi_temp_forecast(hourly_temps)
end

def handle_irc_ansitempapparent(response)
location = geo_lookup(response.user.name, response.match_data[1])
uri = config.lambda_uri + "/hourly/temp-apparent?loc=#{location.latitude},#{location.longitude}"
Lita.logger.debug(uri)
hourly_temps = JSON.parse(RestClient.get(uri))
forecast = get_forecast_io_results(response.user.name, location)
hourly_temps = forecast['hourly']['data'].map {|i| i['apparentTemperature']}
response.reply location.location_name + ' ' + ansi_temp_forecast(hourly_temps) + ' (apparent temp)'
end

def handle_irc_ieeetemp(response)
@scale = 'k'
location = geo_lookup(response.user.name, response.match_data[1])
uri = config.lambda_uri + "/hourly/temp?loc=#{location.latitude},#{location.longitude}"
Lita.logger.debug(uri)
hourly_temps = JSON.parse(RestClient.get(uri))
forecast = get_forecast_io_results(response.user.name, location)
hourly_temps = forecast['hourly']['data'].map {|i| i['temperature']}
response.reply location.location_name + ' ' + ansi_temp_forecast(hourly_temps)
end

def handle_irc_emojitemp(response)
location = geo_lookup(response.user.name, response.match_data[1])
uri = config.lambda_uri + "/hourly/temp?loc=#{location.latitude},#{location.longitude}"
Lita.logger.debug(uri)
hourly_temps = JSON.parse(RestClient.get(uri))
forecast = get_forecast_io_results(response.user.name, location)
hourly_temps = forecast['hourly']['data'].map {|i| i['temperature']}
response.reply location.location_name + ' ' + emoji_temp_forecast(hourly_temps)
end

def handle_irc_ascii_temp(response)
location = geo_lookup(response.user.name, response.match_data[1])
uri = config.lambda_uri + "/hourly/temp?loc=#{location.latitude},#{location.longitude}"
Lita.logger.debug(uri)
hourly_temps = JSON.parse(RestClient.get(uri))
forecast = get_forecast_io_results(response.user.name, location)
hourly_temps = forecast['hourly']['data'].map {|i| i['temperature']}
response.reply location.location_name + ' ' + ascii_temp_forecast(hourly_temps)
end

def handle_irc_daily_temp(response)
location = geo_lookup(response.user.name, response.match_data[1])
uri = config.lambda_uri + "/hourly/temp?loc=#{location.latitude},#{location.longitude}"
Lita.logger.debug(uri)
hourly_temps = JSON.parse(RestClient.get(uri))
forecast = get_forecast_io_results(response.user.name, location)
hourly_temps = forecast['hourly']['data'].map {|i| i['temperature']}
response.reply location.location_name + ' ' + ansi_temp_forecast(hourly_temps, 48)
end

Expand Down Expand Up @@ -262,6 +261,28 @@ def handle_irc_set_scale(response)
response.reply reply
end

def handle_rain_alert(response)
loc = response.match_data[1].to_s
location = geo_lookup(response.user.name, loc)
forecast = get_forecast_io_results(response.user.name, location)
response.reply "setting alert"

every(6) do |timer|
puts timer.inspect
precip_chance = forecast['minutely']['data'].map{|i| i['precipProbability']}
precip_types = forecast['minutely']['data'].map{|i| i['precipType']}

reply = ''

if location.location_name
reply = location.location_name + ' '
end

reply += ansi_rain_forecast(forecast, precip_types)
response.reply reply
end
end

def handle_irc_sunrise(response)
location = geo_lookup(response.user.name, response.match_data[1])
forecast = get_forecast_io_results(response.user.name, location)
Expand Down
4 changes: 3 additions & 1 deletion lib/lita/handlers/onewheel_forecast_io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class OnewheelForecastIo < Handler
config :colors
config :snowflake, default: '❄'
config :default_location, default: 'Portland, OR'
config :lambda_uri, required: true
config :geocoder_key

include ::ForecastIo::Constants
Expand Down Expand Up @@ -108,6 +107,9 @@ class OnewheelForecastIo < Handler
help: { '!set scale [c|f|k]' => 'Set the scale to your chosen degrees.'})
route(/^set scale$/i, :handle_irc_set_scale, command: true,
help: { '!set scale' => 'Toggle between C and F scales.'})
route(/^set rainalert$/i, :handle_rain_alert, command: true)
route(/^set rainalert\s+(.*)$/i, :handle_rain_alert, command: true,
help: { '!set rainalert [location]': 'Set a rain alert for [location]'})

# Humidity
route(/^ansihumidity\s*$/i, :handle_irc_ansi_humidity, command: true)
Expand Down
31 changes: 21 additions & 10 deletions lib/lita/handlers/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ def geo_lookup(user, query, persist = true)
geocoded = nil

if query.nil? or query.empty?
Lita.logger.debug "No query specified, pulling from redis #{REDIS_KEY}, #{user}"
Lita.logger.debug "No query specified, pulling from redis '#{REDIS_KEY}', '#{user}'"
serialized_geocoded = redis.hget(REDIS_KEY, user)
unless serialized_geocoded == 'null' or serialized_geocoded.nil?
if serialized_geocoded[/^http/]
query = serialized_geocoded
elsif
else
geocoded = JSON.parse(serialized_geocoded)
end
Lita.logger.debug "Cached location: #{geocoded.inspect}"
Expand Down Expand Up @@ -106,10 +106,16 @@ def geo_lookup(user, query, persist = true)
else

unless geocoded
uri = "https://atlas.p3k.io/api/geocode?input=#{URI.escape query}"
Lita.logger.debug "Redis hget failed, performing lookup for #{query} on #{uri}"
# geocoded = optimistic_geo_wrapper query, config.geocoder_key
geocoded = JSON.parse RestClient.get("https://atlas.p3k.io/api/geocode?input=#{URI.escape query}")
# uri = "https://atlas.p3k.io/api/geocode?input=#{URI.escape query}"
# Lita.logger.debug "Redis hget failed, performing lookup for #{query} on #{uri}"
geocoded = optimistic_geo_wrapper query, config.geocoder_key
# Catch network errors here
# begin
# geocoded = JSON.parse RestClient.get(uri)
# rescue RuntimeError => e
# puts "x"
# end

Lita.logger.debug "Geolocation found. '#{geocoded.inspect}' failed, performing lookup"
if persist
redis.hset(REDIS_KEY, user, geocoded.to_json)
Expand All @@ -129,10 +135,15 @@ def geo_lookup(user, query, persist = true)
# "seconds": -25200,
# "localtime": "2018-08-09T08:05:43-07:00"}

# loc = Location.new(
# geocoded['best_name'],
# geocoded['latitude'],
# geocoded['longitude']
# )
loc = Location.new(
geocoded['best_name'],
geocoded['latitude'],
geocoded['longitude']
geocoded['formatted_address'],
geocoded['geometry']['location']['lat'],
geocoded['geometry']['location']['lng']
)
end

Expand All @@ -150,7 +161,7 @@ def gimme_some_weather(url)
end

def set_scale(user)
key = user + '-scale'
key = user.to_s + '-scale'
if scale = redis.hget(REDIS_KEY, key)
@scale = scale
end
Expand Down
1 change: 0 additions & 1 deletion spec/fixtures/hourly_temp.json

This file was deleted.

1 change: 0 additions & 1 deletion spec/fixtures/intensity.json

This file was deleted.

1 change: 0 additions & 1 deletion spec/fixtures/no_minutely.json

This file was deleted.

0 comments on commit cbc3dbd

Please sign in to comment.