Class: Zenrows::ApiClient
- Inherits:
-
Object
- Object
- Zenrows::ApiClient
- 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.
Instance Attribute Summary collapse
-
#api_endpoint ⇒ String
readonly
API endpoint URL.
-
#api_key ⇒ String
readonly
ZenRows API key.
-
#config ⇒ Configuration
readonly
Configuration instance.
-
#hooks ⇒ Hooks
readonly
Hook registry for this client.
Instance Method Summary collapse
-
#get(url, **options) ⇒ ApiResponse
Make GET request through ZenRows API.
-
#initialize(api_key: nil, api_endpoint: nil) {|config| ... } ⇒ ApiClient
constructor
Initialize API client.
-
#post(url, body: nil, **options) ⇒ ApiResponse
Make POST request through ZenRows API.
Constructor Details
#initialize(api_key: nil, api_endpoint: nil) {|config| ... } ⇒ ApiClient
Initialize API client
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_endpoint ⇒ String (readonly)
Returns API endpoint URL.
41 42 43 |
# File 'lib/zenrows/api_client.rb', line 41 def api_endpoint @api_endpoint end |
#api_key ⇒ String (readonly)
Returns ZenRows API key.
38 39 40 |
# File 'lib/zenrows/api_client.rb', line 38 def api_key @api_key end |
#config ⇒ Configuration (readonly)
Returns Configuration instance.
44 45 46 |
# File 'lib/zenrows/api_client.rb', line 44 def config @config end |
#hooks ⇒ Hooks (readonly)
Returns Hook registry for this client.
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
91 92 93 94 95 96 97 |
# File 'lib/zenrows/api_client.rb', line 91 def get(url, **) instrument(:get, url, ) do params = build_params(url, ) http_response = build_http_client.get(api_endpoint, params: params) handle_response(http_response, ) end end |
#post(url, body: nil, **options) ⇒ ApiResponse
Make POST request through ZenRows API
105 106 107 108 109 110 111 |
# File 'lib/zenrows/api_client.rb', line 105 def post(url, body: nil, **) instrument(:post, url, ) do params = build_params(url, ) http_response = build_http_client.post(api_endpoint, params: params, body: body) handle_response(http_response, ) end end |