Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

as_json should ignore all arguments. Add default to_json. #54

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/protocol/http/body/readable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def join
end
end

def as_json
def as_json(...)
{
class: self.class.name,
length: self.length,
Expand All @@ -102,6 +102,10 @@ def as_json
empty: self.empty?
}
end

def to_json(...)
as_json.to_json(...)
end
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/protocol/http/body/wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,17 @@ def read
@body.read
end

def as_json
def as_json(...)
{
class: self.class.name,
body: @body&.as_json
}
end

def to_json(...)
as_json.to_json(...)
end

def inspect
@body.inspect
end
Expand Down
6 changes: 5 additions & 1 deletion lib/protocol/http/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def idempotent?
@method != Methods::POST && (@body.nil? || @body.empty?)
end

def as_json
def as_json(...)
{
scheme: @scheme,
authority: @authority,
Expand All @@ -86,6 +86,10 @@ def as_json
}
end

def to_json(...)
as_json.to_json(...)
end

def to_s
"#{@scheme}://#{@authority}: #{@method} #{@path} #{@version}"
end
Expand Down
6 changes: 5 additions & 1 deletion lib/protocol/http/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def self.for_exception(exception)
Response[500, Headers['content-type' => 'text/plain'], ["#{exception.class}: #{exception.message}"]]
end

def as_json
def as_json(...)
{
version: @version,
status: @status,
Expand All @@ -114,6 +114,10 @@ def as_json
}
end

def to_json(...)
as_json.to_json(...)
end

def to_s
"#{@status} #{@version}"
end
Expand Down
16 changes: 16 additions & 0 deletions test/protocol/http/body/readable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,20 @@
expect(body.join).to be_nil
end
end

with "#as_json" do
it "generates a JSON representation" do
expect(body.as_json).to have_keys(
class: be == subject.name,
length: be_nil,
stream: be == false,
ready: be == false,
empty: be == false,
)
end

it "generates a JSON string" do
expect(JSON.dump(body)).to be == body.to_json
end
end
end
4 changes: 4 additions & 0 deletions test/protocol/http/body/wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,9 @@
body: be == source.as_json
)
end

it "generates a JSON string" do
expect(JSON.dump(body)).to be == body.to_json
end
end
end
6 changes: 6 additions & 0 deletions test/protocol/http/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

require 'protocol/http/request'

require 'json'

describe Protocol::HTTP::Request do
let(:headers) {Protocol::HTTP::Headers.new}
let(:body) {nil}
Expand Down Expand Up @@ -38,6 +40,10 @@
protocol: nil
}
end

it "generates a JSON string" do
expect(JSON.dump(request)).to be == request.to_json
end
end

it "should not be HEAD" do
Expand Down
8 changes: 8 additions & 0 deletions test/protocol/http/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@
protocol: be == nil,
)
end

it "generates a JSON string" do
expect(JSON.dump(response)).to be == response.to_json
end
end

it_behaves_like InformationalResponse
Expand Down Expand Up @@ -182,6 +186,10 @@
protocol: be == nil,
)
end

it "generates a JSON string" do
expect(JSON.dump(response)).to be == response.to_json
end
end

it_behaves_like SuccessfulResponse
Expand Down
Loading