Skip to content
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
10 changes: 10 additions & 0 deletions lib/wreq_ruby/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ class Version
HTTP_2 = nil # @return [Wreq::Version] HTTP/2
HTTP_3 = nil # @return [Wreq::Version] HTTP/3
end

# Returns a string representation of the HTTP version.
# @return [String] HTTP version as string
unless method_defined?(:to_s)
def to_s; end
end
end

# HTTP status code wrapper.
Expand Down Expand Up @@ -109,6 +115,10 @@ def client_error?; end
#
# @return [Boolean] true if status is 500-599
def server_error?; end

# Returns a string representation of the status code.
# @return [String] Status code as string
def to_s; end
end
end
end
55 changes: 55 additions & 0 deletions lib/wreq_ruby/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,58 @@ def close; end
end
end
end

# ======================== Ruby API Extensions ========================

module Wreq
class Response
# Returns a compact string representation of the response.
#
# Format: #<Wreq::Response STATUS content-type="..." body=SIZE>
#
# @return [String] Compact formatted response information
# @example
# puts response.to_s
# # => #<Wreq::Response 200 content-type="application/json" body=456B>
def to_s
parts = ["#<Wreq::Response"]

# Status code
parts << code.to_s

# Content-Type header if present
if headers.respond_to?(:get)
content_type = headers.get("content-type")
parts << "content-type=#{content_type.inspect}" if content_type
end

# Body size
if content_length
parts << "body=#{format_bytes(content_length)}"
end

parts.join(" ") + ">"
end

private

def format_bytes(bytes)
return "0B" if bytes.zero?

units = ["B", "KB", "MB", "GB"]
size = bytes.to_f
unit_index = 0

while size >= 1024 && unit_index < units.length - 1
size /= 1024.0
unit_index += 1
end

if unit_index == 0
"#{size.to_i}#{units[unit_index]}"
else
"#{size.round(1)}#{units[unit_index]}"
end
end
end
end
23 changes: 22 additions & 1 deletion src/http.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use magnus::{Error, Module, RModule, Ruby, method};
#![allow(clippy::wrong_self_convention)]
use magnus::{Error, Module, RModule, Ruby, method, typed_data::Inspect};

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

// ===== impl Version =====

impl Version {
/// Convert version to string.
#[inline]
pub fn to_s(&self) -> String {
self.into_ffi().inspect()
}
}

// ===== impl StatusCode =====

impl StatusCode {
/// Return the status code as an integer.
#[inline]
Expand Down Expand Up @@ -69,6 +82,12 @@ impl StatusCode {
pub fn is_server_error(&self) -> bool {
self.0.is_server_error()
}

/// Convert status code to string.
#[inline]
pub fn to_s(&self) -> String {
self.0.to_string()
}
}

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

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

Ok(())
}
2 changes: 1 addition & 1 deletion test/client_cookie_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def setup
@client = Wreq::Client.new(
cookie_store: true,
cookie_provider: @jar,
allow_redirects: true
allow_redirects: true,
)
end

Expand Down