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')

Since:

  • 0.2.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, api_endpoint: nil) ⇒ ApiClient

Initialize API client

Parameters:

  • api_key (String, nil) (defaults to: nil)

    Override API key (uses global config if nil)

  • api_endpoint (String, nil) (defaults to: nil)

    Override API endpoint (uses global config if nil)

Since:

  • 0.2.0



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

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

Instance Attribute Details

#api_endpointString (readonly)

Returns API endpoint URL.

Returns:

  • (String)

    API endpoint URL

Since:

  • 0.2.0



36
37
38
# File 'lib/zenrows/api_client.rb', line 36

def api_endpoint
  @api_endpoint
end

#api_keyString (readonly)

Returns ZenRows API key.

Returns:

  • (String)

    ZenRows API key

Since:

  • 0.2.0



33
34
35
# File 'lib/zenrows/api_client.rb', line 33

def api_key
  @api_key
end

#configConfiguration (readonly)

Returns Configuration instance.

Returns:

Since:

  • 0.2.0



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

def config
  @config
end

Instance Method Details

#get(url, **options) ⇒ ApiResponse

Make GET request through ZenRows API

Parameters:

  • url (String)

    Target URL to scrape

  • options (Hash)

    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:

Raises:

Since:

  • 0.2.0



78
79
80
81
82
# File 'lib/zenrows/api_client.rb', line 78

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

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

Make POST request through ZenRows API

Parameters:

  • url (String)

    Target URL

  • body (String, Hash) (defaults to: nil)

    Request body

  • options (Hash)

    Request options (same as #get)

Returns:

Since:

  • 0.2.0



90
91
92
93
94
# File 'lib/zenrows/api_client.rb', line 90

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