Class: Saper::Runtime

Inherits:
Object
  • Object
show all
Defined in:
lib/saper/core/runtime.rb

Overview

Runtime provides a set of auxiliary functions to Saper actions: browser requests, keychain access, storage of variables.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(variables = {}, options = {}) ⇒ Saper::Runtime

Returns a new Saper::Runtime instance.

Parameters:

  • variables (Hash) (defaults to: {})
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :browser (Browser)

    Browser instance

  • :keychain (String, Keychain)

    Keychain file or instance

  • :agent (String, Symbol)

    User-Agent type or string


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/saper/core/runtime.rb', line 28

def initialize(variables = {}, options = {})
  unless options.is_a?(Hash)
    options = {}
  end
  if options[:logger].is_a?(Logger)
    @logger = options[:logger]
  else
    @logger = Logger.new
  end
  if options[:browser].is_a?(Browser)
    @browser = options[:browser]
  else
    @browser = Browser.new(:agent => options[:agent], :proxy => options[:proxy], :logger => logger)
  end
  if options[:keychain].is_a?(Keychain)
    @keychain = options[:keychain]
  end
  if options[:keychain].is_a?(String)
    @keychain = Keychain.load(options[:keychain], :logger => logger)
  end
  unless @keychain.is_a?(Keychain)
    @keychain = Keychain.new({}, :logger => logger)
  end
  @variables = variables
  if block_given?
    yield self
  end
end

Instance Attribute Details

#browserSaper::Browser (readonly)

Returns Saper::Browser instance used by runtime.

Returns:


8
9
10
# File 'lib/saper/core/runtime.rb', line 8

def browser
  @browser
end

#keychainSaper::Keychain (readonly)

Returns Saper::Keychain instance used by runtime.

Returns:


12
13
14
# File 'lib/saper/core/runtime.rb', line 12

def keychain
  @keychain
end

#loggerObject (readonly)

TODO:

19
20
21
# File 'lib/saper/core/runtime.rb', line 19

def logger
  @logger
end

#variablesHash (readonly)

Returns a Hash of variables stored by runtime.

Returns:

  • (Hash)

16
17
18
# File 'lib/saper/core/runtime.rb', line 16

def variables
  @variables
end

Instance Method Details

#[](name) ⇒ Object

Get variable value

Parameters:

  • name (Symbol)

Returns:

  • (Object)

78
79
80
# File 'lib/saper/core/runtime.rb', line 78

def [](name)
  @variables[name]
end

#[]=(name, value) ⇒ Object

Set variable value

Parameters:

  • name (Symbol)
  • value (Object)

Returns:

  • (Object)

86
87
88
# File 'lib/saper/core/runtime.rb', line 86

def []=(name, value)
  @variables[name] = value
end

#bytes_receivedInteger

Returns volume of incoming traffic (in bytes)

Returns:

  • (Integer)

59
60
61
# File 'lib/saper/core/runtime.rb', line 59

def bytes_received
  browser.received
end

#bytes_sentInteger

Returns volume of outgoing traffic (in bytes)

Returns:

  • (Integer)

65
66
67
# File 'lib/saper/core/runtime.rb', line 65

def bytes_sent
  browser.sent
end

#copySaper::Runtime

Returns a duplicate instance of self.

Returns:


99
100
101
# File 'lib/saper/core/runtime.rb', line 99

def copy
  Runtime.new(variables.dup, :browser => browser, :keychain => keychain)
end

#copy_without_variablesSaper::Runtime

Returns a duplicate instance of self without the stored variables.

Returns:


105
106
107
# File 'lib/saper/core/runtime.rb', line 105

def copy_without_variables
  Runtime.new({}, :browser => browser, :keychain => keychain)
end

#credentials(service) ⇒ Object?

Returns access credentials for specified service

Parameters:

  • service (Symbol)

Returns:

  • (Object, nil)

93
94
95
# File 'lib/saper/core/runtime.rb', line 93

def credentials(service)
  keychain[service]
end

#http_requestsInteger

Returns number of HTTP requests

Returns:

  • (Integer)

71
72
73
# File 'lib/saper/core/runtime.rb', line 71

def http_requests
  browser.requests
end