Skip to content

Commit ac442ae

Browse files
authored
as_json should ignore all arguments. Add default to_json. (#54)
1 parent 6a4b229 commit ac442ae

File tree

8 files changed

+54
-4
lines changed

8 files changed

+54
-4
lines changed

lib/protocol/http/body/readable.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def join
9393
end
9494
end
9595

96-
def as_json
96+
def as_json(...)
9797
{
9898
class: self.class.name,
9999
length: self.length,
@@ -102,6 +102,10 @@ def as_json
102102
empty: self.empty?
103103
}
104104
end
105+
106+
def to_json(...)
107+
as_json.to_json(...)
108+
end
105109
end
106110
end
107111
end

lib/protocol/http/body/wrapper.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,17 @@ def read
5151
@body.read
5252
end
5353

54-
def as_json
54+
def as_json(...)
5555
{
5656
class: self.class.name,
5757
body: @body&.as_json
5858
}
5959
end
6060

61+
def to_json(...)
62+
as_json.to_json(...)
63+
end
64+
6165
def inspect
6266
@body.inspect
6367
end

lib/protocol/http/request.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def idempotent?
7373
@method != Methods::POST && (@body.nil? || @body.empty?)
7474
end
7575

76-
def as_json
76+
def as_json(...)
7777
{
7878
scheme: @scheme,
7979
authority: @authority,
@@ -86,6 +86,10 @@ def as_json
8686
}
8787
end
8888

89+
def to_json(...)
90+
as_json.to_json(...)
91+
end
92+
8993
def to_s
9094
"#{@scheme}://#{@authority}: #{@method} #{@path} #{@version}"
9195
end

lib/protocol/http/response.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def self.for_exception(exception)
104104
Response[500, Headers['content-type' => 'text/plain'], ["#{exception.class}: #{exception.message}"]]
105105
end
106106

107-
def as_json
107+
def as_json(...)
108108
{
109109
version: @version,
110110
status: @status,
@@ -114,6 +114,10 @@ def as_json
114114
}
115115
end
116116

117+
def to_json(...)
118+
as_json.to_json(...)
119+
end
120+
117121
def to_s
118122
"#{@status} #{@version}"
119123
end

test/protocol/http/body/readable.rb

+16
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,20 @@
4242
expect(body.join).to be_nil
4343
end
4444
end
45+
46+
with "#as_json" do
47+
it "generates a JSON representation" do
48+
expect(body.as_json).to have_keys(
49+
class: be == subject.name,
50+
length: be_nil,
51+
stream: be == false,
52+
ready: be == false,
53+
empty: be == false,
54+
)
55+
end
56+
57+
it "generates a JSON string" do
58+
expect(JSON.dump(body)).to be == body.to_json
59+
end
60+
end
4561
end

test/protocol/http/body/wrapper.rb

+4
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,9 @@
7171
body: be == source.as_json
7272
)
7373
end
74+
75+
it "generates a JSON string" do
76+
expect(JSON.dump(body)).to be == body.to_json
77+
end
7478
end
7579
end

test/protocol/http/request.rb

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
require 'protocol/http/request'
77

8+
require 'json'
9+
810
describe Protocol::HTTP::Request do
911
let(:headers) {Protocol::HTTP::Headers.new}
1012
let(:body) {nil}
@@ -38,6 +40,10 @@
3840
protocol: nil
3941
}
4042
end
43+
44+
it "generates a JSON string" do
45+
expect(JSON.dump(request)).to be == request.to_json
46+
end
4147
end
4248

4349
it "should not be HEAD" do

test/protocol/http/response.rb

+8
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@
113113
protocol: be == nil,
114114
)
115115
end
116+
117+
it "generates a JSON string" do
118+
expect(JSON.dump(response)).to be == response.to_json
119+
end
116120
end
117121

118122
it_behaves_like InformationalResponse
@@ -182,6 +186,10 @@
182186
protocol: be == nil,
183187
)
184188
end
189+
190+
it "generates a JSON string" do
191+
expect(JSON.dump(response)).to be == response.to_json
192+
end
185193
end
186194

187195
it_behaves_like SuccessfulResponse

0 commit comments

Comments
 (0)