Class: Zenrows::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/zenrows/proxy.rb

Overview

Builds ZenRows proxy configuration

ZenRows proxy encodes options in the password field of the proxy URL. Format: http://API_KEY-opt1=val1&opt2=val2:@host:port

Examples:

Basic proxy configuration

proxy = Zenrows::Proxy.new(api_key: 'key', host: 'proxy.zenrows.com', port: 1337)
proxy.build(js_render: true, premium_proxy: true)
# => { host: 'proxy.zenrows.com', port: 1337, username: 'key', password: 'js_render=true&premium_proxy=true' }

Since:

  • 0.1.0

Constant Summary collapse

MAX_WAIT_MS =

Maximum wait time in milliseconds (3 minutes)

Since:

  • 0.1.0

180_000
VALID_STICKY_TTL =

Valid sticky TTL values for HTTP proxy

Since:

  • 0.1.0

%w[30s 5m 30m 1h 1d].freeze
VALID_REGIONS =

Valid region codes for HTTP proxy

Since:

  • 0.1.0

{
  "africa" => "af",
  "af" => "af",
  "asia pacific" => "ap",
  "ap" => "ap",
  "europe" => "eu",
  "eu" => "eu",
  "middle east" => "me",
  "me" => "me",
  "north america" => "na",
  "na" => "na",
  "south america" => "sa",
  "sa" => "sa",
  "global" => nil
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, host:, port:) ⇒ Proxy

Returns a new instance of Proxy.

Parameters:

  • api_key (String)

    ZenRows API key

  • host (String)

    Proxy host

  • port (Integer)

    Proxy port

Since:

  • 0.1.0



57
58
59
60
61
# File 'lib/zenrows/proxy.rb', line 57

def initialize(api_key:, host:, port:)
  @api_key = api_key
  @host = host
  @port = port
end

Instance Attribute Details

#api_keyString (readonly)

Returns ZenRows API key.

Returns:

  • (String)

    ZenRows API key

Since:

  • 0.1.0



46
47
48
# File 'lib/zenrows/proxy.rb', line 46

def api_key
  @api_key
end

#hostString (readonly)

Returns Proxy host.

Returns:

  • (String)

    Proxy host

Since:

  • 0.1.0



49
50
51
# File 'lib/zenrows/proxy.rb', line 49

def host
  @host
end

#portInteger (readonly)

Returns Proxy port.

Returns:

  • (Integer)

    Proxy port

Since:

  • 0.1.0



52
53
54
# File 'lib/zenrows/proxy.rb', line 52

def port
  @port
end

Instance Method Details

#build(options = {}) ⇒ Hash

Build proxy configuration hash for HTTP client

Parameters:

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

    Proxy options

Options Hash (options):

  • :js_render (Boolean)

    Enable JavaScript rendering

  • :premium_proxy (Boolean)

    Use residential proxies

  • :proxy_country (String)

    Country code (us, gb, de, etc.)

  • :wait (Boolean, Integer)

    Wait time (true=15s, Integer=ms)

  • :wait_for (String)

    CSS selector to wait for

  • :session_id (Boolean, String, Integer)

    Session persistence

  • :window_height (Integer)

    Browser window height

  • :window_width (Integer)

    Browser window width

  • :js_instructions (Array, String)

    JavaScript instructions

  • :json_response (Boolean)

    Return JSON instead of HTML

  • :original_status (Boolean)

    Return original HTTP status

  • :screenshot (Boolean)

    Take screenshot

  • :screenshot_fullpage (Boolean)

    Full page screenshot

  • :screenshot_selector (String)

    Screenshot specific element

  • :custom_headers (Boolean)

    Enable custom headers passthrough

  • :block_resources (String)

    Block resources (image,media,font)

  • :device (String)

    Device emulation ('mobile' or 'desktop')

  • :antibot (Boolean)

    Enhanced antibot bypass mode

  • :session_ttl (String)

    Session duration ('30s', '5m', '30m', '1h', '1d')

Returns:

  • (Hash)

    Proxy configuration with :host, :port, :username, :password

Raises:

  • (WaitTimeError)

    if wait time exceeds 3 minutes

  • (ArgumentError)

    if session_ttl is invalid

Since:

  • 0.1.0



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/zenrows/proxy.rb', line 88

def build(options = {})
  opts = options.dup
  proxy_params = build_params(opts)

  {
    host: host,
    port: port,
    username: api_key,
    password: proxy_params.map { |k, v| "#{k}=#{v}" }.join("&")
  }
end

#build_array(options = {}) ⇒ Array<String, Integer, String, String>

Build proxy configuration as array [host, port, username, password]

Parameters:

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

    Proxy options (same as #build)

Returns:

  • (Array<String, Integer, String, String>)

    Proxy array

Since:

  • 0.1.0



114
115
116
117
# File 'lib/zenrows/proxy.rb', line 114

def build_array(options = {})
  config = build(options)
  [config[:host], config[:port], config[:username], config[:password]]
end

#build_url(options = {}) ⇒ String

Build proxy URL string

Parameters:

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

    Proxy options (same as #build)

Returns:

  • (String)

    Proxy URL

Since:

  • 0.1.0



104
105
106
107
108
# File 'lib/zenrows/proxy.rb', line 104

def build_url(options = {})
  config = build(options)
  password = config[:password].empty? ? "" : config[:password]
  "http://#{config[:username]}:#{password}@#{config[:host]}:#{config[:port]}"
end