lib/wayfarer/networking/ferrum.rb in wayfarer-0.4.0 vs lib/wayfarer/networking/ferrum.rb in wayfarer-0.4.1
- old
+ new
@@ -1,70 +1,35 @@
# frozen_string_literal: true
module Wayfarer
module Networking
class Ferrum
- def self.renew_on
+ include Strategy
+
+ def renew_on
[::Ferrum::DeadBrowserError]
end
- attr_reader :browser
-
- def initialize
- @browser = instantiate_browser
+ def create
+ ::Ferrum::Browser.new(Wayfarer.config.ferrum.options).tap do |browser|
+ browser.headers.set(Wayfarer.config.network.http_headers)
+ end
end
- def fetch(url)
- browser.goto(url)
- Result::Success.new(live(nil))
+ def destroy(instance)
+ instance.reset
+ instance.quit
end
- def live(_)
- Wayfarer::Page.new(url: browser.current_url,
- body: body,
- status_code: browser.network.response.status,
- headers: browser.network.response.headers)
+ def navigate(instance, url)
+ instance.goto(url)
end
- def body
- browser.body
- end
-
- def renew
- free
- @browser = instantiate_browser
- end
-
- def free
- browser&.reset
- browser&.quit
- @browser = nil
- @capybara = nil
- end
-
- def capybara
- @capybara ||= instantiate_capybara_driver
- end
-
- private
-
- def instantiate_browser
- ::Ferrum::Browser.new(Wayfarer.config.ferrum_options).tap do |browser|
- browser.headers.set(Wayfarer.config.http_headers)
- end
- end
-
- def instantiate_capybara_driver
- Capybara.run_server = false
- Capybara.current_driver = :cuprite
-
- capybara_driver = Capybara::Cuprite::Driver.new(nil)
- capybara_driver.instance_variable_set(:@capybara, browser)
-
- session = Capybara::Session.new(:cuprite, nil)
- session.instance_variable_set(:@driver, capybara_driver)
-
- session
+ def live(instance)
+ success(url: instance.current_url,
+ body: instance.body,
+ status_code: instance.network.response.status,
+ headers: instance.network.response.headers)
end
end
end
end