Class: Html2rss::RequestService::BrowserlessStrategy

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

Overview

Browserless.io strategy to request websites.

Provide the WebSocket URL and your API token via environment variables:

  • BROWSERLESS_IO_WEBSOCKET_URL
  • BROWSERLESS_IO_API_TOKEN

To use this strategy, you need to have a Browserless.io account or run a local Browserless.io instance.

To run a local Browserless.io instance, you can use the following Docker command:

docker run \
  --rm \
  -p 3000:3000 \
  -e "CONCURRENT=10" \
  -e "TOKEN=6R0W53R135510" \
  ghcr.io/browserless/chromium

When running locally, you can skip setting the environment variables, as above commands are aligned with the default values.

Instance Method Summary collapse

Methods inherited from Strategy

#initialize

Constructor Details

This class inherits a constructor from Html2rss::RequestService::Strategy

Instance Method Details

#browser_ws_endpointString

Returns the Browserless websocket endpoint with token query param.

Returns:

  • (String)

    the Browserless websocket endpoint with token query param

Raises:

  • (ArgumentError)

    if a custom endpoint is configured without an API token



49
50
51
52
53
54
55
56
# File 'lib/html2rss/request_service/browserless_strategy.rb', line 49

def browser_ws_endpoint
  @browser_ws_endpoint ||= begin
    ws_url = ENV.fetch('BROWSERLESS_IO_WEBSOCKET_URL', 'ws://127.0.0.1:3000')
    api_token = browserless_api_token(ws_url)

    "#{ws_url}?token=#{api_token}"
  end
end

#executeResponse

Executes a Browserless-backed request with the shared request policy.

Returns:

  • (Response)

    normalized request response

Raises:

  • (RequestTimedOut)

    if the browser session exceeds the configured timeout



39
40
41
42
43
44
# File 'lib/html2rss/request_service/browserless_strategy.rb', line 39

def execute
  validate_request!
  execute_browserless_request
rescue Puppeteer::TimeoutError => error
  raise RequestTimedOut, error.message
end