Module: Unmagic::Browser::Driver

Defined in:
lib/unmagic/browser/driver.rb,
lib/unmagic/browser/driver/base.rb,
lib/unmagic/browser/driver/local.rb,
lib/unmagic/browser/driver/remote.rb,
lib/unmagic/browser/driver/cloudflare.rb,
lib/unmagic/browser/driver/connection.rb,
lib/unmagic/browser/driver/cloudflare/transport.rb

Overview

The three ways to get a browser: one on this machine, one somewhere else you already run, or Cloudflare's.

They're interchangeable — the same Session verbs run against all three — so picking one is the only decision, and it's made once, when you construct the browser.

Defined Under Namespace

Classes: Base, Cloudflare, Connection, Local, Remote

Constant Summary collapse

REGISTRY =

What a bare symbol means.

{
  local: "Unmagic::Browser::Driver::Local",
  remote: "Unmagic::Browser::Driver::Remote",
  cloudflare: "Unmagic::Browser::Driver::Cloudflare",
}.freeze

Class Method Summary collapse

Class Method Details

.build(value) ⇒ Base

Coerce whatever a caller passed as driver: into a driver.

A symbol builds the driver with its defaults; anything else is taken as a driver already, which is how a driver gets options — and how somebody's own driver gets used without inheriting from Base.

Parameters:

  • value (Symbol, String, Base)

    :local, :remote, :cloudflare, or an instance

Returns:

Raises:



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/unmagic/browser/driver.rb', line 29

def build(value)
  return value unless value.is_a?(Symbol) || value.is_a?(String)

  name = value.to_sym
  class_name = REGISTRY[name]
  unless class_name
    known = REGISTRY.keys.map(&:inspect).join(", ")
    raise Error::Misconfigured, "Unknown driver #{value.inspect}. Known drivers are #{known}."
  end

  Object.const_get(class_name).new
end

.close_at_exit(driver) ⇒ void

This method returns an undefined value.

Remember a driver so its shared one-off connection is torn down when the process ends. Registering is idempotent, and the hook is installed only once a driver has actually opened a browser — a program that only ever talks to Cloudflare's REST API never installs one.

Parameters:



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/unmagic/browser/driver.rb', line 49

def close_at_exit(driver)
  @registry_mutex ||= Mutex.new
  @registry_mutex.synchronize do
    @registry ||= []
    return if @registry.include?(driver)

    install_at_exit_hook unless @at_exit_installed
    @at_exit_installed = true
    @registry << driver
  end
end