# frozen_string_literal: true module Wayfarer module Networking class Ferrum def self.renew_on [::Ferrum::DeadBrowserError] end attr_reader :browser def initialize @browser = instantiate_browser end def fetch(url) browser.goto(url) Result::Success.new(live(nil)) end def live(_) Wayfarer::Page.new(url: browser.current_url, body: body, status_code: browser.network.response.status, headers: browser.network.response.headers) 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 end end end end