http-double 0.1.0 → 0.1.1
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 +4 -4
- data/lib/http_double/request_logger.rb +28 -10
- data/lib/http_double/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1658a9ed204156a7f61cd7799d5c12394bbb3d44
|
4
|
+
data.tar.gz: 25a71de1ce9365e1a90440e89eedbb9798b7ec19
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3ee2b0dac09422840ff6502cb61f723a132ce92d561ac272596a441fc900d1dd1448703cb6d758a4b07036610a2969a72b732386a06c9c7edb2592c2d1da4fec
|
7
|
+
data.tar.gz: fdb056755c01ff438db6439c7c1587327a727295703c5d8120882113b23b9c4d9c35d99e613a29acd47356426a31f4c92b781698dc02b11914e32a83efa66744
|
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'rack/utils'
|
2
2
|
require 'ostruct'
|
3
3
|
require 'active_support/hash_with_indifferent_access'
|
4
|
+
require 'forwardable'
|
4
5
|
|
5
6
|
class HttpDouble::RequestLogger
|
6
7
|
|
@@ -24,12 +25,16 @@ class HttpDouble::RequestLogger
|
|
24
25
|
end
|
25
26
|
|
26
27
|
class Request
|
28
|
+
extend Forwardable
|
27
29
|
|
28
|
-
attr_reader :env
|
30
|
+
attr_reader :env
|
29
31
|
|
30
32
|
def initialize(env)
|
31
33
|
@env = env
|
32
|
-
|
34
|
+
end
|
35
|
+
|
36
|
+
def body
|
37
|
+
@body ||= rack_input.rewind && rack_input.read
|
33
38
|
end
|
34
39
|
|
35
40
|
def verb
|
@@ -41,22 +46,35 @@ class HttpDouble::RequestLogger
|
|
41
46
|
end
|
42
47
|
|
43
48
|
def [](field, index = nil)
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
result = result[index] if result and index
|
48
|
-
result
|
49
|
-
else
|
50
|
-
raise "The content type '#{env['CONTENT_TYPE']}' doesn't support indexed access"
|
51
|
-
end
|
49
|
+
result = parsed_input[field]
|
50
|
+
result = result[index] if result and index
|
51
|
+
result
|
52
52
|
end
|
53
53
|
|
54
|
+
delegate %i[first last map each size count length] => :parsed_input
|
55
|
+
|
54
56
|
private
|
55
57
|
|
58
|
+
def parsed_input
|
59
|
+
@parsed_input ||= case env['CONTENT_TYPE']
|
60
|
+
when 'application/x-www-form-urlencoded' then form_fields
|
61
|
+
when 'application/json', 'application/x-json' then json_input
|
62
|
+
else raise "The content type '#{env['CONTENT_TYPE']}' doesn't support indexed access"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def rack_input
|
67
|
+
@rack_input ||= env['rack.input']
|
68
|
+
end
|
69
|
+
|
56
70
|
def form_fields
|
57
71
|
@form_fields ||= IHash.new(Rack::Utils.parse_query(body).map { |key, value| [key.to_sym, value.is_a?(Array) ? value : [value]] }.to_h)
|
58
72
|
end
|
59
73
|
|
74
|
+
def json_input
|
75
|
+
@json_input ||= JSON.parse body, quirks_mode: true
|
76
|
+
end
|
77
|
+
|
60
78
|
end
|
61
79
|
|
62
80
|
end
|
data/lib/http_double/version.rb
CHANGED