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
14 changes: 2 additions & 12 deletions lib/wreq_ruby/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,16 @@ def remote_addr
def bytes
end

# Get the response body as text.
#
# @return [String] Response body decoded as UTF-8 text
# @example
# html = response.text
# puts html
# @raise [Wreq::DecodingError] if body cannot be decoded as binary
def text
end

# Get the response body as text with a specific charset.
# This method allows you to specify a default encoding
# to use when decoding the response body.
# # @param default_encoding [String] Default encoding to use (e.g., "UTF-8")
# # @return [String] Response body decoded as text using the specified encoding
# @example
# html = response.text_with_charset("ISO-8859-1")
# html = response.text("ISO-8859-1")
# puts html
# @raise [Wreq::DecodingError] if body cannot be decoded with the specified encoding
def text_with_charset(default_encoding)
def text(default_encoding: "UTF-8")
end

# Parse the response body as JSON.
Expand Down
30 changes: 12 additions & 18 deletions src/client/resp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bytes::Bytes;
use futures_util::TryFutureExt;
use http::{Extensions, HeaderMap, response::Response as HttpResponse};
use http_body_util::BodyExt;
use magnus::{Error, Module, RArray, RModule, Ruby, Value, block::Yield};
use magnus::{Error, Module, RArray, RModule, Ruby, Value, block::Yield, scan_args::scan_args};
use wreq::Uri;

use crate::{
Expand Down Expand Up @@ -173,20 +173,18 @@ impl Response {
rt::try_block_on(response.bytes().map_err(wreq_error_to_magnus))
}

/// Get the response body as a UTF-8 string.
pub fn text(&self) -> Result<String, Error> {
let response = self.response(false)?;
rt::try_block_on(response.text().map_err(wreq_error_to_magnus))
}

/// Get the full response text given a specific encoding.
pub fn text_with_charset(&self, default_encoding: String) -> Result<String, Error> {
pub fn text(&self, args: &[Value]) -> Result<String, Error> {
let args = scan_args::<(), (Option<String>,), (), (), (), ()>(args)?;
let response = self.response(false)?;
rt::try_block_on(
response
.text_with_charset(default_encoding)
.map_err(wreq_error_to_magnus),
)
match args.optional.0 {
Some(encoding) => rt::try_block_on(
response
.text_with_charset(encoding)
.map_err(wreq_error_to_magnus),
),
None => rt::try_block_on(response.text().map_err(wreq_error_to_magnus)),
}
}

/// Get the response body as JSON.
Expand Down Expand Up @@ -238,11 +236,7 @@ pub fn include(ruby: &Ruby, gem_module: &RModule) -> Result<(), Error> {
response_class.define_method("local_addr", magnus::method!(Response::local_addr, 0))?;
response_class.define_method("remote_addr", magnus::method!(Response::remote_addr, 0))?;
response_class.define_method("bytes", magnus::method!(Response::bytes, 0))?;
response_class.define_method("text", magnus::method!(Response::text, 0))?;
response_class.define_method(
"text_with_charset",
magnus::method!(Response::text_with_charset, 1),
)?;
response_class.define_method("text", magnus::method!(Response::text, -1))?;
response_class.define_method("json", magnus::method!(Response::json, 0))?;
response_class.define_method("chunks", magnus::method!(Response::chunks, 0))?;
response_class.define_method("close", magnus::method!(Response::close, 0))?;
Expand Down