Skip to content

Commit

Permalink
Refactor code to fix rubocop warns
Browse files Browse the repository at this point in the history
  • Loading branch information
route committed Nov 5, 2021
1 parent 07d9168 commit 1aaa1c9
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ Style/MultilineBlockChain:
Metrics/ModuleLength:
Exclude:
- spec/**/*

Metrics/ParameterLists:
Max: 6
58 changes: 36 additions & 22 deletions lib/ferrum/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,34 @@ def authorize(user:, password:, type: :server, &block)
end

def subscribe
subscribe_request_will_be_sent
subscribe_response_received
subscribe_loading_finished
subscribe_loading_failed
subscribe_log_entry_added
end

def authorized_response(ids, request_id, username, password)
if ids.include?(request_id)
{ response: "CancelAuth" }
elsif username && password
{ response: "ProvideCredentials",
username: username,
password: password }
end
end

def select(request_id)
@traffic.select { |e| e.id == request_id }
end

def build_exchange(id)
Network::Exchange.new(@page, id).tap { |e| @traffic << e }
end

private

def subscribe_request_will_be_sent
@page.on("Network.requestWillBeSent") do |params|
request = Network::Request.new(params)

Expand All @@ -151,7 +179,9 @@ def subscribe

@exchange = exchange if exchange.navigation_request?(@page.main_frame.id)
end
end

def subscribe_response_received
@page.on("Network.responseReceived") do |params|
exchange = select(params["requestId"]).last

Expand All @@ -160,12 +190,16 @@ def subscribe
exchange.response = response
end
end
end

def subscribe_loading_finished
@page.on("Network.loadingFinished") do |params|
exchange = select(params["requestId"]).last
exchange.response.body_size = params["encodedDataLength"] if exchange&.response
end
end

def subscribe_loading_failed
@page.on("Network.loadingFailed") do |params|
exchange = select(params["requestId"]).last
exchange.error ||= Network::Error.new
Expand All @@ -176,7 +210,9 @@ def subscribe
exchange.error.monotonic_time = params["timestamp"]
exchange.error.canceled = params["canceled"]
end
end

def subscribe_log_entry_added
@page.on("Log.entryAdded") do |params|
entry = params["entry"] || {}
if entry["source"] == "network" && entry["level"] == "error"
Expand All @@ -191,28 +227,6 @@ def subscribe
end
end

def authorized_response(ids, request_id, username, password)
if ids.include?(request_id)
{ response: "CancelAuth" }
elsif username && password
{ response: "ProvideCredentials",
username: username,
password: password }
else
{ response: "CancelAuth" }
end
end

def select(request_id)
@traffic.select { |e| e.id == request_id }
end

def build_exchange(id)
Network::Exchange.new(@page, id).tap { |e| @traffic << e }
end

private

def blacklist_subscribe
return unless blacklist?
raise ArgumentError, "You can't use blacklist along with whitelist" if whitelist?
Expand Down
2 changes: 2 additions & 0 deletions lib/ferrum/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,11 @@ def window_id
@browser.command("Browser.getWindowForTarget", targetId: @target_id)["windowId"]
end

# rubocop:disable Naming/AccessorMethodName
def set_window_bounds(bounds = {})
@browser.command("Browser.setWindowBounds", windowId: window_id, bounds: bounds)
end
# rubocop:enable Naming/AccessorMethodName

def command(method, wait: 0, slowmoable: false, **params)
iteration = @event.reset if wait.positive?
Expand Down
35 changes: 33 additions & 2 deletions lib/ferrum/page/frames.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,47 @@ def frame_by(id: nil, name: nil, execution_id: nil)
end

def frames_subscribe
subscribe_frame_attached
subscribe_frame_started_loading
subscribe_frame_navigated
subscribe_frame_stopped_loading

subscribe_navigated_within_document

subscribe_request_will_be_sent

subscribe_execution_context_created
subscribe_execution_context_destroyed
subscribe_execution_contexts_cleared
end

private

def subscribe_frame_attached
on("Page.frameAttached") do |params|
parent_frame_id, frame_id = params.values_at("parentFrameId", "frameId")
@frames[frame_id] = Frame.new(frame_id, self, parent_frame_id)
end
end

def subscribe_frame_started_loading
on("Page.frameStartedLoading") do |params|
frame = @frames[params["frameId"]]
frame.state = :started_loading
@event.reset
end
end

def subscribe_frame_navigated
on("Page.frameNavigated") do |params|
frame_id, name = params["frame"]&.values_at("id", "name")
frame = @frames[frame_id]
frame.state = :navigated
frame.name = name unless name.to_s.empty?
end
end

def subscribe_frame_stopped_loading
on("Page.frameStoppedLoading") do |params|
# `DOM.performSearch` doesn't work without getting #document node first.
# It returns node with nodeId 1 and nodeType 9 from which descend the
Expand All @@ -57,19 +80,25 @@ def frames_subscribe

@event.set if idling?
end
end

def subscribe_navigated_within_document
on("Page.navigatedWithinDocument") do
@event.set if idling?
end
end

def subscribe_request_will_be_sent
on("Network.requestWillBeSent") do |params|
# Possible types:
# Document, Stylesheet, Image, Media, Font, Script, TextTrack, XHR,
# Fetch, EventSource, WebSocket, Manifest, SignedExchange, Ping,
# CSPViolationReport, Other
@event.reset if params["frameId"] == @main_frame.id && params["type"] == "Document"
end
end

def subscribe_execution_context_created
on("Runtime.executionContextCreated") do |params|
context_id = params.dig("context", "id")
frame_id = params.dig("context", "auxData", "frameId")
Expand All @@ -87,21 +116,23 @@ def frames_subscribe

@frames[frame_id] ||= frame
end
end

def subscribe_execution_context_destroyed
on("Runtime.executionContextDestroyed") do |params|
execution_id = params["executionContextId"]
frame = frame_by(execution_id: execution_id)
frame&.execution_id = nil
end
end

def subscribe_execution_contexts_cleared
on("Runtime.executionContextsCleared") do
@frames.delete_if { |_, f| !f.main? }
@main_frame.execution_id = nil
end
end

private

def idling?
@frames.all? { |_, f| f.state == :stopped_loading }
end
Expand Down

0 comments on commit 1aaa1c9

Please sign in to comment.