health_rack 0.0.1 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6f46a84c075053e1c303e2853ab00d5fbab262ad
4
- data.tar.gz: c81c644d0dc3eeff6debead6dc5619baeeeeaf9b
3
+ metadata.gz: 9ed561bcb7f6b18597dd1611e6d18e46a71fc5a9
4
+ data.tar.gz: 5c1965af42f24b1557c9ff3cc3ed845bc0c6bf2a
5
5
  SHA512:
6
- metadata.gz: af1d6f828eb8b4328eafd35be3fc2463d7ed10c1a3c4e61e3d36e8b687e951b37b864cc479739411e1ce297e28c566fb8d09b8fc39f1dd1c346742ed795ab680
7
- data.tar.gz: e9a5a40231bc7acfd5de4aa242159dfaa5a854b7aa1b4c95819eb31c24e96c9987010ef19d37946b5bb9d1816cb4d2d94dbc30bf1a52aa810b6afe627fea829b
6
+ metadata.gz: e294c5ffbd140c80de1518a48483553ac0027872f2c7ea218cf0a7f6db97ec67eb4d52c365d1c5bc0c79e569cc1bc8d2f7fa4c08e87492825d6355d442dca38a
7
+ data.tar.gz: 1e0fe259ed7fc9a5e790b75789070ed69bd52bd605c7de1f5ac5c30317c3faa52146463099db77589f1064d2cfa7da7be66117e00aca3685f3c54641c0daaaf6
data/README.md CHANGED
@@ -21,7 +21,7 @@ Or install it yourself as:
21
21
  ## Usage
22
22
 
23
23
  ```ruby
24
- use HealthRack, path: "/status" do
24
+ class Health < HealthRack::Base
25
25
  check "Database" do
26
26
  # Add code to check connectivity to database
27
27
  end
@@ -30,9 +30,11 @@ use HealthRack, path: "/status" do
30
30
  true
31
31
  end
32
32
  end
33
+
34
+ run Health.new
33
35
  ```
34
36
 
35
- Then simply visit the page (ie. `/status`) to view the output. By default the output will be in HTML but you can add the `.json` suffix to get a JSON output instead.
37
+ Then simply visit the page to view the output. By default the output will be in HTML but you can add the `.json` suffix to get a JSON output instead.
36
38
 
37
39
  ## Contributing
38
40
 
data/lib/health_rack.rb CHANGED
@@ -1,14 +1,12 @@
1
1
  require "health_rack/version"
2
2
 
3
3
  module HealthRack
4
- autoload :Configuration, "health_rack/configuration"
5
- autoload :DSL, "health_rack/dsl"
4
+ autoload :Base, "health_rack/base"
6
5
  autoload :Check, "health_rack/check"
7
6
  autoload :Controller, "health_rack/controller"
8
- autoload :Middleware, "health_rack/middleware"
9
7
  autoload :Renderers, "health_rack/renderers"
10
8
 
11
9
  def self.new(*args, &block)
12
- Middleware.new(*args, &block)
10
+ Base.new(*args, &block)
13
11
  end
14
12
  end
@@ -0,0 +1,39 @@
1
+ module HealthRack
2
+ class Base
3
+ class << self
4
+ def title(title = @title)
5
+ @title = title || "Health status"
6
+ end
7
+
8
+ def checks
9
+ _checks.dup
10
+ end
11
+
12
+ def check(name, &block)
13
+ _checks << Check.new(name, &block)
14
+ end
15
+
16
+ private
17
+
18
+ def _checks
19
+ @checks ||= []
20
+ end
21
+ end
22
+
23
+ def call(env)
24
+ Controller.new(env).perform(self)
25
+ end
26
+
27
+ def title
28
+ self.class.title
29
+ end
30
+
31
+ def checks
32
+ self.class.checks
33
+ end
34
+
35
+ def results
36
+ checks.each(&:perform)
37
+ end
38
+ end
39
+ end
@@ -4,15 +4,17 @@ require "rack/body_proxy"
4
4
 
5
5
  module HealthRack
6
6
  class Controller
7
- def initialize(env, config)
8
- @config = config
7
+ def initialize(env)
9
8
  @request = Rack::Request.new(env)
10
9
  @response = Rack::Response.new
11
10
  end
12
11
 
13
- def perform
14
- @response['Content-type'] = renderer.content_type
12
+ def perform(app)
13
+ renderer = renderer_class.new(app)
14
+
15
15
  renderer.render(@response)
16
+
17
+ @response['Content-type'] = renderer.content_type
16
18
  @response.finish
17
19
  end
18
20
 
@@ -25,8 +27,8 @@ module HealthRack
25
27
 
26
28
  private
27
29
 
28
- def renderer
29
- @renderer ||= Renderers.find(format).new(@config)
30
+ def renderer_class
31
+ @renderer_class ||= Renderers.find(format)
30
32
  end
31
33
  end
32
34
  end
@@ -3,8 +3,8 @@ module HealthRack
3
3
  class HTMLRenderer
4
4
  CONTENT_TYPE = "text/html; charset=UTF-8"
5
5
 
6
- def initialize(config)
7
- @config = config
6
+ def initialize(app)
7
+ @app = app
8
8
  end
9
9
 
10
10
  def content_type
@@ -17,11 +17,11 @@ module HealthRack
17
17
  <html>
18
18
  <head>
19
19
  <meta charset="utf-8">
20
- <title>#{@config.title}</title>
20
+ <title>#{@app.title}</title>
21
21
  </head>
22
22
  <body>
23
23
  <table border="1">
24
- <caption>#{@config.title}</caption>
24
+ <caption>#{@app.title}</caption>
25
25
  <thead>
26
26
  <tr>
27
27
  <th>&nbsp;</th>
@@ -32,12 +32,12 @@ module HealthRack
32
32
  <tbody>
33
33
  HTML
34
34
 
35
- @config.checks.each do |check|
35
+ results.each do |result|
36
36
  buffer.write <<-HTML
37
37
  <tr>
38
- <th>#{check.title}</th>
39
- <td>#{status(check)}</td>
40
- <td>#{check.duration.to_f * 1000}</td>
38
+ <th>#{result.title}</th>
39
+ <td>#{status(result)}</td>
40
+ <td>#{result.duration.to_f * 1000}</td>
41
41
  </tr>
42
42
  HTML
43
43
  end
@@ -59,6 +59,10 @@ module HealthRack
59
59
  else 'FAIL'
60
60
  end
61
61
  end
62
+
63
+ def results
64
+ @results ||= @app.results
65
+ end
62
66
  end
63
67
  end
64
68
  end
@@ -5,8 +5,8 @@ module HealthRack
5
5
  class JSONRenderer
6
6
  CONTENT_TYPE = "application/json"
7
7
 
8
- def initialize(config)
9
- @config = config
8
+ def initialize(app)
9
+ @app = app
10
10
  end
11
11
 
12
12
  def content_type
@@ -25,21 +25,25 @@ module HealthRack
25
25
 
26
26
  def data
27
27
  {
28
- title: @config.title,
28
+ title: @app.title,
29
29
  status: overall_status,
30
30
  summary: summary
31
31
  }
32
32
  end
33
33
 
34
34
  def overall_status
35
- @config.checks.all?(&:status)
35
+ results.all?(&:status)
36
36
  end
37
37
 
38
38
  def summary
39
- @config.checks.map do |check|
40
- {title: check.title, status: check.status, duration: check.duration}
39
+ results.map do |result|
40
+ {title: result.title, status: result.status, duration: result.duration}
41
41
  end
42
42
  end
43
+
44
+ def results
45
+ @results ||= @app.results
46
+ end
43
47
  end
44
48
  end
45
49
  end
@@ -1,3 +1,3 @@
1
1
  module HealthRack
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -0,0 +1,64 @@
1
+ require "spec_helper"
2
+
3
+ describe HealthRack::Base do
4
+ describe "default" do
5
+ let(:base){ HealthRack::Base }
6
+
7
+ it "should have 'Health status' as the title" do
8
+ expect(base.title).to eq("Health status")
9
+ end
10
+
11
+ it "should have checks be an empty array" do
12
+ expect(base.checks).to eq([])
13
+ end
14
+ end
15
+
16
+ describe "configuration" do
17
+ let(:base) do
18
+ Class.new(HealthRack::Base) do
19
+ title "Test"
20
+
21
+ check "Foobar" do
22
+ true
23
+ end
24
+ end
25
+ end
26
+
27
+ it "should set the title" do
28
+ expect(base.title).to eq("Test")
29
+ end
30
+
31
+ it "should add a check" do
32
+ expect(base.checks).not_to be_empty
33
+ end
34
+ end
35
+
36
+ describe "calling" do
37
+ let(:app){ HealthRack::Base.new }
38
+ let(:result){ app.call(Hash.new) }
39
+
40
+ it "should include a status code" do
41
+ expect(result[0]).to be_kind_of(Fixnum)
42
+ end
43
+
44
+ it "should include headers" do
45
+ expect(result[1]).to be_kind_of(Hash)
46
+ end
47
+
48
+ it "should return an object that responds to each" do
49
+ expect(result[2]).to respond_to(:each)
50
+ end
51
+ end
52
+
53
+ describe "accessors" do
54
+ let(:app){ HealthRack::Base.new }
55
+
56
+ it "should return the title set on the class" do
57
+ expect(app.title).to eq(HealthRack::Base.title)
58
+ end
59
+
60
+ it "should return the checks set on the class" do
61
+ expect(app.checks).to eq(HealthRack::Base.checks)
62
+ end
63
+ end
64
+ end
@@ -1,13 +1,12 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe HealthRack::Controller do
4
- let(:config){ HealthRack::Configuration.new }
4
+ let(:env){ Hash.new }
5
+ let(:controller){ HealthRack::Controller.new(env) }
5
6
 
6
7
  describe "responding" do
7
- let(:controller){ HealthRack::Controller.new({}, config) }
8
- let(:response){ controller.perform }
9
- let(:headers){ response[1] }
10
- let(:body){ response[2].body.join }
8
+ let(:app){ HealthRack::Base.new }
9
+ let(:response){ controller.perform(app) }
11
10
 
12
11
  it "should return a rack-compatible response" do
13
12
  expect(response[0]).to be_kind_of(Fixnum)
@@ -16,18 +15,15 @@ describe HealthRack::Controller do
16
15
  end
17
16
 
18
17
  it "should set the content header" do
19
- expect(headers).to include("Content-type" => "text/html; charset=UTF-8")
18
+ expect(response[1]).to include("Content-type" => "text/html; charset=UTF-8")
20
19
  end
21
20
 
22
21
  it "should render to the body" do
23
- expect(body).not_to be_empty
22
+ expect(response[2].body.join).not_to be_empty
24
23
  end
25
24
  end
26
25
 
27
26
  describe "the request's format when has an extension" do
28
- let(:env){ Hash.new }
29
- let(:controller){ HealthRack::Controller.new(env, config) }
30
-
31
27
  it "should return the extension" do
32
28
  env.update "PATH_INFO" => "/test.json"
33
29
  expect(controller.format).to eq('json')
@@ -1,22 +1,24 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe HealthRack::Renderers::HTMLRenderer do
4
- class Buffer < Array
5
- alias_method :write, :push
6
-
7
- def to_s
8
- join(" ")
9
- end
4
+ let(:app) do
5
+ Class.new(HealthRack::Base) do
6
+ title "Test"
7
+ check("Foobar"){ true }
8
+ end.new
10
9
  end
10
+ let(:buffer) do
11
+ Class.new(Array) do
12
+ alias_method :write, :push
11
13
 
12
- let(:check){ HealthRack::Check.new("Foobar"){ true } }
13
- let(:config){ HealthRack::Configuration.new(title: "Test") }
14
- let(:buffer){ Buffer.new }
14
+ def to_s
15
+ join(" ")
16
+ end
17
+ end.new
18
+ end
15
19
 
16
20
  before(:each) do
17
- check.perform
18
- config.checks << check
19
- HealthRack::Renderers::HTMLRenderer.new(config).render(buffer)
21
+ HealthRack::Renderers::HTMLRenderer.new(app).render(buffer)
20
22
  end
21
23
 
22
24
  describe "rendering" do
@@ -1,22 +1,24 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe HealthRack::Renderers::JSONRenderer do
4
- class Buffer < Array
5
- alias_method :write, :push
6
-
7
- def to_s
8
- join(" ")
9
- end
4
+ let(:app) do
5
+ Class.new(HealthRack::Base) do
6
+ title "Test"
7
+ check("Foobar"){ true }
8
+ end.new
9
+ end
10
+ let(:buffer) do
11
+ Class.new(Array) do
12
+ alias_method :write, :push
13
+
14
+ def to_s
15
+ join(" ")
16
+ end
17
+ end.new
10
18
  end
11
-
12
- let(:check){ HealthRack::Check.new("Foobar"){ true } }
13
- let(:config){ HealthRack::Configuration.new(title: "Test") }
14
- let(:buffer){ Buffer.new }
15
19
 
16
20
  before(:each) do
17
- check.perform
18
- config.checks << check
19
- HealthRack::Renderers::JSONRenderer.new(config).render(buffer)
21
+ HealthRack::Renderers::JSONRenderer.new(app).render(buffer)
20
22
  end
21
23
 
22
24
  describe "rendering" do
@@ -2,11 +2,10 @@ require "spec_helper"
2
2
 
3
3
  describe HealthRack do
4
4
  describe "initializing" do
5
- let(:app){ lambda{} }
6
- let(:health_rack){ HealthRack.new(app) }
5
+ let(:health_rack){ HealthRack.new }
7
6
 
8
7
  it "should return a new instance of the middleware" do
9
- expect(health_rack).to be_kind_of(HealthRack::Middleware)
8
+ expect(health_rack).to be_kind_of(HealthRack::Base)
10
9
  end
11
10
  end
12
11
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: health_rack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marty Zalega
@@ -67,20 +67,16 @@ files:
67
67
  - Rakefile
68
68
  - health_rack.gemspec
69
69
  - lib/health_rack.rb
70
+ - lib/health_rack/base.rb
70
71
  - lib/health_rack/check.rb
71
- - lib/health_rack/configuration.rb
72
72
  - lib/health_rack/controller.rb
73
- - lib/health_rack/dsl.rb
74
- - lib/health_rack/middleware.rb
75
73
  - lib/health_rack/renderers.rb
76
74
  - lib/health_rack/renderers/html_renderer.rb
77
75
  - lib/health_rack/renderers/json_renderer.rb
78
76
  - lib/health_rack/version.rb
77
+ - spec/lib/health_rack/base_spec.rb
79
78
  - spec/lib/health_rack/check_spec.rb
80
- - spec/lib/health_rack/configuration_spec.rb
81
79
  - spec/lib/health_rack/controller_spec.rb
82
- - spec/lib/health_rack/dsl_spec.rb
83
- - spec/lib/health_rack/middleware_spec.rb
84
80
  - spec/lib/health_rack/renderers/html_renderer_spec.rb
85
81
  - spec/lib/health_rack/renderers/json_renderer_spec.rb
86
82
  - spec/lib/health_rack_spec.rb
@@ -110,11 +106,9 @@ signing_key:
110
106
  specification_version: 4
111
107
  summary: A rack middleware for an easy health check endpoint
112
108
  test_files:
109
+ - spec/lib/health_rack/base_spec.rb
113
110
  - spec/lib/health_rack/check_spec.rb
114
- - spec/lib/health_rack/configuration_spec.rb
115
111
  - spec/lib/health_rack/controller_spec.rb
116
- - spec/lib/health_rack/dsl_spec.rb
117
- - spec/lib/health_rack/middleware_spec.rb
118
112
  - spec/lib/health_rack/renderers/html_renderer_spec.rb
119
113
  - spec/lib/health_rack/renderers/json_renderer_spec.rb
120
114
  - spec/lib/health_rack_spec.rb
@@ -1,16 +0,0 @@
1
- module HealthRack
2
- class Configuration
3
- attr_accessor :title, :path
4
- attr_reader :checks
5
-
6
- def initialize(options = {})
7
- @checks = []
8
-
9
- options.each do |name, value|
10
- method_name = "#{name}="
11
- public_send(method_name, value) if respond_to?(method_name, false)
12
- end
13
- end
14
- end
15
- end
16
-
@@ -1,24 +0,0 @@
1
- module HealthRack
2
- class DSL
3
- def initialize(config, &block)
4
- @config = config
5
-
6
- if block_given?
7
- case block.arity
8
- when 0 then instance_eval(&block)
9
- when 1, -1 then yield(self)
10
- else raise(ArgumentError, "wrong number of arguments 0..1")
11
- end
12
- end
13
- end
14
-
15
- def title(title)
16
- @config.title = title
17
- end
18
-
19
- def check(*args, &block)
20
- @config.checks << Check.new(*args, &block)
21
- nil
22
- end
23
- end
24
- end
@@ -1,23 +0,0 @@
1
- module HealthRack
2
- class Middleware
3
- DEFAULT_OPTIONS = {
4
- title: "Health check",
5
- path: "/health",
6
- }.freeze
7
-
8
- def initialize(app, options = {}, &block)
9
- @app = app
10
- @config = Configuration.new(DEFAULT_OPTIONS.merge(options))
11
-
12
- DSL.new(@config, &block) if block_given?
13
- end
14
-
15
- def call(env)
16
- if @config.path.start_with?(env['PATH_INFO'])
17
- Controller.new(env, @config).perform
18
- else
19
- @app.call(env)
20
- end
21
- end
22
- end
23
- end
@@ -1,36 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe HealthRack::Configuration do
4
- describe "initializing with no arguments" do
5
- let(:config){ HealthRack::Configuration.new }
6
-
7
- it "should default title to nil" do
8
- expect(config.title).to be_nil
9
- end
10
-
11
- it "should default path to nil" do
12
- expect(config.path).to be_nil
13
- end
14
-
15
- it "should default checks to an empty array" do
16
- expect(config.checks).to eq([])
17
- end
18
- end
19
-
20
- describe "initializing with options" do
21
- let(:options){ {title: "Test", path: "/test", checks: [:foo]} }
22
- let(:config){ HealthRack::Configuration.new(options) }
23
-
24
- it "should set title" do
25
- expect(config.title).to eq("Test")
26
- end
27
-
28
- it "should set path" do
29
- expect(config.path).to eq("/test")
30
- end
31
-
32
- it "shouldn't set checks" do
33
- expect(config.checks).to eq([])
34
- end
35
- end
36
- end
@@ -1,45 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe HealthRack::DSL do
4
- let(:config){ HealthRack::Configuration.new }
5
-
6
- describe "initializing without a block" do
7
- it "should not raise argument error" do
8
- expect{ HealthRack::DSL.new(config) }.not_to raise_error
9
- end
10
- end
11
-
12
- describe "initialize with a block that has one argument" do
13
- it "should call block passing itself as an argument" do
14
- expect{|b| HealthRack::DSL.new(config, &b) }.to yield_with_args(HealthRack::DSL)
15
- end
16
- end
17
-
18
- describe "initialize with a block that has no arguments" do
19
- it "should call the block in the context of itself"
20
- end
21
-
22
- describe "initialize with a block that has more than one argument" do
23
- it "should raise an error" do
24
- expect{ HealthRack::DSL.new(config){|a, b|} }.to raise_error(ArgumentError)
25
- end
26
- end
27
-
28
- describe "setting the title" do
29
- let(:dsl){ HealthRack::DSL.new(config) }
30
-
31
- it "should set the title on the config" do
32
- dsl.title "example"
33
- expect(config.title).to eq("example")
34
- end
35
- end
36
-
37
- describe "adding a check" do
38
- let(:dsl){ HealthRack::DSL.new(config) }
39
-
40
- it "should add a new check to the config" do
41
- dsl.check("Test"){ true }
42
- expect(config.checks.first).to be_kind_of(HealthRack::Check)
43
- end
44
- end
45
- end
@@ -1,33 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe HealthRack::Middleware do
4
- describe "initialization" do
5
- let(:app){ lambda{|env|} }
6
-
7
- it "should call block when block is given" do
8
- expect{|b| HealthRack::Middleware.new(app, {}, &b) }.to yield_control
9
- end
10
- end
11
-
12
- describe "calling the middleware" do
13
- let(:env){ {"PATH_INFO" => "/test"} }
14
-
15
- it "should respond when path info matches the path" do
16
- expect do |block|
17
- HealthRack::Middleware.new(Proc.new(&block), {path: "/test"}).call(env)
18
- end.not_to yield_with_args(env)
19
- end
20
-
21
- it "should respond when path info matches the path with the format" do
22
- expect do |block|
23
- HealthRack::Middleware.new(Proc.new(&block), {path: "/test.html"}).call(env)
24
- end.not_to yield_with_args(env)
25
- end
26
-
27
- it "should call the app when path info doesn't match the path" do
28
- expect do |block|
29
- HealthRack::Middleware.new(Proc.new(&block), {}).call(env)
30
- end.to yield_with_args(env)
31
- end
32
- end
33
- end