class RSpec::HueFormatter

Constants

GREEN
IMMEDIATE_TRANSITION
LIGHT_SETTERS_BY_READER
RED
SLOW_TRANSITION
YELLOW

Attributes

example_index[R]

Public Class Methods

new(_output) click to toggle source
# File lib/rspec/hue_formatter.rb, line 28
def initialize(_output)
  @example_index = -1
end

Public Instance Methods

close(_notification) click to toggle source
# File lib/rspec/hue_formatter.rb, line 68
def close(_notification)
  restore_light_states
end
dump_summary(notification) click to toggle source
# File lib/rspec/hue_formatter.rb, line 54
def dump_summary(notification)
  turn_off_lights(IMMEDIATE_TRANSITION)

  sleep 2

  hue = case
        when notification.failure_count.nonzero? then RED
        when notification.pending_count.nonzero? then YELLOW
        else GREEN
        end

  glow_all_lights(hue, 2)
end
example_failed(_notification) click to toggle source
# File lib/rspec/hue_formatter.rb, line 50
def example_failed(_notification)
  flash_current_light(RED)
end
example_passed(_notification) click to toggle source
# File lib/rspec/hue_formatter.rb, line 42
def example_passed(_notification)
  flash_current_light(GREEN)
end
example_pending(_notification) click to toggle source
# File lib/rspec/hue_formatter.rb, line 46
def example_pending(_notification)
  flash_current_light(YELLOW)
end
example_started(_notification) click to toggle source
# File lib/rspec/hue_formatter.rb, line 38
def example_started(_notification)
  @example_index += 1
end
start(_notification) click to toggle source
# File lib/rspec/hue_formatter.rb, line 32
def start(_notification)
  save_light_states
  turn_off_lights
  sleep 2
end

Private Instance Methods

bright_state(hue) click to toggle source
# File lib/rspec/hue_formatter.rb, line 112
def bright_state(hue)
  {
            on: true,
           hue: hue,
    saturation: Hue::Light::SATURATION_RANGE.end,
    brightness: Hue::Light::BRIGHTNESS_RANGE.end
  }
end
current_light() click to toggle source
# File lib/rspec/hue_formatter.rb, line 121
def current_light
  light_for_index(example_index)
end
flash_current_light(hue) click to toggle source
# File lib/rspec/hue_formatter.rb, line 107
def flash_current_light(hue)
  previous_light.set_state({ on: false }, IMMEDIATE_TRANSITION)
  current_light.set_state(bright_state(hue), IMMEDIATE_TRANSITION)
end
glow_all_lights(hue, number_of_times) click to toggle source
# File lib/rspec/hue_formatter.rb, line 92
def glow_all_lights(hue, number_of_times)
  number_of_times.times do
    set_all_light_states(bright_state(hue), SLOW_TRANSITION)
    sleep 1
    set_all_light_states({ brightness: Hue::Light::BRIGHTNESS_RANGE.begin }, SLOW_TRANSITION)
    sleep 1
  end
end
light_for_index(index) click to toggle source
# File lib/rspec/hue_formatter.rb, line 129
def light_for_index(index)
  light_index = index % lights.count
  lights[light_index]
end
lights() click to toggle source
# File lib/rspec/hue_formatter.rb, line 134
def lights
  @lights ||= begin
    hue = Hue::Client.new
    hue.lights.sort_by(&:name)
  end
end
previous_light() click to toggle source
# File lib/rspec/hue_formatter.rb, line 125
def previous_light
  light_for_index(example_index - 1)
end
restore_light_states() click to toggle source
# File lib/rspec/hue_formatter.rb, line 82
def restore_light_states
  lights.zip(@states).each do |light, state|
    light.set_state(state, SLOW_TRANSITION)
  end
end
save_light_states() click to toggle source
# File lib/rspec/hue_formatter.rb, line 74
def save_light_states
  @states = lights.map do |light|
    LIGHT_SETTERS_BY_READER.each_with_object({}) do |(attr, key), state|
      state[key] = light.send(attr)
    end
  end
end
set_all_light_states(state, transition_time) click to toggle source
# File lib/rspec/hue_formatter.rb, line 101
def set_all_light_states(state, transition_time)
  lights.each do |light|
    light.set_state(state, transition_time)
  end
end
turn_off_lights(transition_time = SLOW_TRANSITION) click to toggle source
# File lib/rspec/hue_formatter.rb, line 88
def turn_off_lights(transition_time = SLOW_TRANSITION)
  set_all_light_states({ on: false }, transition_time)
end