Class: BrowserMob::Proxy::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/browsermob/proxy/server.rb

Defined Under Namespace

Classes: ServerDiedError, TimeoutError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, opts = {}) ⇒ Server

Create a new server instance

Parameters:

  • path (String)

    Path to the BrowserMob Proxy server executable

  • opts (Hash) (defaults to: {})

    options to create the server with

Options Hash (opts):

  • port (Integer)

    What port to start the server on

  • log (Boolean)

    Show server output (server inherits stdout/stderr)

  • timeout (Integer)

    Seconds to wait for server to launch before timing out.



20
21
22
23
24
25
26
27
28
29
# File 'lib/browsermob/proxy/server.rb', line 20

def initialize(path, opts = {})
  assert_executable path

  @path    = path
  @port    = Integer(opts[:port] || 8080)
  @timeout = Integer(opts[:timeout] || 10)
  @log     = !!opts[:log]

  @process = create_process
end

Instance Attribute Details

#portObject (readonly)

Returns the value of attribute port.



8
9
10
# File 'lib/browsermob/proxy/server.rb', line 8

def port
  @port
end

Instance Method Details

#create_proxy(port = nil) ⇒ Object



46
47
48
# File 'lib/browsermob/proxy/server.rb', line 46

def create_proxy(port = nil)
  Client.from url, port
end

#startObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/browsermob/proxy/server.rb', line 31

def start
  @process.start

  wait_for_startup

  pid = Process.pid
  at_exit { stop if Process.pid == pid }

  self
end

#stopObject



50
51
52
# File 'lib/browsermob/proxy/server.rb', line 50

def stop
  @process.stop if @process.alive?
end

#urlObject



42
43
44
# File 'lib/browsermob/proxy/server.rb', line 42

def url
  "http://localhost:#{port}"
end