Class: Html2rss::RequestService::PuppetCommander

Inherits:
Object
  • Object
show all
Defined in:
lib/html2rss/request_service/puppet_commander.rb,
lib/html2rss/request_service/puppet_commander/preload_runner.rb,
lib/html2rss/request_service/puppet_commander/navigation_guards.rb

Overview

Commands the Puppeteer Browser to the website and builds the Response.

Public interface is #call; page setup, navigation, and body collection are private orchestration steps.

Defined Under Namespace

Classes: NavigationGuards, PreloadRunner

Constant Summary collapse

BROWSER_UNSAFE_HEADERS =

Request header names that must not be forwarded to the browser session.

%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

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.

Parameters:

  • ctx (Context)
  • browser (Puppeteer::Browser)
  • skip_request_resources (Set<String>) (defaults to: %w[stylesheet image media font].to_set)

    the resource types not to request

  • referer (String) (defaults to: [ctx.url.scheme, ctx.url.host].join('://'))

    the referer to use for the request



22
23
24
25
26
27
28
29
30
31
# File 'lib/html2rss/request_service/puppet_commander.rb', line 22

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
  @referer = referer
  @navigation_guards = NavigationGuards.new(ctx:, skip_request_resources:)
  @preload_runner = PreloadRunner.new(ctx:)
end

Instance Method Details

#callResponse

Visits the request URL and normalizes the page into a response object.

Returns:



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/html2rss/request_service/puppet_commander.rb', line 37

def call
  page = new_page
  navigation_response = navigate_to_destination(page, ctx.url)
  preload_runner.call(page)
  navigation_guards.raise_deferred_error!
  final_navigation_response = navigation_guards.latest_navigation_response || navigation_response
  navigation_guards.validate_final!(final_navigation_response)
  build_response(page, final_navigation_response)
ensure
  page&.close
end