forked from rubycdp/ferrum
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Split methods into Utils
- Loading branch information
Showing
18 changed files
with
224 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,161 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
require "concurrent-ruby" | ||
require "ferrum/utils/platform" | ||
require "ferrum/utils/elapsed_time" | ||
require "ferrum/utils/attempt" | ||
require "ferrum/errors" | ||
require "ferrum/browser" | ||
require "ferrum/node" | ||
|
||
module Ferrum | ||
class Error < StandardError; end | ||
|
||
class NoSuchPageError < Error; end | ||
|
||
class NoSuchTargetError < Error; end | ||
|
||
class NotImplementedError < Error; end | ||
|
||
class StatusError < Error | ||
def initialize(url, message = nil) | ||
super(message || "Request to #{url} failed to reach server, check DNS and server status") | ||
end | ||
end | ||
|
||
class PendingConnectionsError < StatusError | ||
attr_reader :pendings | ||
|
||
def initialize(url, pendings = []) | ||
@pendings = pendings | ||
|
||
message = "Request to #{url} reached server, but there are still pending connections: #{pendings.join(', ')}" | ||
|
||
super(url, message) | ||
end | ||
end | ||
|
||
class TimeoutError < Error | ||
def message | ||
"Timed out waiting for response. It's possible that this happened " \ | ||
"because something took a very long time (for example a page load " \ | ||
"was slow). If so, setting the :timeout option to a higher value might " \ | ||
"help." | ||
end | ||
end | ||
|
||
class ScriptTimeoutError < Error | ||
def message | ||
"Timed out waiting for evaluated script to return a value" | ||
end | ||
end | ||
|
||
class ProcessTimeoutError < Error | ||
attr_reader :output | ||
|
||
def initialize(timeout, output) | ||
@output = output | ||
super("Browser did not produce websocket url within #{timeout} seconds, try to increase `:process_timeout`. See https://github.com/rubycdp/ferrum#customization") | ||
end | ||
end | ||
|
||
class DeadBrowserError < Error | ||
def initialize(message = "Browser is dead or given window is closed") | ||
super | ||
end | ||
end | ||
|
||
class NodeMovingError < Error | ||
def initialize(node, prev, current) | ||
@node = node | ||
@prev = prev | ||
@current = current | ||
super(message) | ||
end | ||
|
||
def message | ||
"#{@node.inspect} that you're trying to click is moving, hence " \ | ||
"we cannot. Previously it was at #{@prev.inspect} but now at " \ | ||
"#{@current.inspect}." | ||
end | ||
end | ||
|
||
class CoordinatesNotFoundError < Error | ||
def initialize(message = "Could not compute content quads") | ||
super | ||
end | ||
end | ||
|
||
class BrowserError < Error | ||
attr_reader :response | ||
|
||
def initialize(response) | ||
@response = response | ||
super(response["message"]) | ||
end | ||
|
||
def code | ||
response["code"] | ||
end | ||
|
||
def data | ||
response["data"] | ||
end | ||
end | ||
|
||
class NodeNotFoundError < BrowserError; end | ||
|
||
class NoExecutionContextError < BrowserError | ||
def initialize(response = nil) | ||
response ||= { "message" => "There's no context available" } | ||
super(response) | ||
end | ||
end | ||
|
||
class JavaScriptError < BrowserError | ||
attr_reader :class_name, :message, :stack_trace | ||
|
||
def initialize(response, stack_trace = nil) | ||
@class_name, @message = response.values_at("className", "description") | ||
@stack_trace = stack_trace | ||
super(response.merge("message" => @message)) | ||
end | ||
end | ||
|
||
class << self | ||
def windows? | ||
RbConfig::CONFIG["host_os"] =~ /mingw|mswin|cygwin/ | ||
end | ||
|
||
def mac? | ||
RbConfig::CONFIG["host_os"] =~ /darwin/ | ||
end | ||
|
||
def mri? | ||
defined?(RUBY_ENGINE) && RUBY_ENGINE == "ruby" | ||
end | ||
|
||
def started | ||
@started ||= monotonic_time | ||
end | ||
|
||
def elapsed_time(start = nil) | ||
monotonic_time - (start || @started) | ||
end | ||
|
||
def monotonic_time | ||
Concurrent.monotonic_time | ||
end | ||
|
||
def timeout?(start, timeout) | ||
elapsed_time(start) > timeout | ||
end | ||
|
||
def with_attempts(errors:, max:, wait:) | ||
attempts ||= 1 | ||
yield | ||
rescue *Array(errors) | ||
raise if attempts >= max | ||
|
||
attempts += 1 | ||
sleep(wait) | ||
retry | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
# frozen_string_literal: true | ||
|
||
module Ferrum | ||
class Error < StandardError; end | ||
|
||
class NoSuchPageError < Error; end | ||
|
||
class NoSuchTargetError < Error; end | ||
|
||
class NotImplementedError < Error; end | ||
|
||
class StatusError < Error | ||
def initialize(url, message = nil) | ||
super(message || "Request to #{url} failed to reach server, check DNS and server status") | ||
end | ||
end | ||
|
||
class PendingConnectionsError < StatusError | ||
attr_reader :pendings | ||
|
||
def initialize(url, pendings = []) | ||
@pendings = pendings | ||
|
||
message = "Request to #{url} reached server, but there are still pending connections: #{pendings.join(', ')}" | ||
|
||
super(url, message) | ||
end | ||
end | ||
|
||
class TimeoutError < Error | ||
def message | ||
"Timed out waiting for response. It's possible that this happened " \ | ||
"because something took a very long time (for example a page load " \ | ||
"was slow). If so, setting the :timeout option to a higher value might " \ | ||
"help." | ||
end | ||
end | ||
|
||
class ScriptTimeoutError < Error | ||
def message | ||
"Timed out waiting for evaluated script to return a value" | ||
end | ||
end | ||
|
||
class ProcessTimeoutError < Error | ||
attr_reader :output | ||
|
||
def initialize(timeout, output) | ||
@output = output | ||
super("Browser did not produce websocket url within #{timeout} seconds, try to increase `:process_timeout`. See https://github.com/rubycdp/ferrum#customization") | ||
end | ||
end | ||
|
||
class DeadBrowserError < Error | ||
def initialize(message = "Browser is dead or given window is closed") | ||
super | ||
end | ||
end | ||
|
||
class NodeMovingError < Error | ||
def initialize(node, prev, current) | ||
@node = node | ||
@prev = prev | ||
@current = current | ||
super(message) | ||
end | ||
|
||
def message | ||
"#{@node.inspect} that you're trying to click is moving, hence " \ | ||
"we cannot. Previously it was at #{@prev.inspect} but now at " \ | ||
"#{@current.inspect}." | ||
end | ||
end | ||
|
||
class CoordinatesNotFoundError < Error | ||
def initialize(message = "Could not compute content quads") | ||
super | ||
end | ||
end | ||
|
||
class BrowserError < Error | ||
attr_reader :response | ||
|
||
def initialize(response) | ||
@response = response | ||
super(response["message"]) | ||
end | ||
|
||
def code | ||
response["code"] | ||
end | ||
|
||
def data | ||
response["data"] | ||
end | ||
end | ||
|
||
class NodeNotFoundError < BrowserError; end | ||
|
||
class NoExecutionContextError < BrowserError | ||
def initialize(response = nil) | ||
response ||= { "message" => "There's no context available" } | ||
super(response) | ||
end | ||
end | ||
|
||
class JavaScriptError < BrowserError | ||
attr_reader :class_name, :message, :stack_trace | ||
|
||
def initialize(response, stack_trace = nil) | ||
@class_name, @message = response.values_at("className", "description") | ||
@stack_trace = stack_trace | ||
super(response.merge("message" => @message)) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.