Module: HealthBit

Extended by:
HealthBit
Included in:
HealthBit
Defined in:
lib/health_bit.rb,
lib/health_bit/check.rb,
lib/health_bit/version.rb,
lib/health_bit/formatter.rb,
lib/health_bit/check_error.rb

Defined Under Namespace

Classes: Check, CheckError, Formatter

Constant Summary collapse

DEFAULT_SUCCESS_TEXT =
'%<count>d checks passed 🎉'
DEFAULT_HEADERS =
{
  'Content-Type' => 'text/plain;charset=utf-8',
  'Cache-Control' => 'private,max-age=0,must-revalidate,no-store'
}.freeze
DEFAULT_SUCCESS_CODE =
200
DEFAULT_FAIL_CODE =
500
DEFAULT_FORMATTER =
Formatter.new
VERSION =
'0.2.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fail_codeObject



33
34
35
# File 'lib/health_bit.rb', line 33

def fail_code
  @fail_code || DEFAULT_FAIL_CODE
end

#formatterFormatter

Returns:



42
43
44
# File 'lib/health_bit.rb', line 42

def formatter
  @formatter || DEFAULT_FORMATTER
end

#headersObject



37
38
39
# File 'lib/health_bit.rb', line 37

def headers
  (@headers || DEFAULT_HEADERS).dup
end

#show_backtraceObject

Returns the value of attribute show_backtrace.



23
24
25
# File 'lib/health_bit.rb', line 23

def show_backtrace
  @show_backtrace
end

#success_codeObject



29
30
31
# File 'lib/health_bit.rb', line 29

def success_code
  @success_code || DEFAULT_SUCCESS_CODE
end

#success_textObject



25
26
27
# File 'lib/health_bit.rb', line 25

def success_text
  format(@success_text || DEFAULT_SUCCESS_TEXT, count: checks.length)
end

Instance Method Details

#add(name, handler = nil, &block) ⇒ self

Returns:

  • (self)

Raises:

  • (ArgumentError)


55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/health_bit.rb', line 55

def add(name, handler = nil, &block)
  raise ArgumentError, <<~MSG if handler && block
    Both <handler> and <block> were passed to the <#{name}> check
  MSG

  raise ArgumentError, <<~MSG unless handler || block
    Nor <handler> or <block> were passed to the <#{name}> check
  MSG

  checks.push(Check.new(name, handler || block))

  self
end

#check(env) ⇒ nil, CheckError

Returns:



70
71
72
73
74
75
76
# File 'lib/health_bit.rb', line 70

def check(env)
  checks.each do |check|
    (exception = check.call(env)).nil? ? next : (return exception)
  end

  nil
end

#checksObject



46
47
48
# File 'lib/health_bit.rb', line 46

def checks
  @checks ||= []
end

#cloneObject



98
99
100
101
102
# File 'lib/health_bit.rb', line 98

def clone
  Module.new.tap do |dolly|
    dolly.singleton_class.include(HealthBit)
  end
end

#configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (HealthBit)

    the object that the method was called on



50
51
52
# File 'lib/health_bit.rb', line 50

def configure
  yield(self)
end

#rack(this = self) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/health_bit.rb', line 78

def rack(this = self)
  @rack ||= Rack::Builder.new do
    run ->(env) do
      if (error = this.check(env))
        [
          this.formatter.code_failure(error, env, this),
          this.formatter.headers_failure(error, env, this),
          [this.formatter.format_failure(error, env, this)]
        ]
      else
        [
          this.formatter.code_success(env, this),
          this.formatter.headers_success(env, this),
          [this.formatter.format_success(error, env, this)]
        ]
      end
    end
  end
end