Skip to content

Commit 6c989a8

Browse files
authored
Added common RequestParams for both Request and InterceptedRequest. (#384)
1 parent 8313a9c commit 6c989a8

File tree

3 files changed

+63
-59
lines changed

3 files changed

+63
-59
lines changed

lib/ferrum/network/intercepted_request.rb

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
# frozen_string_literal: true
22

3+
require "ferrum/network/request_params"
34
require "base64"
45

56
module Ferrum
67
class Network
78
class InterceptedRequest
9+
include RequestParams
10+
811
attr_accessor :request_id, :frame_id, :resource_type, :network_id, :status
912

1013
def initialize(page, params)
@@ -54,18 +57,6 @@ def abort
5457
@page.command("Fetch.failRequest", requestId: request_id, errorReason: "BlockedByClient")
5558
end
5659

57-
def url
58-
@request["url"]
59-
end
60-
61-
def method
62-
@request["method"]
63-
end
64-
65-
def headers
66-
@request["headers"]
67-
end
68-
6960
def initial_priority
7061
@request["initialPriority"]
7162
end

lib/ferrum/network/request.rb

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# frozen_string_literal: true
22

3+
require "ferrum/network/request_params"
34
require "time"
45

56
module Ferrum
@@ -9,6 +10,8 @@ class Network
910
# object.
1011
#
1112
class Request
13+
include RequestParams
14+
1215
#
1316
# Initializes the request object.
1417
#
@@ -68,42 +71,6 @@ def frame_id
6871
@params["frameId"]
6972
end
7073

71-
#
72-
# The URL for the request.
73-
#
74-
# @return [String]
75-
#
76-
def url
77-
@request["url"]
78-
end
79-
80-
#
81-
# The URL fragment for the request.
82-
#
83-
# @return [String, nil]
84-
#
85-
def url_fragment
86-
@request["urlFragment"]
87-
end
88-
89-
#
90-
# The request method.
91-
#
92-
# @return [String]
93-
#
94-
def method
95-
@request["method"]
96-
end
97-
98-
#
99-
# The request headers.
100-
#
101-
# @return [Hash{String => String}]
102-
#
103-
def headers
104-
@request["headers"]
105-
end
106-
10774
#
10875
# The request timestamp.
10976
#
@@ -113,17 +80,6 @@ def time
11380
@time ||= Time.strptime(@params["wallTime"].to_s, "%s")
11481
end
11582

116-
#
117-
# The optional HTTP `POST` form data.
118-
#
119-
# @return [String, nil]
120-
# The HTTP `POST` form data.
121-
#
122-
def post_data
123-
@request["postData"]
124-
end
125-
alias body post_data
126-
12783
#
12884
# Converts the request to a Hash.
12985
#
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# frozen_string_literal: true
2+
3+
module Ferrum
4+
class Network
5+
#
6+
# Common methods used by both {Request} and {InterceptedRequest}.
7+
#
8+
module RequestParams
9+
#
10+
# The URL for the request.
11+
#
12+
# @return [String]
13+
#
14+
def url
15+
@request["url"]
16+
end
17+
18+
#
19+
# The URL fragment for the request.
20+
#
21+
# @return [String, nil]
22+
#
23+
def url_fragment
24+
@request["urlFragment"]
25+
end
26+
27+
#
28+
# The request method.
29+
#
30+
# @return [String]
31+
#
32+
def method
33+
@request["method"]
34+
end
35+
36+
#
37+
# The request headers.
38+
#
39+
# @return [Hash{String => String}]
40+
#
41+
def headers
42+
@request["headers"]
43+
end
44+
45+
#
46+
# The optional HTTP `POST` form data.
47+
#
48+
# @return [String, nil]
49+
# The HTTP `POST` form data.
50+
#
51+
def post_data
52+
@request["postData"]
53+
end
54+
alias body post_data
55+
end
56+
end
57+
end

0 commit comments

Comments
 (0)