Class: Html2rss::RequestService::PuppetCommander

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

Overview

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

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



12
13
14
15
16
17
18
19
20
# File 'lib/html2rss/request_service/puppet_commander.rb', line 12

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) ⇒ Object



54
# File 'lib/html2rss/request_service/puppet_commander.rb', line 54

def body(page) = page.content

#callResponse

Returns:



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

def call
  page = new_page

  response = navigate_to_destination(page, ctx.url)

  Response.new(body: body(page), headers: response.headers)
ensure
  page&.close
end


50
51
52
# File 'lib/html2rss/request_service/puppet_commander.rb', line 50

def navigate_to_destination(page, url)
  page.goto(url, wait_until: 'networkidle0', referer:)
end

#new_pagePuppeteer::Page



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

def new_page
  page = browser.new_page
  page.extra_http_headers = ctx.headers

  return page if skip_request_resources.empty?

  page.request_interception = true
  page.on('request') do |request|
    skip_request_resources.member?(request.resource_type) ? request.abort : request.continue
  end

  page
end