Skip to content

Commit

Permalink
Added common RequestParams for both Request and `InterceptedReque…
Browse files Browse the repository at this point in the history
…st`. (#384)
  • Loading branch information
postmodern authored Aug 11, 2023
1 parent 8313a9c commit 6c989a8
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 59 deletions.
15 changes: 3 additions & 12 deletions lib/ferrum/network/intercepted_request.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# frozen_string_literal: true

require "ferrum/network/request_params"
require "base64"

module Ferrum
class Network
class InterceptedRequest
include RequestParams

attr_accessor :request_id, :frame_id, :resource_type, :network_id, :status

def initialize(page, params)
Expand Down Expand Up @@ -54,18 +57,6 @@ def abort
@page.command("Fetch.failRequest", requestId: request_id, errorReason: "BlockedByClient")
end

def url
@request["url"]
end

def method
@request["method"]
end

def headers
@request["headers"]
end

def initial_priority
@request["initialPriority"]
end
Expand Down
50 changes: 3 additions & 47 deletions lib/ferrum/network/request.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require "ferrum/network/request_params"
require "time"

module Ferrum
Expand All @@ -9,6 +10,8 @@ class Network
# object.
#
class Request
include RequestParams

#
# Initializes the request object.
#
Expand Down Expand Up @@ -68,42 +71,6 @@ def frame_id
@params["frameId"]
end

#
# The URL for the request.
#
# @return [String]
#
def url
@request["url"]
end

#
# The URL fragment for the request.
#
# @return [String, nil]
#
def url_fragment
@request["urlFragment"]
end

#
# The request method.
#
# @return [String]
#
def method
@request["method"]
end

#
# The request headers.
#
# @return [Hash{String => String}]
#
def headers
@request["headers"]
end

#
# The request timestamp.
#
Expand All @@ -113,17 +80,6 @@ def time
@time ||= Time.strptime(@params["wallTime"].to_s, "%s")
end

#
# The optional HTTP `POST` form data.
#
# @return [String, nil]
# The HTTP `POST` form data.
#
def post_data
@request["postData"]
end
alias body post_data

#
# Converts the request to a Hash.
#
Expand Down
57 changes: 57 additions & 0 deletions lib/ferrum/network/request_params.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

module Ferrum
class Network
#
# Common methods used by both {Request} and {InterceptedRequest}.
#
module RequestParams
#
# The URL for the request.
#
# @return [String]
#
def url
@request["url"]
end

#
# The URL fragment for the request.
#
# @return [String, nil]
#
def url_fragment
@request["urlFragment"]
end

#
# The request method.
#
# @return [String]
#
def method
@request["method"]
end

#
# The request headers.
#
# @return [Hash{String => String}]
#
def headers
@request["headers"]
end

#
# The optional HTTP `POST` form data.
#
# @return [String, nil]
# The HTTP `POST` form data.
#
def post_data
@request["postData"]
end
alias body post_data
end
end
end

0 comments on commit 6c989a8

Please sign in to comment.