rack-logjam 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3afcec7ffd389b14ebacd722576d00e0925b2c91
4
+ data.tar.gz: fd6c4bcc7b2fcd6a06679afde498495654a7dd2a
5
+ SHA512:
6
+ metadata.gz: 25cd7cd84390166d4e5532c9cde283e55eb668279c9636193f50a8d340ba8792699b5105426065ba43104697a942c60d36fe0c5c59cf92eb13fb92a68ff827df
7
+ data.tar.gz: 5d73f70586ad76410055ac469e8f775395ab4f90952299a870e5f41731eec3cbe61f4247fa4bfc49122e04d57b07eb40516644df6fe7a2beec518ed26f66c75a
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-logjam.gemspec
4
+ gemspec
data/License.markdown ADDED
@@ -0,0 +1,9 @@
1
+ # The MIT License
2
+
3
+ Source code for _rack-logjam_ is Copyright © 2014 [Ninja Loss](mailto:ninja.loss@gmail.com) and [contributors](http://github.com/ninja-loss/rack-logjam/contributors "rack-logjam contributors at GitHub").
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED “AS IS,” WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,29 @@
1
+ # Rack::Logjam
2
+
3
+ Logs helpful HTTP information on Rack requests.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rack-logjam'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rack-logjam
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/ninja-loss/rack-logjam/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,7 @@
1
+ require "rack/logjam/version"
2
+
3
+ module Rack
4
+ module Logjam
5
+ # Your code goes here...
6
+ end
7
+ end
@@ -0,0 +1,137 @@
1
+ module Rack; module Logjam; class Middleware
2
+
3
+ def initialize( app )
4
+ @app = app
5
+ end
6
+
7
+ def call( env )
8
+ before env
9
+ app.call( env ).tap do |rack_response|
10
+ after env, *rack_response
11
+ end
12
+ end
13
+
14
+ protected
15
+
16
+ attr_reader :app
17
+
18
+ def before( env )
19
+ return unless api_request?( env )
20
+
21
+ Rails.logger.info <<-end_info
22
+ [#{ANSI.green { 'api' }}] #{ANSI.cyan { '--- Request Env ---' }}
23
+ #{ANSI.magenta { JSON.pretty_generate( request_log_data( env )) }}
24
+ [#{ANSI.green { 'api' }}] #{ANSI.cyan { '--- Request Body ---' }}
25
+ #{ANSI.cyan { formatted_request_body( env ) }}
26
+ end_info
27
+ end
28
+
29
+ def after( env, status, headers, response )
30
+ return unless api_request?( env )
31
+
32
+ Rails.logger.info <<-end_info
33
+ [#{ANSI.green { 'api' }}] #{ANSI.cyan { '--- Response ---' }}
34
+ Status: #{status}
35
+ Headers: #{headers.inspect}
36
+ Body:
37
+ #{ANSI.cyan { format_body( response.body, accept( env ), env ) }}
38
+ end_info
39
+ end
40
+
41
+ def request_log_data( env )
42
+ request_data = {
43
+ auth_token: env['HTTP_X_NCITE_AUTH_TOKEN'],
44
+ content_type: content_type( env ),
45
+ content_length: env['CONTENT_LENGTH'],
46
+ accept: accept( env ),
47
+ accept_version: env['HTTP_ACCEPT_VERSION'],
48
+ method: env['REQUEST_METHOD'],
49
+ path: path_info( env ),
50
+ query: query( env )
51
+ }
52
+ #request_data[:user_id] = current_user.id if current_user
53
+ request_data
54
+ end
55
+
56
+ def api_request?( env )
57
+ path_info( env ) =~ /^\/api\//
58
+ end
59
+
60
+ def content_type( env )
61
+ env['CONTENT_TYPE']
62
+ end
63
+
64
+ def accept( env )
65
+ env['HTTP_ACCEPT']
66
+ end
67
+
68
+ def path_info( env )
69
+ env['PATH_INFO']
70
+ end
71
+
72
+ def query( env )
73
+ URI.unescape( env['QUERY_STRING'] )
74
+ end
75
+
76
+ def formatted_request_body( env )
77
+ format_body( rack_input_content( env ), content_type( env ), env )
78
+ end
79
+
80
+ def format_body( body, format, env )
81
+ return body if body.strip.nil? || body.strip.empty?
82
+
83
+ if format == Mime::JSON.to_s
84
+ hash = truncate_json_attributes( body )
85
+ return JSON.pretty_generate( hash )
86
+ elsif format == Mime::XML.to_s
87
+ return Nokogiri.XML( body ) do |config|
88
+ config.default_xml.noblanks
89
+ end.to_xml( indent: 2 )
90
+ elsif format == Mime::URL_ENCODED_FORM.to_s
91
+ return URI.unescape( body )
92
+ elsif format == Mime::OCTET_STREAM.to_s
93
+ return "no body b/c content type is #{Mime::OCTET_STREAM}"
94
+ end
95
+
96
+ body
97
+ end
98
+
99
+ def rack_input_content( env )
100
+ ( rack_input = env['rack.input'] ).read.tap do |content|
101
+ rack_input.rewind
102
+ end
103
+ end
104
+
105
+ # Can currently use the following xpath expressions that will translate to json_path:
106
+ # /some/path -> $.some.path (search from root)
107
+ # some/path -> some.path (search for any sub-path that matches this, not bound to root)
108
+ # //path -> $..path (recursive search)
109
+ #
110
+ def truncated_attributes_as_xpath
111
+ %w(
112
+ //image_as_base64
113
+ //fingerprint_image_as_base64
114
+ )
115
+ end
116
+
117
+ def truncate_json_attributes( json )
118
+ json_path = JsonPath.for( json )
119
+ truncated_attributes_as_json_paths.each do |j_path|
120
+ json_path.gsub!( j_path ) { |val| (val.nil? || val.empty?) ? val : "#{val[0..25]} ... [TRUNCATED] ..." }
121
+ end
122
+ json_path.to_hash
123
+ end
124
+
125
+ def truncated_attributes_as_json_paths
126
+ truncated_attributes_as_xpath.map do |xpath|
127
+ if xpath.start_with?( '//' )
128
+ xpath.gsub( '//', '$..' )
129
+ else
130
+ parts = xpath.split( '/' )
131
+ parts[0] = '$' if parts[0] == ''
132
+ parts.join( '.' )
133
+ end
134
+ end
135
+ end
136
+
137
+ end; end; end
@@ -0,0 +1,9 @@
1
+ module Rack
2
+
3
+ module Logjam
4
+
5
+ VERSION = '0.0.1'
6
+
7
+ end
8
+
9
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/logjam/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'rack-logjam'
8
+ spec.version = Rack::Logjam::VERSION
9
+ spec.authors = ['Jason Harrelson', 'Nils Jonsson']
10
+ spec.email = ['ninja.loss@gmail.com']
11
+ spec.summary = 'Logs helpful HTTP information on Rack requests.'
12
+ spec.description = ''
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) do |f|
18
+ File.basename f
19
+ end
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.5'
24
+ spec.add_development_dependency 'rake'
25
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-logjam
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jason Harrelson
8
+ - Nils Jonsson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-02-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.5'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.5'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ description: ''
43
+ email:
44
+ - ninja.loss@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - License.markdown
52
+ - README.markdown
53
+ - Rakefile
54
+ - lib/rack/logjam.rb
55
+ - lib/rack/logjam/middleware.rb
56
+ - lib/rack/logjam/version.rb
57
+ - rack-logjam.gemspec
58
+ homepage: ''
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.2.1
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Logs helpful HTTP information on Rack requests.
82
+ test_files: []
83
+ has_rdoc: