Explorar el Código

Status helper

new-scoring
aeris hace 2 años
padre
commit
d0160f1067
Se han modificado 3 ficheros con 150 adiciones y 0 borrados
  1. 1
    0
      lib/cryptcheck.rb
  2. 28
    0
      lib/cryptcheck/status.rb
  3. 121
    0
      spec/cryptcheck/status_spec.rb

+ 1
- 0
lib/cryptcheck.rb Ver fichero

@@ -31,6 +31,7 @@ module CryptCheck
end
end

autoload :Status, 'cryptcheck/status'
autoload :Logger, 'cryptcheck/logger'
autoload :Tls, 'cryptcheck/tls'
module Tls

+ 28
- 0
lib/cryptcheck/status.rb Ver fichero

@@ -0,0 +1,28 @@
module CryptCheck
module Status
LEVELS = %i(critical error warning good perfect best).freeze
PROBLEMS = %i(critical error warning).freeze

def self.status(statuses)
statuses = self.collect statuses
self.select LEVELS, statuses
end

def self.problem(statuses)
statuses = self.collect statuses
self.select PROBLEMS, statuses
end

private
def self.collect(statuses)
first = statuses.first
statuses = statuses.collect &:status if first.respond_to? :status
statuses
end

def self.select(levels, statuses)
return nil if statuses.empty?
(levels & statuses).first
end
end
end

+ 121
- 0
spec/cryptcheck/status_spec.rb Ver fichero

@@ -0,0 +1,121 @@
require 'ostruct'

describe CryptCheck::Status do
describe '::status' do
it 'must handle empty list' do
expect(CryptCheck::Status.status []).to be_nil
end

it 'must answer correctly' do
{
[:critical, :critical] => :critical,
[:critical, :error] => :critical,
[:critical, :warning] => :critical,
[:critical, :good] => :critical,
[:critical, :perfect] => :critical,
[:critical, :best] => :critical,

[:error, :critical] => :critical,
[:error, :error] => :error,
[:error, :warning] => :error,
[:error, :good] => :error,
[:error, :perfect] => :error,
[:error, :best] => :error,

[:warning, :critical] => :critical,
[:warning, :error] => :error,
[:warning, :warning] => :warning,
[:warning, :good] => :warning,
[:warning, :perfect] => :warning,
[:warning, :best] => :warning,

[:good, :critical] => :critical,
[:good, :error] => :error,
[:good, :warning] => :warning,
[:good, :good] => :good,
[:good, :perfect] => :good,
[:good, :best] => :good,

[:perfect, :critical] => :critical,
[:perfect, :error] => :error,
[:perfect, :warning] => :warning,
[:perfect, :good] => :good,
[:perfect, :perfect] => :perfect,
[:perfect, :best] => :perfect,

[:best, :critical] => :critical,
[:best, :error] => :error,
[:best, :warning] => :warning,
[:best, :good] => :good,
[:best, :perfect] => :perfect,
[:best, :best] => :best
}.each do |levels, result|
got = CryptCheck::Status.status levels
expect(got).to be(result), "#{levels} : expected #{result.inspect}, got #{got.inspect}"
end
end

it 'must handle object list' do
critical = OpenStruct.new status: :critical
warning = OpenStruct.new status: :warning
expect(CryptCheck::Status.status [critical, warning]).to be :critical
end
end

describe '::problem' do
it 'must answer correctly' do
{
[:critical, :critical] => :critical,
[:critical, :error] => :critical,
[:critical, :warning] => :critical,
[:critical, :good] => :critical,
[:critical, :perfect] => :critical,
[:critical, :best] => :critical,

[:error, :critical] => :critical,
[:error, :error] => :error,
[:error, :warning] => :error,
[:error, :good] => :error,
[:error, :perfect] => :error,
[:error, :best] => :error,

[:warning, :critical] => :critical,
[:warning, :error] => :error,
[:warning, :warning] => :warning,
[:warning, :good] => :warning,
[:warning, :perfect] => :warning,
[:warning, :best] => :warning,

[:good, :critical] => :critical,
[:good, :error] => :error,
[:good, :warning] => :warning,
[:good, :good] => nil,
[:good, :perfect] => nil,
[:good, :best] => nil,

[:perfect, :critical] => :critical,
[:perfect, :error] => :error,
[:perfect, :warning] => :warning,
[:perfect, :good] => nil,
[:perfect, :perfect] => nil,
[:perfect, :best] => nil,

[:best, :critical] => :critical,
[:best, :error] => :error,
[:best, :warning] => :warning,
[:best, :good] => nil,
[:best, :perfect] => nil,
[:best, :best] => nil
}.each do |levels, result|
got = CryptCheck::Status.problem levels
expect(got).to be(result), "#{levels} : expected #{result.inspect}, got #{got.inspect}"
end
end

it 'must handle object list' do
critical = OpenStruct.new status: :critical
warning = OpenStruct.new status: :warning
expect(CryptCheck::Status.problem [critical, warning]).to be :critical
end
end
end

Cargando…
Cancelar
Guardar