watchdocs-rails 0.6.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/watchdocs/rails.rb +1 -0
- data/lib/watchdocs/rails/helpers.rb +3 -0
- data/lib/watchdocs/rails/helpers/body_helper.rb +38 -0
- data/lib/watchdocs/rails/helpers/headers_helper.rb +19 -0
- data/lib/watchdocs/rails/helpers/query_string_helper.rb +16 -0
- data/lib/watchdocs/rails/middleware.rb +17 -47
- data/lib/watchdocs/rails/version.rb +1 -1
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7d1845d7f31d111819223a47123e0e1faf07bfb8
|
4
|
+
data.tar.gz: b39fc2397b69a677627adaf348c23146f555e1e9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ee89cfdf079de10cfb05cc5d9812c992fe0aba7f741c7f233b2adcb92bd50b3bb601bc0c00e5f3a88cdefa315ef2bc09f8b15bda0448c1293c33f17b9ab0d657
|
7
|
+
data.tar.gz: d54d105671e5479961455a3154508c3129780a15f1f3618003a7f3155b045181996f0afe38d19d6fe54c90e599db232cd5eb8923ab0497d4c6a1a70567f1b78f
|
data/lib/watchdocs/rails.rb
CHANGED
@@ -0,0 +1,38 @@
|
|
1
|
+
module Watchdocs
|
2
|
+
module Rails
|
3
|
+
module Helpers
|
4
|
+
module BodyHelper
|
5
|
+
def body_string(body)
|
6
|
+
body_string = ''
|
7
|
+
body.each { |line| body_string += line } if body
|
8
|
+
body_string
|
9
|
+
end
|
10
|
+
|
11
|
+
def parse_response_body(body)
|
12
|
+
return if body.empty?
|
13
|
+
filter_data(JSON.parse(body))
|
14
|
+
rescue JSON::ParserError => e
|
15
|
+
log_and_return_empty "Invalid JSON data: #{e.message}, Body: #{body}"
|
16
|
+
rescue StandardError
|
17
|
+
log_and_return_empty "Response body format not supported. Body: #{body}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def parse_request_body(body)
|
21
|
+
return if body.empty?
|
22
|
+
filter_data(JSON.parse(body))
|
23
|
+
rescue JSON::ParserError
|
24
|
+
begin
|
25
|
+
filter_data(Rack::Utils.parse_nested_query(body))
|
26
|
+
rescue StandardError
|
27
|
+
log_and_return_empty "Request body format not supported. Body: #{body}"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def log_and_return_empty(error)
|
32
|
+
$stderr.puts "Watchdocs Middleware Error: #{error}"
|
33
|
+
{}
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Watchdocs
|
2
|
+
module Rails
|
3
|
+
module Helpers
|
4
|
+
module HeadersHelper
|
5
|
+
def request_headers(env)
|
6
|
+
env.keys
|
7
|
+
.select { |k| k.start_with? 'HTTP_' }
|
8
|
+
.map { |k| format_header(k) }
|
9
|
+
.sort
|
10
|
+
end
|
11
|
+
|
12
|
+
def format_header(header)
|
13
|
+
header.sub(/^HTTP_/, '')
|
14
|
+
.tr('_', '-')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Watchdocs
|
2
|
+
module Rails
|
3
|
+
module Helpers
|
4
|
+
module QueryStringHelper
|
5
|
+
def parse_query_string(params)
|
6
|
+
filter_data(
|
7
|
+
Rack::Utils.parse_nested_query(params)
|
8
|
+
)
|
9
|
+
rescue StandardError
|
10
|
+
$stderr.puts "Query String format not supported. String: #{params}"
|
11
|
+
{}
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -1,6 +1,10 @@
|
|
1
1
|
module Watchdocs
|
2
2
|
module Rails
|
3
3
|
class Middleware
|
4
|
+
include Rails::Helpers::HeadersHelper
|
5
|
+
include Rails::Helpers::BodyHelper
|
6
|
+
include Rails::Helpers::QueryStringHelper
|
7
|
+
|
4
8
|
attr_reader :app, :report
|
5
9
|
|
6
10
|
def initialize(app)
|
@@ -10,12 +14,16 @@ module Watchdocs
|
|
10
14
|
|
11
15
|
def call(env)
|
12
16
|
app.call(env).tap do |response|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
17
|
+
begin
|
18
|
+
if json_response?(response)
|
19
|
+
clear_report
|
20
|
+
catch_request(env)
|
21
|
+
catch_response(response)
|
22
|
+
match_endpoint_pattern
|
23
|
+
record_call
|
24
|
+
end
|
25
|
+
rescue StandardError => e
|
26
|
+
$stderr.puts "Watchdocs Middleware Error: #{e.message}"
|
19
27
|
end
|
20
28
|
end
|
21
29
|
end
|
@@ -35,7 +43,7 @@ module Watchdocs
|
|
35
43
|
@report[:request] = {
|
36
44
|
method: env['REQUEST_METHOD'],
|
37
45
|
url: env['PATH_INFO'],
|
38
|
-
query_string_params:
|
46
|
+
query_string_params: parse_query_string(env['QUERY_STRING']),
|
39
47
|
body: parse_request_body(env['rack.input'].read),
|
40
48
|
headers: request_headers(env)
|
41
49
|
}
|
@@ -70,46 +78,8 @@ module Watchdocs
|
|
70
78
|
::Rails.env.test?
|
71
79
|
end
|
72
80
|
|
73
|
-
def
|
74
|
-
|
75
|
-
.select { |k| k.start_with? 'HTTP_' }
|
76
|
-
.map { |k| format_header(k) }
|
77
|
-
.sort
|
78
|
-
end
|
79
|
-
|
80
|
-
def format_header(header)
|
81
|
-
header.sub(/^HTTP_/, '')
|
82
|
-
.tr('_', '-')
|
83
|
-
end
|
84
|
-
|
85
|
-
def body_string(body)
|
86
|
-
body_string = ''
|
87
|
-
body.each { |line| body_string += line } if body
|
88
|
-
body_string
|
89
|
-
end
|
90
|
-
|
91
|
-
def parse_response_body(body)
|
92
|
-
return if body.empty?
|
93
|
-
filter_body(JSON.parse(body))
|
94
|
-
rescue JSON::ParserError => e
|
95
|
-
{ watchdocs_error: "Invalid JSON data: #{e.message}" }
|
96
|
-
rescue StandardError
|
97
|
-
{ watchdocs_error: 'Response body format not supported' }
|
98
|
-
end
|
99
|
-
|
100
|
-
def parse_request_body(body)
|
101
|
-
return if body.empty?
|
102
|
-
filter_body(JSON.parse(body))
|
103
|
-
rescue JSON::ParserError
|
104
|
-
begin
|
105
|
-
filter_body(Rack::Utils.parse_nested_query(body))
|
106
|
-
rescue StandardError
|
107
|
-
{ watchdocs_error: 'Request body format not supported' }
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
def filter_body(body)
|
112
|
-
body.is_a?(Enumerable) ? body.filter_data : body
|
81
|
+
def filter_data(data)
|
82
|
+
data.is_a?(Enumerable) ? data.filter_data : data
|
113
83
|
end
|
114
84
|
end
|
115
85
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: watchdocs-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- mazikwyry
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-04-
|
11
|
+
date: 2017-04-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -141,6 +141,10 @@ files:
|
|
141
141
|
- lib/watchdocs/rails/core_extensions/enumerable.rb
|
142
142
|
- lib/watchdocs/rails/core_extensions/hash.rb
|
143
143
|
- lib/watchdocs/rails/core_extensions/object.rb
|
144
|
+
- lib/watchdocs/rails/helpers.rb
|
145
|
+
- lib/watchdocs/rails/helpers/body_helper.rb
|
146
|
+
- lib/watchdocs/rails/helpers/headers_helper.rb
|
147
|
+
- lib/watchdocs/rails/helpers/query_string_helper.rb
|
144
148
|
- lib/watchdocs/rails/middleware.rb
|
145
149
|
- lib/watchdocs/rails/recordings.rb
|
146
150
|
- lib/watchdocs/rails/recordings/exporter.rb
|