Class: Zenrows::Backends::Base Abstract
- Inherits:
-
Object
- Object
- Zenrows::Backends::Base
- Defined in:
- lib/zenrows/backends/base.rb
Overview
Subclass and override #build_client to implement
Abstract base class for HTTP backends
Backends are responsible for building configured HTTP clients that route through the ZenRows proxy.
Instance Attribute Summary collapse
-
#config ⇒ Zenrows::Configuration
readonly
Configuration instance.
-
#hooks ⇒ Zenrows::Hooks
readonly
Hook registry for this backend.
-
#proxy ⇒ Zenrows::Proxy
readonly
Proxy configuration builder.
Instance Method Summary collapse
-
#backend_name ⇒ Symbol
Get the backend name for context.
-
#build_client(options = {}) ⇒ Object
Build a configured HTTP client.
-
#calculate_timeouts(options = {}) ⇒ Hash
Calculate appropriate timeout based on options.
-
#initialize(proxy:, config:, hooks: nil) ⇒ Base
constructor
A new instance of Base.
-
#ssl_context ⇒ OpenSSL::SSL::SSLContext
Build SSL context for proxy connections.
-
#wrap_client(client, options) ⇒ Object
Wrap HTTP client with instrumentation if hooks are registered.
Constructor Details
#initialize(proxy:, config:, hooks: nil) ⇒ Base
Returns a new instance of Base.
27 28 29 30 31 |
# File 'lib/zenrows/backends/base.rb', line 27 def initialize(proxy:, config:, hooks: nil) @proxy = proxy @config = config @hooks = hooks || config.hooks&.dup || Hooks.new end |
Instance Attribute Details
#config ⇒ Zenrows::Configuration (readonly)
Returns Configuration instance.
19 20 21 |
# File 'lib/zenrows/backends/base.rb', line 19 def config @config end |
#hooks ⇒ Zenrows::Hooks (readonly)
Returns Hook registry for this backend.
22 23 24 |
# File 'lib/zenrows/backends/base.rb', line 22 def hooks @hooks end |
#proxy ⇒ Zenrows::Proxy (readonly)
Returns Proxy configuration builder.
16 17 18 |
# File 'lib/zenrows/backends/base.rb', line 16 def proxy @proxy end |
Instance Method Details
#backend_name ⇒ Symbol
Get the backend name for context
103 104 105 |
# File 'lib/zenrows/backends/base.rb', line 103 def backend_name :base end |
#build_client(options = {}) ⇒ Object
Build a configured HTTP client
38 39 40 |
# File 'lib/zenrows/backends/base.rb', line 38 def build_client( = {}) raise NotImplementedError, "#{self.class}#build_client must be implemented" end |
#calculate_timeouts(options = {}) ⇒ Hash
Calculate appropriate timeout based on options
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/zenrows/backends/base.rb', line 56 def calculate_timeouts( = {}) connect = config.connect_timeout read = config.read_timeout # Add time for JS rendering read += 15 if [:js_render] # Add buffer for wait time if [:wait] wait_seconds = normalize_wait_seconds([:wait]) read += wait_seconds + 20 end # Add time for screenshots read += 10 if [:screenshot] || [:screenshot_fullpage] # Add time for JS instructions if [:js_instructions] instructions = [:js_instructions] count = instructions.is_a?(Array) ? instructions.size : 1 read += count * 5 end {connect: connect, read: read} end |
#ssl_context ⇒ OpenSSL::SSL::SSLContext
Build SSL context for proxy connections
45 46 47 48 49 50 |
# File 'lib/zenrows/backends/base.rb', line 45 def ssl_context require "openssl" ctx = OpenSSL::SSL::SSLContext.new ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE ctx end |
#wrap_client(client, options) ⇒ Object
Wrap HTTP client with instrumentation if hooks are registered
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/zenrows/backends/base.rb', line 87 def wrap_client(client, ) return client if hooks.empty? InstrumentedClient.new( client, hooks: hooks, context_base: { options: , backend: backend_name } ) end |