Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 16 additions & 0 deletions lib/wreq_ruby/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ class Method
TRACE = nil # @return [Wreq::Method] HTTP TRACE method
PATCH = nil # @return [Wreq::Method] HTTP PATCH method
end

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

# HTTP version enumeration backed by Rust.
Expand All @@ -42,6 +48,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 +121,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
47 changes: 47 additions & 0 deletions lib/wreq_ruby/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,50 @@ def close; end
end
end
end

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

module Wreq
class Response
# Returns a string representation of the response.
#
# Includes status code, HTTP version, URL, and key headers.
#
# @return [String] Formatted response information
# @example
# puts response.to_s
# # => HTTP/1.1 200 OK
# # URL: https://example.com/api
# # Content-Type: application/json
# # Content-Length: 1024
# # ...
def to_s
lines = []

# Status line
lines << "#{version.to_s} #{status.to_s}"

# URL
lines << "URL: #{url}"

# Headers
if headers.respond_to?(:each)
headers.each do |name, value|
lines << "#{name}: #{value}"
end
end

# Body preview (first 200 chars if reasonable size)
if content_length && content_length > 0
body = text rescue nil
if body && !body.empty?
lines << ""
preview = body.length > 200 ? "#{body[0...200]}..." : body
lines << preview
end
end

lines.join("\n")
end
end
end
34 changes: 33 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,28 @@ 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 Method =====

impl Method {
/// Convert method to string.
#[inline]
pub fn to_s(&self) -> String {
self.into_ffi().to_string()
}
}

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

impl StatusCode {
/// Return the status code as an integer.
#[inline]
Expand Down Expand Up @@ -69,6 +92,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 @@ -87,13 +116,15 @@ pub fn include(ruby: &Ruby, gem_module: &RModule) -> Result<(), Error> {
method_class.const_set("HEAD", Method::HEAD)?;
method_class.const_set("TRACE", Method::TRACE)?;
method_class.const_set("OPTIONS", Method::OPTIONS)?;
method_class.define_method("to_s", method!(Method::to_s, 0))?;

let version_class = gem_module.define_class("Version", ruby.class_object())?;
version_class.const_set("HTTP_09", Version::HTTP_09)?;
version_class.const_set("HTTP_10", Version::HTTP_10)?;
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 +133,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