Class: Zenrows::ApiClient

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

Overview

REST API client for ZenRows Universal Scraper API

Unlike the proxy-based Client, ApiClient calls the ZenRows API directly. This enables features not available in proxy mode: autoparse, css_extractor, response_type (markdown), and outputs.

Examples:

Basic usage

api = Zenrows::ApiClient.new
response = api.get('https://example.com')

With autoparse

response = api.get('https://amazon.com/dp/B01LD5GO7I', autoparse: true)
puts response.data  # Structured product data

With CSS extraction

response = api.get(url, css_extractor: { title: 'h1', links: 'a @href' })

With markdown output

response = api.get(url, response_type: 'markdown')

With per-client hooks

api = Zenrows::ApiClient.new do |c|
  c.on_response { |resp, ctx| puts "#{ctx[:host]} -> #{resp.status}" }
end

Author:

  • Ernest Bursa

Since:

  • 0.2.0

API:

  • public

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, api_endpoint: nil) {|config| ... } ⇒ ApiClient

Initialize API client

Parameters:

  • (defaults to: nil)

    Override API key (uses global config if nil)

  • (defaults to: nil)

    Override API endpoint (uses global config if nil)

Yields:

  • (config)

    Optional block for per-client configuration (hooks)

Yield Parameters:

Since:

  • 0.2.0

API:

  • public



55
56
57
58
59
60
61
62
63
# File 'lib/zenrows/api_client.rb', line 55

def initialize(api_key: nil, api_endpoint: nil, &block)
  @config = Zenrows.configuration
  @api_key = api_key || @config.api_key
  @api_endpoint = api_endpoint || @config.api_endpoint
  @config.validate! unless api_key

  # Build hooks: start with global, allow per-client additions
  @hooks = block ? build_hooks(&block) : Zenrows.configuration.hooks.dup
end

Instance Attribute Details

#api_endpointString (readonly)

Returns API endpoint URL.

Returns:

  • API endpoint URL

Since:

  • 0.2.0

API:

  • public



41
42
43
# File 'lib/zenrows/api_client.rb', line 41

def api_endpoint
  @api_endpoint
end

#api_keyString (readonly)

Returns ZenRows API key.

Returns:

  • ZenRows API key

Since:

  • 0.2.0

API:

  • public



38
39
40
# File 'lib/zenrows/api_client.rb', line 38

def api_key
  @api_key
end

#configConfiguration (readonly)

Returns Configuration instance.

Returns:

  • Configuration instance

Since:

  • 0.2.0

API:

  • public



44
45
46
# File 'lib/zenrows/api_client.rb', line 44

def config
  @config
end

#hooksHooks (readonly)

Returns Hook registry for this client.

Returns:

  • Hook registry for this client

Since:

  • 0.2.0

API:

  • public



47
48
49
# File 'lib/zenrows/api_client.rb', line 47

def hooks
  @hooks
end

Instance Method Details

#get(url, **options) ⇒ ApiResponse

Make GET request through ZenRows API

Parameters:

  • Target URL to scrape

  • Request options

Options Hash (**options):

  • :autoparse (Boolean)

    Auto-extract structured data

  • :css_extractor (Hash, CssExtractor)

    CSS selectors for extraction

  • :response_type (String)

    Response format ('markdown')

  • :outputs (String)

    Extract specific data ('headings,links,menus')

  • :js_render (Boolean)

    Enable JavaScript rendering

  • :premium_proxy (Boolean)

    Use residential proxies

  • :proxy_country (String)

    Country code

  • :wait (Integer, Boolean)

    Wait time in ms

  • :wait_for (String)

    CSS selector to wait for

  • :session_id (Boolean, String)

    Session persistence

  • :js_instructions (Array, String)

    Browser automation

  • :json_response (Boolean)

    Return JSON with XHR data

  • :screenshot (Boolean)

    Take screenshot

  • :screenshot_fullpage (Boolean)

    Full page screenshot

  • :screenshot_selector (String)

    Screenshot element

  • :block_resources (String)

    Block resources

  • :device (String)

    Device emulation

  • :antibot (Boolean)

    Enhanced antibot

Returns:

  • Response wrapper

Raises:

  • if API key not configured

  • if API key invalid

  • if rate limited

Since:

  • 0.2.0

API:

  • public



91
92
93
94
95
96
97
# File 'lib/zenrows/api_client.rb', line 91

def get(url, **options)
  instrument(:get, url, options) do
    params = build_params(url, options)
    http_response = build_http_client.get(api_endpoint, params: params)
    handle_response(http_response, options)
  end
end

#post(url, body: nil, **options) ⇒ ApiResponse

Make POST request through ZenRows API

Parameters:

  • Target URL

  • (defaults to: nil)

    Request body

  • Request options (same as #get)

Returns:

  • Response wrapper

Since:

  • 0.2.0

API:

  • public



105
106
107
108
109
110
111
# File 'lib/zenrows/api_client.rb', line 105

def post(url, body: nil, **options)
  instrument(:post, url, options) do
    params = build_params(url, options)
    http_response = build_http_client.post(api_endpoint, params: params, body: body)
    handle_response(http_response, options)
  end
end