stethoscope 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc ADDED
@@ -0,0 +1,7 @@
1
+
2
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
3
+ && -s "${rvm_path:-$HOME/.rvm}/environments/ruby-1.8.7-p302@stethoscope" ]] ; then
4
+ \. "${rvm_path:-$HOME/.rvm}/environments/ruby-1.8.7-p302@stethoscope"
5
+ else
6
+ rvm --create use "ruby-1.8.7-p302@stethoscope"
7
+ fi
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- stethoscope (0.0.1)
4
+ stethoscope (0.2.0)
5
5
  dictionary (>= 1.0)
6
6
  rack (> 1.0)
7
7
  tilt (>= 1.0)
@@ -10,6 +10,7 @@ GEM
10
10
  remote: http://rubygems.org/
11
11
  specs:
12
12
  dictionary (1.0.0)
13
+ json_pure (1.5.3)
13
14
  nanotest (0.9.4.1)
14
15
  nanotest_extensions (0.6.3)
15
16
  rack (1.2.1)
@@ -17,19 +18,17 @@ GEM
17
18
  rack (>= 1.0)
18
19
  rake (0.8.7)
19
20
  rspec-core (2.0.0.beta.20)
20
- tilt (1.0.1)
21
+ tilt (1.3.2)
21
22
 
22
23
  PLATFORMS
23
24
  ruby
24
25
 
25
26
  DEPENDENCIES
26
27
  bundler (>= 1.0.0)
27
- dictionary (>= 1.0)
28
+ json_pure
28
29
  nanotest
29
30
  nanotest_extensions
30
- rack (> 1.0)
31
31
  rack-test
32
32
  rake
33
33
  rspec-core (>= 2.0.0.beta.20)
34
34
  stethoscope!
35
- tilt (>= 1.0)
@@ -5,8 +5,8 @@ if defined?(Rails::Railtie)
5
5
  initializer 'stethoscope.middleare' do |app|
6
6
  app.middleware.use Stethoscope
7
7
 
8
- require 'stethoscope/checks/active_record' if defined? ( ActiveRecord )
9
- require 'stethoscope/checks/data_mapper' if defined? ( DataMapper )
8
+ require 'stethoscope/checks/active_record' if defined?(ActiveRecord)
9
+ require 'stethoscope/checks/data_mapper' if defined?(DataMapper)
10
10
  end
11
11
  end
12
12
  end
@@ -10,11 +10,11 @@
10
10
  <p class='error'>No Checks Enabled</p>
11
11
  <% else %>
12
12
  <ul class='checks'>
13
- <% checks.each do |name, hash| %>
13
+ <% checks.each do |(name, hash)| %>
14
14
  <li>
15
15
  <strong><%= name %>:</strong>
16
16
  <ul>
17
- <% hash.each do |item, value| %>
17
+ <% hash.each do |(item, value)| %>
18
18
  <li>
19
19
  <% if item.to_s == 'error' %>
20
20
  <h4><%= item.class %></h4>
@@ -1,3 +1,3 @@
1
1
  class Stethoscope
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/stethoscope.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'dictionary'
2
2
  require 'tilt'
3
+ require 'json'
3
4
  require 'stethoscope/rails'
4
5
 
5
6
  # Stethoscope is Rack middleware that provides a heartbeat function to an application.
@@ -110,28 +111,40 @@ class Stethoscope
110
111
 
111
112
  def call(env)
112
113
  request = Rack::Request.new(env)
113
- if request.path == Stethoscope.url
114
- responses = Dictionary.new do |d,k|
115
- dict = Dictionary.new
116
- dict[:status] = 200
117
- d[k] = dict
118
- end
114
+ return @app.call(env) unless check_heartbeat?(request.path)
115
+ responses = Hash.new do |h,k|
116
+ h[k] = {:status => 200}
117
+ end
119
118
 
120
- Stethoscope.checks.each do |name, check|
121
- begin
122
- check.call(responses[name])
123
- rescue => e
124
- responses[name][:error] = e
125
- responses[name][:status] = 500
126
- end
119
+ Stethoscope.checks.each do |name, check|
120
+ begin
121
+ check.call(responses[name])
122
+ rescue => e
123
+ responses[name][:error] = e
124
+ responses[name][:status] = 500
127
125
  end
126
+ end
128
127
 
129
- status = responses.any?{ |k,v| v[:status] && !((200..299).include?(v[:status])) } ? 500 : 200
130
- result = Stethoscope.template.render(Object.new, :checks => responses)
128
+ status = responses.any?{ |k,v| v[:status] && !((200..299).include?(v[:status])) } ? 500 : 200
129
+ _format = format(request.path)
131
130
 
132
- Rack::Response.new(result, status).finish
133
- else
134
- @app.call(env)
131
+ case format(request.path)
132
+ when :html
133
+ result = Stethoscope.template.render(Object.new, :checks => responses)
134
+ when :json
135
+ result = {:checks => responses, :status => status}.to_json
135
136
  end
137
+
138
+ Rack::Response.new(result, status).finish
139
+ end
140
+
141
+ private
142
+ def check_heartbeat?(path)
143
+ path == self.class.url || path == (self.class.url + '.json')
144
+ end
145
+
146
+ def format(path)
147
+ return :json if path == (self.class.url + '.json')
148
+ :html
136
149
  end
137
150
  end
data/stethoscope.gemspec CHANGED
@@ -14,9 +14,9 @@ Gem::Specification.new do |s|
14
14
  s.required_rubygems_version = ">= 1.3.6"
15
15
  s.rubyforge_project = "stethoscope"
16
16
 
17
- s.add_dependency "rack", "> 1.0"
18
- s.add_dependency "dictionary", ">= 1.0"
19
- s.add_dependency "tilt", ">= 1.0"
17
+ s.add_dependency "rack", "> 1.0"
18
+ s.add_dependency "dictionary", ">= 1.0"
19
+ s.add_dependency "tilt", ">= 1.0"
20
20
 
21
21
  s.add_development_dependency "bundler", ">= 1.0.0"
22
22
  s.add_development_dependency "rspec-core", ">= 2.0.0.beta.20"
@@ -24,6 +24,7 @@ Gem::Specification.new do |s|
24
24
  s.add_development_dependency "nanotest_extensions"
25
25
  s.add_development_dependency "rake"
26
26
  s.add_development_dependency "rack-test"
27
+ s.add_development_dependency 'json_pure'
27
28
 
28
29
 
29
30
  s.files = `git ls-files`.split("\n")
@@ -37,6 +37,18 @@ class TestStethoscope
37
37
  assert {response.body.to_s =~ /Ba-Boomp/ }
38
38
  end
39
39
 
40
+ test do
41
+ Stethoscope.check(:foo){ |response| response[:status] = 200; response[:test1] = :test1 }
42
+ Stethoscope.check(:bar){ |response| response[:status] = 200; response[:test2] = :test2 }
43
+
44
+ response = get "/heartbeat.json"
45
+ assert { response.status == 200 }
46
+ assert { response.body !~ /default/ }
47
+ result = JSON.parse(response.body)
48
+ assert { result['status'] == 200 }
49
+ assert { result['checks'] == {'foo' => {'status' => 200, 'test1' => 'test1'}, 'bar' => {'status' => 200, 'test2' => 'test2'} } }
50
+ end
51
+
40
52
  # Check checks
41
53
  context do
42
54
  setup { $captures = [] }
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stethoscope
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
5
- prerelease: false
4
+ hash: 23
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
8
  - 2
10
- version: 0.1.2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Daniel Neighman
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-07 00:00:00 +10:00
19
- default_executable:
18
+ date: 2011-08-17 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
21
  name: rack
@@ -153,6 +152,20 @@ dependencies:
153
152
  version: "0"
154
153
  type: :development
155
154
  version_requirements: *id009
155
+ - !ruby/object:Gem::Dependency
156
+ name: json_pure
157
+ prerelease: false
158
+ requirement: &id010 !ruby/object:Gem::Requirement
159
+ none: false
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ hash: 3
164
+ segments:
165
+ - 0
166
+ version: "0"
167
+ type: :development
168
+ version_requirements: *id010
156
169
  description: Heartbeat middleware for responding to heartbeat pings
157
170
  email:
158
171
  - has.sox@gmail.com
@@ -164,6 +177,7 @@ extra_rdoc_files: []
164
177
 
165
178
  files:
166
179
  - .gitignore
180
+ - .rvmrc
167
181
  - Gemfile
168
182
  - Gemfile.lock
169
183
  - LICENSE
@@ -178,7 +192,6 @@ files:
178
192
  - stethoscope.gemspec
179
193
  - test/test_helper.rb
180
194
  - test/test_stethoscope.rb
181
- has_rdoc: true
182
195
  homepage: http://rubygems.org/gems/stethoscope
183
196
  licenses: []
184
197
 
@@ -210,7 +223,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
210
223
  requirements: []
211
224
 
212
225
  rubyforge_project: stethoscope
213
- rubygems_version: 1.3.7
226
+ rubygems_version: 1.8.6
214
227
  signing_key:
215
228
  specification_version: 3
216
229
  summary: Heartbeat middleware for responding to heartbeat pings