Skip to content

Commit d100224

Browse files
authored
feat(response): implement to_s for Response, Version and StatusCode (#17)
1 parent a39d24d commit d100224

File tree

4 files changed

+88
-2
lines changed

4 files changed

+88
-2
lines changed

lib/wreq_ruby/http.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ class Version
4242
HTTP_2 = nil # @return [Wreq::Version] HTTP/2
4343
HTTP_3 = nil # @return [Wreq::Version] HTTP/3
4444
end
45+
46+
# Returns a string representation of the HTTP version.
47+
# @return [String] HTTP version as string
48+
unless method_defined?(:to_s)
49+
def to_s; end
50+
end
4551
end
4652

4753
# HTTP status code wrapper.
@@ -109,6 +115,10 @@ def client_error?; end
109115
#
110116
# @return [Boolean] true if status is 500-599
111117
def server_error?; end
118+
119+
# Returns a string representation of the status code.
120+
# @return [String] Status code as string
121+
def to_s; end
112122
end
113123
end
114124
end

lib/wreq_ruby/response.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,58 @@ def close; end
112112
end
113113
end
114114
end
115+
116+
# ======================== Ruby API Extensions ========================
117+
118+
module Wreq
119+
class Response
120+
# Returns a compact string representation of the response.
121+
#
122+
# Format: #<Wreq::Response STATUS content-type="..." body=SIZE>
123+
#
124+
# @return [String] Compact formatted response information
125+
# @example
126+
# puts response.to_s
127+
# # => #<Wreq::Response 200 content-type="application/json" body=456B>
128+
def to_s
129+
parts = ["#<Wreq::Response"]
130+
131+
# Status code
132+
parts << code.to_s
133+
134+
# Content-Type header if present
135+
if headers.respond_to?(:get)
136+
content_type = headers.get("content-type")
137+
parts << "content-type=#{content_type.inspect}" if content_type
138+
end
139+
140+
# Body size
141+
if content_length
142+
parts << "body=#{format_bytes(content_length)}"
143+
end
144+
145+
parts.join(" ") + ">"
146+
end
147+
148+
private
149+
150+
def format_bytes(bytes)
151+
return "0B" if bytes.zero?
152+
153+
units = ["B", "KB", "MB", "GB"]
154+
size = bytes.to_f
155+
unit_index = 0
156+
157+
while size >= 1024 && unit_index < units.length - 1
158+
size /= 1024.0
159+
unit_index += 1
160+
end
161+
162+
if unit_index == 0
163+
"#{size.to_i}#{units[unit_index]}"
164+
else
165+
"#{size.round(1)}#{units[unit_index]}"
166+
end
167+
end
168+
end
169+
end

src/http.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use magnus::{Error, Module, RModule, Ruby, method};
1+
#![allow(clippy::wrong_self_convention)]
2+
use magnus::{Error, Module, RModule, Ruby, method, typed_data::Inspect};
23

34
define_ruby_enum!(
45
/// An HTTP version.
@@ -33,6 +34,18 @@ define_ruby_enum!(
3334
#[magnus::wrap(class = "Wreq::StatusCode", free_immediately, size)]
3435
pub struct StatusCode(pub wreq::StatusCode);
3536

37+
// ===== impl Version =====
38+
39+
impl Version {
40+
/// Convert version to string.
41+
#[inline]
42+
pub fn to_s(&self) -> String {
43+
self.into_ffi().inspect()
44+
}
45+
}
46+
47+
// ===== impl StatusCode =====
48+
3649
impl StatusCode {
3750
/// Return the status code as an integer.
3851
#[inline]
@@ -69,6 +82,12 @@ impl StatusCode {
6982
pub fn is_server_error(&self) -> bool {
7083
self.0.is_server_error()
7184
}
85+
86+
/// Convert status code to string.
87+
#[inline]
88+
pub fn to_s(&self) -> String {
89+
self.0.to_string()
90+
}
7291
}
7392

7493
impl From<wreq::StatusCode> for StatusCode {
@@ -94,6 +113,7 @@ pub fn include(ruby: &Ruby, gem_module: &RModule) -> Result<(), Error> {
94113
version_class.const_set("HTTP_11", Version::HTTP_11)?;
95114
version_class.const_set("HTTP_2", Version::HTTP_2)?;
96115
version_class.const_set("HTTP_3", Version::HTTP_3)?;
116+
version_class.define_method("to_s", method!(Version::to_s, 0))?;
97117

98118
let status_code_class = gem_module.define_class("StatusCode", ruby.class_object())?;
99119
status_code_class.define_method("as_int", method!(StatusCode::as_int, 0))?;
@@ -102,6 +122,7 @@ pub fn include(ruby: &Ruby, gem_module: &RModule) -> Result<(), Error> {
102122
status_code_class.define_method("redirection?", method!(StatusCode::is_redirection, 0))?;
103123
status_code_class.define_method("client_error?", method!(StatusCode::is_client_error, 0))?;
104124
status_code_class.define_method("server_error?", method!(StatusCode::is_server_error, 0))?;
125+
status_code_class.define_method("to_s", method!(StatusCode::to_s, 0))?;
105126

106127
Ok(())
107128
}

test/client_cookie_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def setup
1010
@client = Wreq::Client.new(
1111
cookie_store: true,
1212
cookie_provider: @jar,
13-
allow_redirects: true
13+
allow_redirects: true,
1414
)
1515
end
1616

0 commit comments

Comments
 (0)