Class: Isomorfeus::Puppetmaster::Session
- Inherits:
-
Object
- Object
- Isomorfeus::Puppetmaster::Session
- Defined in:
- lib/isomorfeus/puppetmaster/session.rb
Constant Summary collapse
- TIMEOUT =
milliseconds
30000
Instance Attribute Summary collapse
-
#app ⇒ Object
Returns the value of attribute app.
-
#browser ⇒ Object
Returns the value of attribute browser.
-
#default_page ⇒ Object
Returns the value of attribute default_page.
-
#product ⇒ Object
Returns the value of attribute product.
-
#response ⇒ Object
Returns the value of attribute response.
Instance Method Summary collapse
- #close ⇒ Object
- #device_names ⇒ Object
- #devices ⇒ Object
- #eval(j, *args) ⇒ Object
- #eval_ruby(ruby_source = '', &block) ⇒ Object
- #eval_with_opal(ruby_source = '', &block) ⇒ Object
- #evaluate(script, *args) ⇒ Object
- #goto(uri, referer: nil, timeout: nil, wait_until: 'networkidle0') ⇒ Object (also: #visit)
-
#initialize(app:, args: nil, channel: nil, default_viewport: nil, devtools: nil, dumpio: nil, env: nil, executable_path: nil, handle_SIGINT: nil, handle_SIGTERM: nil, handle_SIGHUP: nil, headless: true, ignore_default_args: nil, ignore_https_errors: nil, pipe: nil, product: :chrome, slow_mo: nil, timeout: nil, user_data_dir: nil) ⇒ Session
constructor
A new instance of Session.
- #isomorphic(ruby_source = '', &block) ⇒ Object
- #isomorphic_with_opal(ruby_source = '', &block) ⇒ Object
- #network_conditions ⇒ Object
- #network_conition_names ⇒ Object
- #new_page(uri = nil, referer: nil, timeout: nil, wait_until: nil) ⇒ Object
- #pages ⇒ Object
- #parse_uri(uri) ⇒ Object
- #user_agent ⇒ Object
Constructor Details
#initialize(app:, args: nil, channel: nil, default_viewport: nil, devtools: nil, dumpio: nil, env: nil, executable_path: nil, handle_SIGINT: nil, handle_SIGTERM: nil, handle_SIGHUP: nil, headless: true, ignore_default_args: nil, ignore_https_errors: nil, pipe: nil, product: :chrome, slow_mo: nil, timeout: nil, user_data_dir: nil) ⇒ Session
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/isomorfeus/puppetmaster/session.rb', line 8 def initialize(app:, args: nil, channel: nil, default_viewport: nil, devtools: nil, dumpio: nil, env: nil, executable_path: nil, handle_SIGINT: nil, handle_SIGTERM: nil, handle_SIGHUP: nil, headless: true, ignore_default_args: nil, ignore_https_errors: nil, pipe: nil, product: :chrome, slow_mo: nil, timeout: nil, user_data_dir: nil) @app = app product = product.to_s @product = %w[chrome firefox].include?(product) ? product : 'chrome' @browser = ::Puppeteer.launch(args: args, channel: channel&.to_s, devtools: devtools, default_viewport: , dumpio: dumpio, env: env, executable_path: executable_path, handle_SIGINT: handle_SIGINT, handle_SIGTERM: handle_SIGTERM, handle_SIGHUP: handle_SIGHUP, headless: headless, ignore_default_args: ignore_default_args, ignore_https_errors: ignore_https_errors, pipe: pipe, product: product, slow_mo: slow_mo, timeout: timeout ? timeout : TIMEOUT, user_data_dir: user_data_dir) @original_user_agent = @browser.user_agent @default_page = @browser.new_page @default_page.puppetmaster_session = self ObjectSpace.define_finalizer(self, self.class.close_browser(self)) end |
Instance Attribute Details
#app ⇒ Object
Returns the value of attribute app.
6 7 8 |
# File 'lib/isomorfeus/puppetmaster/session.rb', line 6 def app @app end |
#browser ⇒ Object
Returns the value of attribute browser.
6 7 8 |
# File 'lib/isomorfeus/puppetmaster/session.rb', line 6 def browser @browser end |
#default_page ⇒ Object
Returns the value of attribute default_page.
6 7 8 |
# File 'lib/isomorfeus/puppetmaster/session.rb', line 6 def default_page @default_page end |
#product ⇒ Object
Returns the value of attribute product.
6 7 8 |
# File 'lib/isomorfeus/puppetmaster/session.rb', line 6 def product @product end |
#response ⇒ Object
Returns the value of attribute response.
6 7 8 |
# File 'lib/isomorfeus/puppetmaster/session.rb', line 6 def response @response end |
Instance Method Details
#close ⇒ Object
27 28 29 |
# File 'lib/isomorfeus/puppetmaster/session.rb', line 27 def close @browser.close end |
#device_names ⇒ Object
35 36 37 |
# File 'lib/isomorfeus/puppetmaster/session.rb', line 35 def device_names devices.keys end |
#devices ⇒ Object
31 32 33 |
# File 'lib/isomorfeus/puppetmaster/session.rb', line 31 def devices ::Puppeteer::DEVICES end |
#eval(j, *args) ⇒ Object
88 89 90 91 92 |
# File 'lib/isomorfeus/puppetmaster/session.rb', line 88 def eval(j, *args) r = @default_page.evaluate_handle("function() { return #{j} }", *args) return r if r.is_a?(Puppeteer::ElementHandle) r&.json_value end |
#eval_ruby(ruby_source = '', &block) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/isomorfeus/puppetmaster/session.rb', line 94 def eval_ruby(ruby_source = '', &block) ruby_source = Isomorfeus::Puppetmaster.block_source_code(&block) if block_given? compiled_ruby = Isomorfeus::Puppetmaster.compile_ruby_source(ruby_source) res = self.evaluate "function(){ try { return #{compiled_ruby} } catch (e) { return { error: e.name, message: e.message, stack: e.stack }; }}" if res.is_a?(Hash) && res.key?('error') && res.key?('message') && res.key?('stack') e = RuntimeError.new("#{res['error']}: #{res['message']}") e.set_backtrace(res['stack'].lines) raise e end res end |
#eval_with_opal(ruby_source = '', &block) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/isomorfeus/puppetmaster/session.rb', line 106 def eval_with_opal(ruby_source = '', &block) ruby_source = Isomorfeus::Puppetmaster.block_source_code(&block) if block_given? compiled_ruby = Isomorfeus::Puppetmaster.compile_ruby_source(ruby_source) res = self.evaluate " function(){\n if (typeof Opal === \"undefined\") {\n \#{Isomorfeus::Puppetmaster.opal_prelude}\n }\n try { return \#{compiled_ruby} }\n catch (e) { return { error: e.name, message: e.message, stack: e.stack }; }\n }\n JAVASCRIPT\n if res.is_a?(Hash) && res.key?('error') && res.key?('message') && res.key?('stack')\n e = RuntimeError.new(\"\#{res['error']}: \#{res['message']}\")\n e.set_backtrace(res['stack'].lines)\n raise e\n end\n res\nrescue Puppeteer::ExecutionContext::EvaluationError => e\n raise Isomorfeus::Puppetmaster::JavaScriptError.new(e.message)\nend\n" |
#evaluate(script, *args) ⇒ Object
83 84 85 86 |
# File 'lib/isomorfeus/puppetmaster/session.rb', line 83 def evaluate(script, *args) script = script.strip @default_page.evaluate_handle(script, *args) end |
#goto(uri, referer: nil, timeout: nil, wait_until: 'networkidle0') ⇒ Object Also known as: visit
43 44 45 46 47 |
# File 'lib/isomorfeus/puppetmaster/session.rb', line 43 def goto(uri, referer: nil, timeout: nil, wait_until: 'networkidle0') parsed_uri = parse_uri(uri) @response = @default_page.goto(parsed_uri.to_s, referer: referer, timeout: timeout, wait_until: wait_until) @default_page end |
#isomorphic(ruby_source = '', &block) ⇒ Object
128 129 130 131 132 |
# File 'lib/isomorfeus/puppetmaster/session.rb', line 128 def isomorphic(ruby_source = '', &block) ruby_source = Isomorfeus::Puppetmaster.block_source_code(&block) if block_given? Isomorfeus::Puppetmaster.served_app.on_server(ruby_source) eval_ruby(ruby_source) end |
#isomorphic_with_opal(ruby_source = '', &block) ⇒ Object
134 135 136 137 138 |
# File 'lib/isomorfeus/puppetmaster/session.rb', line 134 def isomorphic_with_opal(ruby_source = '', &block) ruby_source = Isomorfeus::Puppetmaster.block_source_code(&block) if block_given? Isomorfeus::Puppetmaster.served_app.on_server(ruby_source) eval_with_opal(ruby_source) end |
#network_conditions ⇒ Object
50 51 52 |
# File 'lib/isomorfeus/puppetmaster/session.rb', line 50 def network_conditions ::Puppeteer::NETWORK_CONDITIONS end |
#network_conition_names ⇒ Object
54 55 56 |
# File 'lib/isomorfeus/puppetmaster/session.rb', line 54 def network_conition_names network_conditions.map { |n| n[:name] } end |
#new_page(uri = nil, referer: nil, timeout: nil, wait_until: nil) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/isomorfeus/puppetmaster/session.rb', line 58 def new_page(uri = nil, referer: nil, timeout: nil, wait_until: nil) new_page = @browser.new_page if uri parsed_uri = parse_uri(uri) new_page.goto(parsed_uri.to_s, referer: referer, timeout: timeout, wait_until: wait_until) end new_page end |
#pages ⇒ Object
39 40 41 |
# File 'lib/isomorfeus/puppetmaster/session.rb', line 39 def pages @browser.pages end |
#parse_uri(uri) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/isomorfeus/puppetmaster/session.rb', line 67 def parse_uri(uri) return nil if uri.nil? return uri if uri == 'about:blank' parsed_uri = URI.parse(uri) if parsed_uri.host.nil? && parsed_uri.port.nil? && parsed_uri.scheme.nil? parsed_uri.host = self.app.host parsed_uri.port = self.app.port parsed_uri.scheme = self.app.scheme end parsed_uri end |
#user_agent ⇒ Object
79 80 81 |
# File 'lib/isomorfeus/puppetmaster/session.rb', line 79 def user_agent @browser.user_agent end |