Class: Flunky::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/flunky/session.rb

Overview

Owns one driver for the life of a browsing session, caches the latest snapshot, and hands out the action DSL. This is the object callers hold.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(driver: nil, **opts) ⇒ Session

Per-session options override the global configuration. A custom backend can be injected with driver:; otherwise we build a Ferrum driver.



11
12
13
14
15
16
# File 'lib/flunky/session.rb', line 11

def initialize(driver: nil, **opts)
  @options = Flunky.configuration.to_h.merge(opts)
  @driver = driver || build_default_driver
  @driver.start
  @snapshot = nil
end

Instance Attribute Details

#driverObject (readonly)

Returns the value of attribute driver.



7
8
9
# File 'lib/flunky/session.rb', line 7

def driver
  @driver
end

Instance Method Details

#actionsObject



34
35
36
# File 'lib/flunky/session.rb', line 34

def actions
  @actions ||= Actions.new(@driver, self)
end

#closeObject



38
39
40
# File 'lib/flunky/session.rb', line 38

def close
  @driver.quit
end

#observeObject

Re-read the page and cache the fresh snapshot.



25
26
27
# File 'lib/flunky/session.rb', line 25

def observe
  @snapshot = Snapshot.capture(@driver, max_elements: @options[:max_elements])
end

#snapshotObject

Memoized view; captures once on first use so refs exist before any action.



30
31
32
# File 'lib/flunky/session.rb', line 30

def snapshot
  @snapshot ||= observe
end

#visit(url) ⇒ Object



18
19
20
21
22
# File 'lib/flunky/session.rb', line 18

def visit(url)
  @driver.go_to(url)
  @snapshot = nil
  self
end