Skip to content

Commit

Permalink
Merge pull request #14 from reidblomquist/color-rain-conditions
Browse files Browse the repository at this point in the history
colorized conditions percip output
  • Loading branch information
onewheelskyward committed Oct 28, 2015
2 parents c515c9c + 10d94b0 commit 5b56fff
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
12 changes: 7 additions & 5 deletions lib/lita/handlers/forecasts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def get_max_rain_chance(forecast, key = 'minutely')
end
end

def do_the_rain_chance_thing(forecast, chars, key, use_color = config.colors)
def do_the_rain_chance_thing(forecast, chars, key, use_color = config.colors, minute_limit = nil)
if forecast['minutely'].nil?
return 'No minute-by-minute data available.'
end
Expand All @@ -46,6 +46,10 @@ def do_the_rain_chance_thing(forecast, chars, key, use_color = config.colors)
end
end

if minute_limit
data = condense_data(data, minute_limit)
end

str = get_dot_str(chars, data, 0, 1, key)

if i_can_has_snow
Expand Down Expand Up @@ -205,14 +209,12 @@ def do_the_sunset_thing(forecast)
def conditions(forecast)
temp_str, temps = do_the_temp_thing(forecast, ansi_chars, 8)
wind_str, winds = do_the_wind_direction_thing(forecast, ansi_wind_arrows, 8)
rain_str, rains = do_the_rain_chance_thing(forecast, ansi_chars, 'precipProbability', false)

rs = compress_string(rain_str, 4)
rain_str, rains = do_the_rain_chance_thing(forecast, ansi_chars, 'precipProbability', config.colors, 15)

sun_chance = ((1 - forecast['daily']['data'][0]['cloudCover']) * 100).round
"#{get_temperature temps.first.round(2)} |#{temp_str}| #{get_temperature temps.last.round(2)} "\
"/ #{get_speed(winds.first)} |#{wind_str}| #{get_speed(winds.last)} "\
"/ #{sun_chance}% chance of sun / 60m precip |#{rs}|"
"/ #{sun_chance}% chance of sun / 60m precip |#{rain_str}|"
end

def do_the_seven_day_thing(forecast)
Expand Down
33 changes: 25 additions & 8 deletions lib/lita/handlers/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,33 @@ def get_colored_string(data_limited, key, dot_str, range_hash)
colored_str += "\x03" + colors[color] + collect_str + "\x03"
end

def compress_string(str, compression_factor)
i = 0
rs = ''
str.to_s.each_char do |char|
if i % compression_factor == 0
rs += char
# this method lets us condense rain forcasts into smaller sets
# it averages the values contained in a chunk of data perportionate the the limit set
# then returns a new array of hashes containing those averaged values
def condense_data(data, limit)
return if limit >= data.length
chunk_length = (data.length / limit.to_f).round
results = []
data.each_slice(chunk_length) do |chunk|
chunk_results = {}
condensed_chunk = collect_values(chunk)
condensed_chunk.each do |k, v|
if v[0].class == Fixnum || v[0].class == Float
new_val = v.inject{ |sum,val| sum + val} / v.size
elsif v[0].class == String
new_val = v[0]
end
chunk_results[k] = new_val
end
i += 1
results << chunk_results
end
rs
results
end

# this method is simply to transform an array of hashes into a hash of arrays
# kudos to Phrogz for the info here: http://stackoverflow.com/questions/5490952/merge-array-of-hashes-to-get-hash-of-arrays-of-values
def collect_values(hashes)
{}.tap{ |r| hashes.each{ |h| h.each{ |k,v| (r[k]||=[]) << v } } }
end

def get_dot_str(chars, data, min, differential, key)
Expand Down
2 changes: 1 addition & 1 deletion spec/lita/handlers/forecast_io_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@

it '!conditions' do
send_message 'conditions'
expect(replies.last).to eq("Portland, OR 28.3°F |\u000306_▁▃\u000310▅▇█\u000303█\u0003| 38.72°F / 4.3 mph |\u000306↓\u000310↙←\u000311↖↑↗\u000308→\u0003| 12.71 mph / 98% chance of sun / 60m precip |_▅█_____________|")
expect(replies.last).to eq("Portland, OR 28.3°F |\u000306_▁▃\u000310▅▇█\u000303█\u0003| 38.72°F / 4.3 mph |\u000306↓\u000310↙←\u000311↖↑↗\u000308→\u0003| 12.71 mph / 98% chance of sun / 60m precip |\u000306☃\u000311▇\u000308▇\u000302_____________\u0003|")
end

it '!alerts' do
Expand Down

0 comments on commit 5b56fff

Please sign in to comment.