Class: Html2rss::RequestService::PuppetCommander
- Inherits:
-
Object
- Object
- Html2rss::RequestService::PuppetCommander
- Defined in:
- lib/html2rss/request_service/puppet_commander.rb
Overview
Commands the Puppeteer Browser to the website and builds the Response.
Constant Summary collapse
- BROWSER_UNSAFE_HEADERS =
rubocop:disable Metrics/ClassLength
%w[ host connection content-length transfer-encoding sec-fetch-dest sec-fetch-mode sec-fetch-site sec-fetch-user upgrade-insecure-requests ].to_set.freeze
Instance Method Summary collapse
-
#body(page) ⇒ String
Rendered HTML content.
-
#call ⇒ Response
Visits the request URL and normalizes the page into a response object.
- #configure_navigation_guards(page) ⇒ void
- #configure_page(page) ⇒ void
-
#initialize(ctx, browser, skip_request_resources: %w[stylesheet image media font].to_set, referer: [ctx.url.scheme, ctx.url.host].join('://')) ⇒ PuppetCommander
constructor
A new instance of PuppetCommander.
-
#navigate_to_destination(page, url) ⇒ Puppeteer::HTTPResponse?
The navigation response if one was produced.
- #new_page ⇒ Puppeteer::Page
Constructor Details
#initialize(ctx, browser, skip_request_resources: %w[stylesheet image media font].to_set, referer: [ctx.url.scheme, ctx.url.host].join('://')) ⇒ PuppetCommander
Returns a new instance of PuppetCommander.
18 19 20 21 22 23 24 25 26 |
# File 'lib/html2rss/request_service/puppet_commander.rb', line 18 def initialize(ctx, browser, skip_request_resources: %w[stylesheet image media font].to_set, referer: [ctx.url.scheme, ctx.url.host].join('://')) @ctx = ctx @browser = browser @skip_request_resources = skip_request_resources @referer = referer end |
Instance Method Details
#body(page) ⇒ String
Returns rendered HTML content.
94 |
# File 'lib/html2rss/request_service/puppet_commander.rb', line 94 def body(page) = page.content |
#call ⇒ Response
Visits the request URL and normalizes the page into a response object.
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/html2rss/request_service/puppet_commander.rb', line 32 def call page = new_page = navigate_to_destination(page, ctx.url) perform_preload(page) = || () build_response(page, ) ensure page&.close end |
#configure_navigation_guards(page) ⇒ void
This method returns an undefined value.
67 68 69 70 71 72 73 |
# File 'lib/html2rss/request_service/puppet_commander.rb', line 67 def (page) page.request_interception = true page.on('request') do |request| handle_request(request) end page.on('response') { |response| handle_response(response) } end |
#configure_page(page) ⇒ void
This method returns an undefined value.
58 59 60 61 62 |
# File 'lib/html2rss/request_service/puppet_commander.rb', line 58 def configure_page(page) page.extra_http_headers = browser_headers page. = page.default_timeout = end |
#navigate_to_destination(page, url) ⇒ Puppeteer::HTTPResponse?
Returns the navigation response if one was produced.
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/html2rss/request_service/puppet_commander.rb', line 79 def navigate_to_destination(page, url) = nil = nil page.goto(url, wait_until: 'networkidle0', referer:, timeout: ).tap do end rescue StandardError raise end |
#new_page ⇒ Puppeteer::Page
47 48 49 50 51 52 53 |
# File 'lib/html2rss/request_service/puppet_commander.rb', line 47 def new_page page = browser.new_page @main_frame = page.main_frame if page.respond_to?(:main_frame) configure_page(page) (page) page end |