module Sinatra::StoplightAdmin::Helpers

Constants

COLORS

Public Instance Methods

data_store() click to toggle source
# File lib/sinatra/stoplight_admin.rb, line 17
def data_store
  return @data_store if defined?(@data_store)
  @data_store = Stoplight::Light.default_data_store = settings.data_store
end
green(light) click to toggle source
# File lib/sinatra/stoplight_admin.rb, line 89
def green(light)
  l = Stoplight::Light.new(light) {}
  if data_store.get_state(l) == Stoplight::State::LOCKED_RED
    new_state = Stoplight::State::LOCKED_GREEN
    data_store.set_state(l, new_state)
  end

  data_store.clear_failures(l)
end
light_info(light) click to toggle source
# File lib/sinatra/stoplight_admin.rb, line 29
def light_info(light)
  l = Stoplight::Light.new(light) {}
  color = l.color
  failures, state = l.data_store.get_all(l)

  {
    name: light,
    color: color,
    failures: failures,
    locked: locked?(state)
  }
end
light_sort_key(light) click to toggle source
# File lib/sinatra/stoplight_admin.rb, line 42
def light_sort_key(light)
  [-COLORS.index(light[:color]),
   light[:name]]
end
lights() click to toggle source
# File lib/sinatra/stoplight_admin.rb, line 22
def lights
  data_store
    .names
    .map { |name| light_info(name) }
    .sort_by { |light| light_sort_key(light) }
end
lock(light) click to toggle source
# File lib/sinatra/stoplight_admin.rb, line 71
def lock(light)
  l = Stoplight::Light.new(light) {}
  new_state =
    case l.color
    when Stoplight::Color::GREEN
      Stoplight::State::LOCKED_GREEN
    else
      Stoplight::State::LOCKED_RED
    end

  data_store.set_state(l, new_state)
end
locked?(state) click to toggle source
# File lib/sinatra/stoplight_admin.rb, line 47
def locked?(state)
  [Stoplight::State::LOCKED_GREEN,
   Stoplight::State::LOCKED_RED]
    .include?(state)
end
red(light) click to toggle source
# File lib/sinatra/stoplight_admin.rb, line 99
def red(light)
  l = Stoplight::Light.new(light) {}
  data_store.set_state(l, Stoplight::State::LOCKED_RED)
end
stat_params(ls) click to toggle source
# File lib/sinatra/stoplight_admin.rb, line 53
def stat_params(ls)
  h = {
    count_red: 0, count_yellow: 0, count_green: 0,
    percent_red: 0, percent_yellow: 0, percent_green: 0
  }
  return h if (size = ls.size).zero?

  h[:count_red] = ls.count { |l| l[:color] == RED }
  h[:count_yellow] = ls.count { |l| l[:color] == YELLOW }
  h[:count_green] = size - h[:count_red] - h[:count_yellow]

  h[:percent_red] = (100.0 * h[:count_red] / size).ceil
  h[:percent_yellow] = (100.0 * h[:count_yellow] / size).ceil
  h[:percent_green] = 100.0 - h[:percent_red] - h[:percent_yellow]

  h
end
unlock(light) click to toggle source
# File lib/sinatra/stoplight_admin.rb, line 84
def unlock(light)
  l = Stoplight::Light.new(light) {}
  data_store.set_state(l, Stoplight::State::UNLOCKED)
end
with_lights() { |l| ... } click to toggle source
# File lib/sinatra/stoplight_admin.rb, line 104
def with_lights
  [*params[:names]]
    .map  { |l| URI.unescape(l) }
    .each { |l| yield(l) }
end