rack-logjam 0.0.1 → 0.0.2

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: 3afcec7ffd389b14ebacd722576d00e0925b2c91
4
- data.tar.gz: fd6c4bcc7b2fcd6a06679afde498495654a7dd2a
3
+ metadata.gz: 73b701b47ef0a2b508486982ca4f8feb85c52c98
4
+ data.tar.gz: 4484c3413f56d8e829da75ac23c9018c26d45d08
5
5
  SHA512:
6
- metadata.gz: 25cd7cd84390166d4e5532c9cde283e55eb668279c9636193f50a8d340ba8792699b5105426065ba43104697a942c60d36fe0c5c59cf92eb13fb92a68ff827df
7
- data.tar.gz: 5d73f70586ad76410055ac469e8f775395ab4f90952299a870e5f41731eec3cbe61f4247fa4bfc49122e04d57b07eb40516644df6fe7a2beec518ed26f66c75a
6
+ metadata.gz: b571b14bd4f51e7efb8d8ea1e103c05b2b42c316a3f0af7a04f19c9df8bb1e996a998b1ab487f7524cca60693de8e446db3df170d4706320aac1f581c7aefbd9
7
+ data.tar.gz: f73607ebeaf91bbf78634416e56d95f9bc3eab977a1f3c5adbf920d563ed83a564c3747936ad99f5fd7e935a4320d33d0a5fc12cc3aef223f50a406a16698a3d
@@ -1,137 +1,148 @@
1
- module Rack; module Logjam; class Middleware
1
+ require 'action_dispatch/http/mime_type'
2
+ require 'jsonpath'
2
3
 
3
- def initialize( app )
4
- @app = app
5
- end
4
+ module Rack
6
5
 
7
- def call( env )
8
- before env
9
- app.call( env ).tap do |rack_response|
10
- after env, *rack_response
11
- end
12
- end
6
+ module Logjam
7
+
8
+ class Middleware
9
+
10
+ def initialize( app )
11
+ @app = app
12
+ end
13
+
14
+ def call( env )
15
+ before env
16
+ app.call( env ).tap do |rack_response|
17
+ after env, *rack_response
18
+ end
19
+ end
13
20
 
14
- protected
21
+ protected
15
22
 
16
- attr_reader :app
23
+ attr_reader :app
17
24
 
18
- def before( env )
19
- return unless api_request?( env )
25
+ def before( env )
26
+ return unless api_request?( env )
20
27
 
21
- Rails.logger.info <<-end_info
28
+ Rails.logger.info <<-end_info
22
29
  [#{ANSI.green { 'api' }}] #{ANSI.cyan { '--- Request Env ---' }}
23
30
  #{ANSI.magenta { JSON.pretty_generate( request_log_data( env )) }}
24
31
  [#{ANSI.green { 'api' }}] #{ANSI.cyan { '--- Request Body ---' }}
25
32
  #{ANSI.cyan { formatted_request_body( env ) }}
26
- end_info
27
- end
33
+ end_info
34
+ end
28
35
 
29
- def after( env, status, headers, response )
30
- return unless api_request?( env )
36
+ def after( env, status, headers, response )
37
+ return unless api_request?( env )
31
38
 
32
- Rails.logger.info <<-end_info
39
+ Rails.logger.info <<-end_info
33
40
  [#{ANSI.green { 'api' }}] #{ANSI.cyan { '--- Response ---' }}
34
41
  Status: #{status}
35
42
  Headers: #{headers.inspect}
36
43
  Body:
37
44
  #{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
45
+ end_info
46
+ end
55
47
 
56
- def api_request?( env )
57
- path_info( env ) =~ /^\/api\//
58
- end
48
+ def request_log_data( env )
49
+ request_data = {
50
+ auth_token: env['HTTP_X_NCITE_AUTH_TOKEN'],
51
+ content_type: content_type( env ),
52
+ content_length: env['CONTENT_LENGTH'],
53
+ accept: accept( env ),
54
+ accept_version: env['HTTP_ACCEPT_VERSION'],
55
+ method: env['REQUEST_METHOD'],
56
+ path: path_info( env ),
57
+ query: query( env )
58
+ }
59
+ #request_data[:user_id] = current_user.id if current_user
60
+ request_data
61
+ end
59
62
 
60
- def content_type( env )
61
- env['CONTENT_TYPE']
62
- end
63
+ def api_request?( env )
64
+ path_info( env ) =~ /^\/api\//
65
+ end
63
66
 
64
- def accept( env )
65
- env['HTTP_ACCEPT']
66
- end
67
+ def content_type( env )
68
+ env['CONTENT_TYPE']
69
+ end
67
70
 
68
- def path_info( env )
69
- env['PATH_INFO']
70
- end
71
+ def accept( env )
72
+ env['HTTP_ACCEPT']
73
+ end
71
74
 
72
- def query( env )
73
- URI.unescape( env['QUERY_STRING'] )
74
- end
75
+ def path_info( env )
76
+ env['PATH_INFO']
77
+ end
75
78
 
76
- def formatted_request_body( env )
77
- format_body( rack_input_content( env ), content_type( env ), env )
78
- end
79
+ def query( env )
80
+ URI.unescape( env['QUERY_STRING'] )
81
+ end
79
82
 
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
83
+ def formatted_request_body( env )
84
+ format_body( rack_input_content( env ), content_type( env ), env )
85
+ end
95
86
 
96
- body
97
- end
87
+ def format_body( body, format, env )
88
+ return body if body.strip.nil? || body.strip.empty?
89
+
90
+ if format == ::Mime::JSON.to_s
91
+ hash = truncate_json_attributes( body )
92
+ return ::JSON.pretty_generate( hash )
93
+ elsif format == ::Mime::XML.to_s
94
+ return ::Nokogiri.XML( body ) do |config|
95
+ config.default_xml.noblanks
96
+ end.to_xml( indent: 2 )
97
+ elsif format == ::Mime::URL_ENCODED_FORM.to_s
98
+ return URI.unescape( body )
99
+ elsif format == ::Mime::OCTET_STREAM.to_s
100
+ return "no body b/c content type is #{::Mime::OCTET_STREAM}"
101
+ end
102
+
103
+ body
104
+ end
98
105
 
99
- def rack_input_content( env )
100
- ( rack_input = env['rack.input'] ).read.tap do |content|
101
- rack_input.rewind
102
- end
103
- end
106
+ def rack_input_content( env )
107
+ ( rack_input = env['rack.input'] ).read.tap do |content|
108
+ rack_input.rewind
109
+ end
110
+ end
104
111
 
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
112
+ # Can currently use the following xpath expressions that will translate to json_path:
113
+ # /some/path -> $.some.path (search from root)
114
+ # some/path -> some.path (search for any sub-path that matches this, not bound to root)
115
+ # //path -> $..path (recursive search)
116
+ #
117
+ def truncated_attributes_as_xpath
118
+ %w(
119
+ //image_as_base64
120
+ //fingerprint_image_as_base64
121
+ )
122
+ end
116
123
 
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
+ def truncate_json_attributes( json )
125
+ json_path = ::JsonPath.for( json )
126
+ truncated_attributes_as_json_paths.each do |j_path|
127
+ json_path.gsub!( j_path ) { |val| (val.nil? || val.empty?) ? val : "#{val[0..25]} ... [TRUNCATED] ..." }
128
+ end
129
+ json_path.to_hash
130
+ end
124
131
 
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( '.' )
132
+ def truncated_attributes_as_json_paths
133
+ truncated_attributes_as_xpath.map do |xpath|
134
+ if xpath.start_with?( '//' )
135
+ xpath.gsub( '//', '$..' )
136
+ else
137
+ parts = xpath.split( '/' )
138
+ parts[0] = '$' if parts[0] == ''
139
+ parts.join( '.' )
140
+ end
141
+ end
133
142
  end
143
+
134
144
  end
145
+
135
146
  end
136
147
 
137
- end; end; end
148
+ end
@@ -2,7 +2,7 @@ module Rack
2
2
 
3
3
  module Logjam
4
4
 
5
- VERSION = '0.0.1'
5
+ VERSION = '0.0.2'
6
6
 
7
7
  end
8
8
 
data/lib/rack/logjam.rb CHANGED
@@ -1,7 +1,10 @@
1
- require "rack/logjam/version"
2
-
3
1
  module Rack
2
+
4
3
  module Logjam
5
- # Your code goes here...
4
+
5
+ autoload :VERSION, 'rack/logjam/version'
6
+ autoload :Middleware, 'rack/logjam/middleware'
7
+
6
8
  end
9
+
7
10
  end
@@ -0,0 +1 @@
1
+ require 'rack/logjam'
data/rack-logjam.gemspec CHANGED
@@ -20,6 +20,10 @@ Gem::Specification.new do |spec|
20
20
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
21
  spec.require_paths = ['lib']
22
22
 
23
+ spec.add_dependency 'actionpack', '~> 3.2'
24
+ spec.add_dependency 'jsonpath', '~> 0.5'
25
+ spec.add_dependency 'nokogiri', '~> 1.6'
26
+
23
27
  spec.add_development_dependency 'bundler', '~> 1.5'
24
28
  spec.add_development_dependency 'rake'
25
29
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-logjam
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Harrelson
@@ -11,6 +11,48 @@ bindir: bin
11
11
  cert_chain: []
12
12
  date: 2014-02-09 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: actionpack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '3.2'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '3.2'
28
+ - !ruby/object:Gem::Dependency
29
+ name: jsonpath
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '0.5'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '0.5'
42
+ - !ruby/object:Gem::Dependency
43
+ name: nokogiri
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '1.6'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '1.6'
14
56
  - !ruby/object:Gem::Dependency
15
57
  name: bundler
16
58
  requirement: !ruby/object:Gem::Requirement
@@ -51,6 +93,7 @@ files:
51
93
  - License.markdown
52
94
  - README.markdown
53
95
  - Rakefile
96
+ - lib/rack-logjam.rb
54
97
  - lib/rack/logjam.rb
55
98
  - lib/rack/logjam/middleware.rb
56
99
  - lib/rack/logjam/version.rb