Class: Zenrows::Backends::HttpRb

Inherits:
Base
  • Object
show all
Defined in:
lib/zenrows/backends/http_rb.rb

Overview

HTTP.rb backend adapter

Uses the http.rb gem to build configured HTTP clients that route through the ZenRows proxy.

Examples:

Basic usage

backend = Zenrows::Backends::HttpRb.new(proxy: proxy, config: config)
http = backend.build_client(js_render: true)
response = http.get(url)  # SSL context is auto-configured

Author:

  • Ernest Bursa

Since:

  • 0.1.0

Instance Attribute Summary

Attributes inherited from Base

#config, #proxy

Instance Method Summary collapse

Methods inherited from Base

#calculate_timeouts, #initialize, #ssl_context

Constructor Details

This class inherits a constructor from Zenrows::Backends::Base

Instance Method Details

#build_client(options = {}) ⇒ HTTP::Client

Build a configured HTTP client

Parameters:

  • options (Hash) (defaults to: {})

    Request options

Options Hash (options):

  • :js_render (Boolean)

    Enable JavaScript rendering

  • :premium_proxy (Boolean)

    Use residential proxies

  • :proxy_country (String)

    Country code

  • :wait (Boolean, Integer)

    Wait time

  • :wait_for (String)

    CSS selector to wait for

  • :headers (Hash)

    Custom HTTP headers

Returns:

  • (HTTP::Client)

    Configured HTTP client

Since:

  • 0.1.0



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/zenrows/backends/http_rb.rb', line 31

def build_client(options = {})
  opts = options.dup
  headers = opts.delete(:headers) || {}

  # Enable custom_headers if we have headers
  opts[:custom_headers] = true if headers.any?

  # Get proxy configuration
  proxy_config = proxy.build(opts)

  # Calculate timeouts
  timeouts = calculate_timeouts(opts)

  # Build HTTP client with SSL context and proxy
  HTTP
    .timeout(connect: timeouts[:connect], read: timeouts[:read])
    .headers(headers)
    .via(
      proxy_config[:host],
      proxy_config[:port],
      proxy_config[:username],
      proxy_config[:password],
      ssl_context: ssl_context
    )
end