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
1 change: 1 addition & 0 deletions lib/wreq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

# Load type hint definitions
require_relative "wreq_ruby/http"
require_relative "wreq_ruby/emulate"
require_relative "wreq_ruby/client"
require_relative "wreq_ruby/response"
require_relative "wreq_ruby/body"
Expand Down
7 changes: 6 additions & 1 deletion lib/wreq_ruby/emulate.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# frozen_string_literal: true

# Profile and platform constants mirror the native enum variant names.
# standard:disable Naming/ConstantName

module Wreq
# Browser and client fingerprint profile enumeration backed by Rust.
#
Expand Down Expand Up @@ -279,7 +282,7 @@ def hash
class Emulation
# Native fields and methods are set by the extension.
# This stub is for documentation only.
unless method_defined?(:new)
unless singleton_methods(false).include?(:new)
# @param profile [Wreq::Profile, nil] Fingerprint profile to emulate
# @param platform [Wreq::Platform, nil] Operating system platform to emulate
# @param http2 [Boolean, nil] Whether HTTP/2 emulation is enabled; defaults
Expand All @@ -294,3 +297,5 @@ def self.new(**options)
end
end
end

# standard:enable Naming/ConstantName
31 changes: 6 additions & 25 deletions src/cookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,19 @@ use crate::{
gvl,
};

// Defines constant registration, `into_ffi`/`from_ffi`, and handlers for
// Ruby's `to_s`, `to_sym`, `==`, `eql?`, and `hash` methods.
define_ruby_enum!(
/// The Cookie SameSite attribute.
const,
SameSite,
"Wreq::SameSite",
cookie::SameSite,
Strict,
Lax,
None,
symbols:
Strict => "strict",
Lax => "lax",
None => "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(ruby: &Ruby, rb_self: &Self) -> magnus::Symbol {
let name = match *rb_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
49 changes: 11 additions & 38 deletions src/emulate.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use ::serde::Deserialize;
use magnus::{
Error, Module, Object, RModule, Ruby, Value, function, method,
typed_data::{Inspect, Obj},
};
use magnus::{Error, Module, Object, RModule, Ruby, Value, function, method, typed_data::Obj};

use crate::options::{NativeOption, Options};

Expand Down Expand Up @@ -41,9 +38,10 @@ impl Builder {
}
}

// Defines constant registration, `into_ffi`/`from_ffi`, and handlers for
// Ruby's `to_s`, `==`, `eql?`, and `hash` methods.
define_ruby_enum!(
/// An emulation profile.
const,
Profile,
"Wreq::Profile",
wreq_util::Profile,
Expand Down Expand Up @@ -187,51 +185,26 @@ define_ruby_enum!(
Opera131,
);

// Defines constant registration, `into_ffi`/`from_ffi`, and handlers for
// Ruby's `to_s`, `to_sym`, `==`, `eql?`, and `hash` methods.
define_ruby_enum!(
/// An emulation profile for OS.
const,
Platform,
"Wreq::Platform",
wreq_util::Platform,
Windows,
MacOS,
Linux,
Android,
IOS,
symbols:
Windows => "windows",
MacOS => "macos",
Linux => "linux",
Android => "android",
IOS => "ios",
);

/// A struct to represent the `EmulationOption` class.
#[derive(Clone)]
#[magnus::wrap(class = "Wreq::Emulation", free_immediately, size)]
pub struct Emulation(pub wreq_util::Emulation);

// ===== impl Profile =====

impl Profile {
pub fn to_s(&self) -> String {
self.into_ffi().inspect()
}
}

// ===== impl Platform =====

impl Platform {
pub fn to_s(&self) -> String {
self.into_ffi().inspect()
}

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

// ===== impl Emulation =====

impl Emulation {
Expand Down
80 changes: 22 additions & 58 deletions src/http.rs
Original file line number Diff line number Diff line change
@@ -1,81 +1,45 @@
use magnus::{Error, Module, RModule, Ruby, TryConvert, Value, method, typed_data::Inspect};
use magnus::{Error, Module, RModule, Ruby, TryConvert, Value, method};

// Defines constant registration, `into_ffi`/`from_ffi`, and handlers for
// Ruby's `to_s`, `==`, `eql?`, and `hash` methods.
// `wreq::Version` only exposes protocol strings through `Debug`, so static
// labels avoid allocating an intermediate `String`.
define_ruby_enum!(
/// An HTTP version.
const,
Version,
"Wreq::Version",
wreq::Version,
HTTP_09,
HTTP_10,
HTTP_11,
HTTP_2,
HTTP_3,
strings:
HTTP_09 => "HTTP/0.9",
HTTP_10 => "HTTP/1.0",
HTTP_11 => "HTTP/1.1",
HTTP_2 => "HTTP/2.0",
HTTP_3 => "HTTP/3.0",
);

// Defines constant registration, `into_ffi`/`from_ffi`, and handlers for
// Ruby's `to_s`, `to_sym`, `==`, `eql?`, and `hash` methods.
define_ruby_enum!(
/// An HTTP method.
Method,
"Wreq::Method",
wreq::Method,
GET,
HEAD,
POST,
PUT,
DELETE,
OPTIONS,
TRACE,
PATCH,
symbols:
GET => "get",
HEAD => "head",
POST => "post",
PUT => "put",
DELETE => "delete",
OPTIONS => "options",
TRACE => "trace",
PATCH => "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(ruby: &Ruby, rb_self: &Self) -> magnus::Symbol {
let name = match *rb_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)]
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 TryConvert for Version {
fn try_convert(value: magnus::Value) -> Result<Self, magnus::Error> {
<&Version>::try_convert(value).cloned()
Expand Down
Loading