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
27 changes: 27 additions & 0 deletions lib/wreq_ruby/cookie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,33 @@ class SameSite
Lax = nil
# None same-site policy.
None = nil

# Returns the SameSite attribute name (e.g. "Strict", "Lax", "None").
# @return [String]
def to_s
end

# Returns the SameSite attribute as a lowercase symbol (e.g. :strict, :lax, :none).
# @return [Symbol]
def to_sym
end

# Value-based equality.
# @param other [Object]
# @return [Boolean]
def ==(other)
end

# Strict equality for Hash key and Set member semantics.
# @param other [Object]
# @return [Boolean]
def eql?(other)
end

# Hash value consistent with {#eql?} for use as Hash keys.
# @return [Integer]
def hash
end
end

# A single HTTP cookie.
Expand Down
53 changes: 53 additions & 0 deletions lib/wreq_ruby/emulate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,29 @@ class Profile
def to_s
end
end

unless method_defined?(:==)
# Value-based equality.
# @param other [Object]
# @return [Boolean]
def ==(other)
end
end

unless method_defined?(:eql?)
# Strict equality for Hash key and Set member semantics.
# @param other [Object]
# @return [Boolean]
def eql?(other)
end
end

unless method_defined?(:hash)
# Hash value consistent with {#eql?} for use as Hash keys.
# @return [Integer]
def hash
end
end
end

# Operating system platform enumeration backed by Rust.
Expand Down Expand Up @@ -194,6 +217,36 @@ class Platform
def to_s
end
end

unless method_defined?(:to_sym)
# Returns the platform as a lowercase symbol (e.g. :windows, :linux).
# @return [Symbol]
def to_sym
end
end

unless method_defined?(:==)
# Value-based equality.
# @param other [Object]
# @return [Boolean]
def ==(other)
end
end

unless method_defined?(:eql?)
# Strict equality for Hash key and Set member semantics.
# @param other [Object]
# @return [Boolean]
def eql?(other)
end
end

unless method_defined?(:hash)
# Hash value consistent with {#eql?} for use as Hash keys.
# @return [Integer]
def hash
end
end
end

# Emulation option wrapper.
Expand Down
74 changes: 74 additions & 0 deletions lib/wreq_ruby/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,43 @@ class Method
TRACE = nil # @return [Wreq::Method] HTTP TRACE method
PATCH = nil # @return [Wreq::Method] HTTP PATCH method
end

# Returns the HTTP method token (e.g. "GET", "POST").
# @return [String]
unless method_defined?(:to_s)
def to_s
end
end

# Returns the HTTP method as a lowercase symbol (e.g. :get, :post).
# @return [Symbol]
unless method_defined?(:to_sym)
def to_sym
end
end

# Value-based equality. Returns true when both represent the same HTTP method.
# @param other [Object]
# @return [Boolean]
unless method_defined?(:==)
def ==(other)
end
end

# Strict equality for Hash key and Set member semantics.
# @param other [Object]
# @return [Boolean]
unless method_defined?(:eql?)
def eql?(other)
end
end

# Hash value consistent with {#eql?} for use as Hash keys.
# @return [Integer]
unless method_defined?(:hash)
def hash
end
end
end

# HTTP version enumeration backed by Rust.
Expand Down Expand Up @@ -63,6 +100,21 @@ def to_s
def ==(other)
end
end

# Strict equality for Hash key and Set member semantics.
# @param other [Object]
# @return [Boolean]
unless method_defined?(:eql?)
def eql?(other)
end
end

# Hash value consistent with {#eql?} for use as Hash keys.
# @return [Integer]
unless method_defined?(:hash)
def hash
end
end
end

# HTTP status code wrapper.
Expand Down Expand Up @@ -141,6 +193,28 @@ def server_error?
# @return [String] Status code as string
def to_s
end

# Returns the status code as an integer.
# @return [Integer] the numeric HTTP status code
def to_i
end

# Value-based equality. Only compares with other StatusCode instances.
# @param other [Object]
# @return [Boolean]
def ==(other)
end

# Strict equality for Hash key and Set member semantics.
# @param other [Object]
# @return [Boolean]
def eql?(other)
end

# Hash value consistent with {#eql?} for use as Hash keys.
# @return [Integer]
def hash
end
end
end
end
Expand Down
26 changes: 26 additions & 0 deletions src/cookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ define_ruby_enum!(
None,
);

impl SameSite {
/// SameSite attribute as a string.
pub fn to_s(&self) -> &'static str {
match self {
SameSite::Strict => "Strict",
SameSite::Lax => "Lax",
SameSite::None => "None",
}
}

/// SameSite attribute as a lowercase Ruby symbol.
pub fn to_sym(&self) -> magnus::Symbol {
let name = match self {
SameSite::Strict => "strict",
SameSite::Lax => "lax",
SameSite::None => "none",
};
ruby!().to_symbol(name)
}
}

/// A single HTTP cookie.
#[derive(Clone)]
#[magnus::wrap(class = "Wreq::Cookie", free_immediately, size)]
Expand Down Expand Up @@ -281,6 +302,11 @@ pub fn include(ruby: &Ruby, gem_module: &RModule) -> Result<(), Error> {
// SameSite enum
let same_site_class = gem_module.define_class("SameSite", ruby.class_object())?;
SameSite::define_constants(same_site_class)?;
same_site_class.define_method("to_s", method!(SameSite::to_s, 0))?;
same_site_class.define_method("to_sym", method!(SameSite::to_sym, 0))?;
same_site_class.define_method("==", method!(SameSite::equals, 1))?;
same_site_class.define_method("eql?", method!(SameSite::is_eql, 1))?;
same_site_class.define_method("hash", method!(SameSite::hash_value, 0))?;

// Cookie class
let cookie_class = gem_module.define_class("Cookie", ruby.class_object())?;
Expand Down
18 changes: 18 additions & 0 deletions src/emulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,17 @@ impl Platform {
pub fn to_s(&self) -> String {
self.into_ffi().inspect()
}

pub fn to_sym(&self) -> magnus::Symbol {
let name = match self {
Platform::Windows => "windows",
Platform::MacOS => "macos",
Platform::Linux => "linux",
Platform::Android => "android",
Platform::IOS => "ios",
};
ruby!().to_symbol(name)
}
}

// ===== impl Emulation =====
Expand Down Expand Up @@ -222,11 +233,18 @@ pub fn include(ruby: &Ruby, gem_module: &RModule) -> Result<(), Error> {
// Profile enum binding
let profile = gem_module.define_class("Profile", ruby.class_object())?;
profile.define_method("to_s", method!(Profile::to_s, 0))?;
profile.define_method("==", method!(Profile::equals, 1))?;
profile.define_method("eql?", method!(Profile::is_eql, 1))?;
profile.define_method("hash", method!(Profile::hash_value, 0))?;
Profile::define_constants(profile)?;

// Platform enum binding
let platform = gem_module.define_class("Platform", ruby.class_object())?;
platform.define_method("to_s", method!(Platform::to_s, 0))?;
platform.define_method("to_sym", method!(Platform::to_sym, 0))?;
platform.define_method("==", method!(Platform::equals, 1))?;
platform.define_method("eql?", method!(Platform::is_eql, 1))?;
platform.define_method("hash", method!(Platform::hash_value, 0))?;
Platform::define_constants(platform)?;

// Emulation class binding
Expand Down
81 changes: 73 additions & 8 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,39 @@ define_ruby_enum!(
PATCH,
);

impl Method {
/// HTTP method token as a string.
#[inline]
pub fn to_s(&self) -> &'static str {
match self {
Method::GET => "GET",
Method::HEAD => "HEAD",
Method::POST => "POST",
Method::PUT => "PUT",
Method::DELETE => "DELETE",
Method::OPTIONS => "OPTIONS",
Method::TRACE => "TRACE",
Method::PATCH => "PATCH",
}
}

/// HTTP method as a lowercase Ruby symbol.
#[inline]
pub fn to_sym(&self) -> magnus::Symbol {
let name = match self {
Method::GET => "get",
Method::HEAD => "head",
Method::POST => "post",
Method::PUT => "put",
Method::DELETE => "delete",
Method::OPTIONS => "options",
Method::TRACE => "trace",
Method::PATCH => "patch",
};
ruby!().to_symbol(name)
}
}

/// HTTP status code.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
#[magnus::wrap(class = "Wreq::StatusCode", free_immediately, size)]
Expand All @@ -41,14 +74,6 @@ impl Version {
pub fn to_s(&self) -> String {
self.into_ffi().inspect()
}

/// Value-based equality for Ruby (`==`).
#[inline]
pub fn equals(&self, other: Value) -> bool {
<&Version>::try_convert(other)
.map(|other| *self == *other)
.unwrap_or(false)
}
}

impl TryConvert for Version {
Expand Down Expand Up @@ -101,6 +126,35 @@ impl StatusCode {
pub fn to_s(&self) -> String {
self.0.to_string()
}

/// Value-based equality for Ruby (`==`).
#[inline]
pub fn equals(&self, other: Value) -> bool {
<&StatusCode>::try_convert(other)
.map(|other| *self == *other)
.unwrap_or(false)
}

/// Strict equality for Ruby (`eql?`).
#[inline]
pub fn is_eql(&self, other: Value) -> bool {
self.equals(other)
}

/// Hash value for Ruby (`hash`).
#[inline]
pub fn hash_value(&self) -> u64 {
use std::hash::{Hash, Hasher};
let mut hasher = std::collections::hash_map::DefaultHasher::new();
self.0.hash(&mut hasher);
hasher.finish()
}

/// Return the status code as an integer (Ruby `to_i`).
#[inline]
pub const fn to_i(&self) -> u16 {
self.0.as_u16()
}
}

impl From<wreq::StatusCode> for StatusCode {
Expand All @@ -112,11 +166,18 @@ impl From<wreq::StatusCode> for StatusCode {
pub fn include(ruby: &Ruby, gem_module: &RModule) -> Result<(), Error> {
let method_class = gem_module.define_class("Method", ruby.class_object())?;
Method::define_constants(method_class)?;
method_class.define_method("to_s", method!(Method::to_s, 0))?;
method_class.define_method("to_sym", method!(Method::to_sym, 0))?;
method_class.define_method("==", method!(Method::equals, 1))?;
method_class.define_method("eql?", method!(Method::is_eql, 1))?;
method_class.define_method("hash", method!(Method::hash_value, 0))?;

let version_class = gem_module.define_class("Version", ruby.class_object())?;
Version::define_constants(version_class)?;
version_class.define_method("to_s", method!(Version::to_s, 0))?;
version_class.define_method("==", method!(Version::equals, 1))?;
version_class.define_method("eql?", method!(Version::is_eql, 1))?;
version_class.define_method("hash", method!(Version::hash_value, 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 @@ -126,6 +187,10 @@ pub fn include(ruby: &Ruby, gem_module: &RModule) -> Result<(), Error> {
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))?;
status_code_class.define_method("==", method!(StatusCode::equals, 1))?;
status_code_class.define_method("eql?", method!(StatusCode::is_eql, 1))?;
status_code_class.define_method("hash", method!(StatusCode::hash_value, 0))?;
status_code_class.define_method("to_i", method!(StatusCode::to_i, 0))?;

Ok(())
}
Loading