rack-informant 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ rack-informant-*.gem
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
@@ -0,0 +1,14 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rack-informant (1.0.0)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+
10
+ PLATFORMS
11
+ ruby
12
+
13
+ DEPENDENCIES
14
+ rack-informant!
@@ -0,0 +1,75 @@
1
+ module Rack
2
+ # Middleware that "informs on" an application, reporting everything it does
3
+ # to its secret employer. Other middlewares may become hostile if they find
4
+ # out.
5
+ #
6
+ # After each request is processed, calls the provided block with a hash
7
+ # containing:
8
+ # * :method - HTTP request method, e.g. 'GET', 'POST'
9
+ # * :path - path segment of the request (no host, port or querystring),
10
+ # e.g. '/info'
11
+ # * :status - HTTP response status code, e.g. 200, 404
12
+ # * :runtime - total request processing time (by the server's reckoning) in
13
+ # *seconds*, as a float. e.g. 0.1 == 100 ms.
14
+ #
15
+ # This middleware is async-safe (i.e. should work correctly for both
16
+ # synchronous requests and asynchronous requests).
17
+ #
18
+ # Example usage:
19
+ #
20
+ # require 'rack/informant'
21
+ # use Rack::Informant do |request|
22
+ # # poor man's request logging (use Rack::CommonLogger instead)
23
+ # puts "#{request[:method]} #{request[:path]}: #{request[:status]} #{request[:runtime]}"
24
+ # end
25
+
26
+ class Informant
27
+ def initialize(app, &block)
28
+ @app = app
29
+ @callback = block || lambda {|*_|}
30
+ end
31
+
32
+ def call(env)
33
+ start = Time.now
34
+
35
+ if original_callback = env['async.callback']
36
+ env['async.callback'] = proc do |response|
37
+ status, headers, body = response
38
+
39
+ inform!(env, start, status, headers, body)
40
+
41
+ original_callback.call(response)
42
+ end
43
+ end
44
+
45
+ catch :async do
46
+ status, headers, body = @app.call(env)
47
+
48
+ # if we got this far, then @app.call didn't throw :async
49
+ return [status, headers, body] if status == -1 # alternative async API
50
+
51
+ # if we got *this* far, @app.call is definitely synchronous
52
+ inform!(env, start, status, headers, body)
53
+
54
+ return [status, headers, body]
55
+ end
56
+
57
+ # we only end up here if @app.call threw :async, so just throw it on.
58
+ throw :async
59
+ end
60
+
61
+ private
62
+ def inform!(env, start, status, headers, body)
63
+ runtime = Time.now - start
64
+
65
+ event = {
66
+ method: env['REQUEST_METHOD'],
67
+ path: env['PATH_INFO'],
68
+ status: status,
69
+ runtime: runtime,
70
+ }
71
+
72
+ @callback.call(event)
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,12 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'rack-informant'
3
+ s.authors = ['Rapportive Inc']
4
+ s.email = 'supportive@rapportive.com'
5
+ s.version = '1.1.0'
6
+ s.summary = %q{Reporting middleware.}
7
+ s.description = "Middleware that reports all requests to an interested party (e.g. for analytics)."
8
+ s.homepage = "https://github.com/rapportive-oss/rack-informant"
9
+ s.date = Date.today.to_s
10
+ s.files = `git ls-files`.split("\n")
11
+ s.require_paths = %w(lib)
12
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-informant
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rapportive Inc
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-15 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Middleware that reports all requests to an interested party (e.g. for
15
+ analytics).
16
+ email: supportive@rapportive.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - Gemfile.lock
24
+ - lib/rack/informant.rb
25
+ - rack-informant.gemspec
26
+ homepage: https://github.com/rapportive-oss/rack-informant
27
+ licenses: []
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 1.8.19
47
+ signing_key:
48
+ specification_version: 3
49
+ summary: Reporting middleware.
50
+ test_files: []
51
+ has_rdoc: