Class: Zenrows::Configuration
- Inherits:
-
Object
- Object
- Zenrows::Configuration
- Includes:
- MonitorMixin
- Defined in:
- lib/zenrows/configuration.rb
Overview
Global configuration for Zenrows client
Constant Summary collapse
- DEFAULTS =
Default configuration values
{ host: "superproxy.zenrows.com", port: 1337, api_endpoint: "https://api.zenrows.com/v1/", connect_timeout: 5, read_timeout: 180, backend: :http_rb }.freeze
Instance Attribute Summary collapse
-
#api_endpoint ⇒ String
ZenRows API endpoint for ApiClient.
-
#api_key ⇒ String?
ZenRows API key (required).
-
#backend ⇒ Symbol
HTTP backend to use (:http_rb, :faraday, :net_http).
-
#connect_timeout ⇒ Integer
Default connection timeout in seconds.
-
#hooks ⇒ Zenrows::Hooks
readonly
Hook registry for request lifecycle events.
-
#host ⇒ String
ZenRows proxy host.
-
#logger ⇒ Logger?
Logger instance for debug output.
-
#port ⇒ Integer
ZenRows proxy port.
-
#read_timeout ⇒ Integer
Default read timeout in seconds.
Instance Method Summary collapse
-
#add_subscriber(subscriber) ⇒ self
Add a subscriber object for hook events.
-
#after_request(callable = nil) {|context| ... } ⇒ self
Register a callback to run after each request (always runs).
-
#around_request(callable = nil) {|context, &block| ... } ⇒ self
Register a callback to wrap around requests.
-
#before_request(callable = nil) {|context| ... } ⇒ self
Register a callback to run before each request.
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
-
#on_error(callable = nil) {|error, context| ... } ⇒ self
Register a callback to run on request error.
-
#on_response(callable = nil) {|response, context| ... } ⇒ self
Register a callback to run on successful response.
-
#reset! ⇒ void
Reset configuration to defaults.
-
#to_h ⇒ Hash
Convert configuration to hash.
-
#valid? ⇒ Boolean
Check if configuration is valid.
-
#validate! ⇒ true
Validate that required configuration is present.
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
61 62 63 64 |
# File 'lib/zenrows/configuration.rb', line 61 def initialize super # Initialize MonitorMixin reset! end |
Instance Attribute Details
#api_endpoint ⇒ String
Returns ZenRows API endpoint for ApiClient.
46 47 48 |
# File 'lib/zenrows/configuration.rb', line 46 def api_endpoint @api_endpoint end |
#api_key ⇒ String?
Returns ZenRows API key (required).
25 26 27 |
# File 'lib/zenrows/configuration.rb', line 25 def api_key @api_key end |
#backend ⇒ Symbol
Returns HTTP backend to use (:http_rb, :faraday, :net_http).
40 41 42 |
# File 'lib/zenrows/configuration.rb', line 40 def backend @backend end |
#connect_timeout ⇒ Integer
Returns Default connection timeout in seconds.
34 35 36 |
# File 'lib/zenrows/configuration.rb', line 34 def connect_timeout @connect_timeout end |
#hooks ⇒ Zenrows::Hooks (readonly)
Returns Hook registry for request lifecycle events.
49 50 51 |
# File 'lib/zenrows/configuration.rb', line 49 def hooks @hooks end |
#host ⇒ String
Returns ZenRows proxy host.
28 29 30 |
# File 'lib/zenrows/configuration.rb', line 28 def host @host end |
#logger ⇒ Logger?
Returns Logger instance for debug output.
43 44 45 |
# File 'lib/zenrows/configuration.rb', line 43 def logger @logger end |
#port ⇒ Integer
Returns ZenRows proxy port.
31 32 33 |
# File 'lib/zenrows/configuration.rb', line 31 def port @port end |
#read_timeout ⇒ Integer
Returns Default read timeout in seconds.
37 38 39 |
# File 'lib/zenrows/configuration.rb', line 37 def read_timeout @read_timeout end |
Instance Method Details
#add_subscriber(subscriber) ⇒ self
Add a subscriber object for hook events
Subscribers can implement any of: before_request, after_request, on_response, on_error, around_request.
185 186 187 188 |
# File 'lib/zenrows/configuration.rb', line 185 def add_subscriber(subscriber) hooks.add_subscriber(subscriber) self end |
#after_request(callable = nil) {|context| ... } ⇒ self
Register a callback to run after each request (always runs)
106 107 108 109 |
# File 'lib/zenrows/configuration.rb', line 106 def after_request(callable = nil, &block) hooks.register(:after_request, callable, &block) self end |
#around_request(callable = nil) {|context, &block| ... } ⇒ self
Register a callback to wrap around requests
Around callbacks can modify timing, add retries, etc. The block MUST call the passed block and return its result.
165 166 167 168 |
# File 'lib/zenrows/configuration.rb', line 165 def around_request(callable = nil, &block) hooks.register(:around_request, callable, &block) self end |
#before_request(callable = nil) {|context| ... } ⇒ self
Register a callback to run before each request
92 93 94 95 |
# File 'lib/zenrows/configuration.rb', line 92 def before_request(callable = nil, &block) hooks.register(:before_request, callable, &block) self end |
#on_error(callable = nil) {|error, context| ... } ⇒ self
Register a callback to run on request error
142 143 144 145 |
# File 'lib/zenrows/configuration.rb', line 142 def on_error(callable = nil, &block) hooks.register(:on_error, callable, &block) self end |
#on_response(callable = nil) {|response, context| ... } ⇒ self
Register a callback to run on successful response
127 128 129 130 |
# File 'lib/zenrows/configuration.rb', line 127 def on_response(callable = nil, &block) hooks.register(:on_response, callable, &block) self end |
#reset! ⇒ void
This method returns an undefined value.
Reset configuration to defaults
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/zenrows/configuration.rb', line 69 def reset! synchronize do @api_key = nil @host = DEFAULTS[:host] @port = DEFAULTS[:port] @api_endpoint = DEFAULTS[:api_endpoint] @connect_timeout = DEFAULTS[:connect_timeout] @read_timeout = DEFAULTS[:read_timeout] @backend = DEFAULTS[:backend] @logger = nil @hooks = Hooks.new end end |
#to_h ⇒ Hash
Convert configuration to hash
213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/zenrows/configuration.rb', line 213 def to_h { api_key: api_key, host: host, port: port, api_endpoint: api_endpoint, connect_timeout: connect_timeout, read_timeout: read_timeout, backend: backend } end |
#valid? ⇒ Boolean
Check if configuration is valid
203 204 205 206 207 208 |
# File 'lib/zenrows/configuration.rb', line 203 def valid? validate! true rescue ConfigurationError false end |
#validate! ⇒ true
Validate that required configuration is present
194 195 196 197 198 |
# File 'lib/zenrows/configuration.rb', line 194 def validate! raise ConfigurationError, "api_key is required" if api_key.nil? || api_key.empty? true end |