Class: Btape::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/btape/runner.rb

Overview

The recording session around a script: opens the browser, starts the recorder, hands the commands to Executor, and turns the captured frames into a GIF.

Defined Under Namespace

Classes: Context

Constant Summary collapse

DEFAULT_VIEWPORT =
[1280, 720].freeze
CLEANUP_TIMEOUT =

How long unwinding gets. Set Timeout bounds the run, but the thing that usually trips it is a wedged browser, and both stopping the recorder and quitting the browser then talk to it — so without a deadline of their own they would hang the process after the timeout had already fired.

10

Instance Method Summary collapse

Constructor Details

#initialize(browser_factory: lambda { |options| Ferrum::Browser.new(**options) }, recorder_class: Recorder, gif_encoder: nil, logger: NullLogger.new) ⇒ Runner

An encoder passed here is used as given; otherwise one is built from the settings the tape and the caller supplied.



29
30
31
32
33
34
35
36
# File 'lib/btape/runner.rb', line 29

def initialize(browser_factory: lambda { |options|
  Ferrum::Browser.new(**options)
}, recorder_class: Recorder, gif_encoder: nil, logger: NullLogger.new)
  @browser_factory = browser_factory
  @recorder_class = recorder_class
  @gif_encoder = gif_encoder
  @logger = logger
end

Instance Method Details

#run(commands, base_directory: Dir.pwd, settings: {}, frames_directory: nil, on_frame: nil, output: nil) ⇒ Object

Returns a Result. Pass frames_directory: to keep the PNG frames after the run, on_frame: to be handed each one as it is captured, and output: to override the tape's Output with another path or with an IO to write the GIF into.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/btape/runner.rb', line 42

def run(commands, base_directory: Dir.pwd, settings: {}, frames_directory: nil, on_frame: nil, output: nil)
  settings = Settings.from_commands(commands).merge(settings)
  sink, output_path = resolve_output(commands, base_directory, output)
  context = Context.new(
    commands: commands,
    settings: settings,
    geometry: resolve_viewport(commands),
    sink: sink,
    output_path: output_path,
    on_frame: on_frame,
    keep_frames: !frames_directory.nil?
  )

  within_frames_directory(frames_directory, base_directory) do |directory|
    context.directory = directory
    record(context)
  end
end