Class: Zenrows::Backends::Base Abstract

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

Overview

This class is abstract.

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.

Author:

  • Ernest Bursa

Since:

  • 0.1.0

Direct Known Subclasses

HttpRb, NetHttp

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proxy:, config:, hooks: nil) ⇒ Base

Returns a new instance of Base.

Parameters:

Since:

  • 0.1.0



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

#configZenrows::Configuration (readonly)

Returns Configuration instance.

Returns:

Since:

  • 0.1.0



19
20
21
# File 'lib/zenrows/backends/base.rb', line 19

def config
  @config
end

#hooksZenrows::Hooks (readonly)

Returns Hook registry for this backend.

Returns:

Since:

  • 0.1.0



22
23
24
# File 'lib/zenrows/backends/base.rb', line 22

def hooks
  @hooks
end

#proxyZenrows::Proxy (readonly)

Returns Proxy configuration builder.

Returns:

Since:

  • 0.1.0



16
17
18
# File 'lib/zenrows/backends/base.rb', line 16

def proxy
  @proxy
end

Instance Method Details

#backend_nameSymbol

Get the backend name for context

Returns:

  • (Symbol)

    Backend identifier

Since:

  • 0.1.0



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

Parameters:

  • options (Hash) (defaults to: {})

    Request options

Returns:

  • (Object)

    Configured HTTP client

Raises:

  • (NotImplementedError)

    if not implemented by subclass

Since:

  • 0.1.0



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

def build_client(options = {})
  raise NotImplementedError, "#{self.class}#build_client must be implemented"
end

#calculate_timeouts(options = {}) ⇒ Hash

Calculate appropriate timeout based on options

Parameters:

  • options (Hash) (defaults to: {})

    Request options

Returns:

  • (Hash)

    Timeout configuration with :connect and :read

Since:

  • 0.1.0



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(options = {})
  connect = config.connect_timeout
  read = config.read_timeout

  # Add time for JS rendering
  read += 15 if options[:js_render]

  # Add buffer for wait time
  if options[:wait]
    wait_seconds = normalize_wait_seconds(options[:wait])
    read += wait_seconds + 20
  end

  # Add time for screenshots
  read += 10 if options[:screenshot] || options[:screenshot_fullpage]

  # Add time for JS instructions
  if options[:js_instructions]
    instructions = options[:js_instructions]
    count = instructions.is_a?(Array) ? instructions.size : 1
    read += count * 5
  end

  {connect: connect, read: read}
end

#ssl_contextOpenSSL::SSL::SSLContext

Build SSL context for proxy connections

Returns:

  • (OpenSSL::SSL::SSLContext)

    SSL context with verification disabled

Since:

  • 0.1.0



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

Parameters:

  • client (Object)

    The underlying HTTP client

  • options (Hash)

    Request options used for this client

Returns:

  • (Object)

    Instrumented client or original if no hooks

Since:

  • 0.1.0



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/zenrows/backends/base.rb', line 87

def wrap_client(client, options)
  return client if hooks.empty?

  InstrumentedClient.new(
    client,
    hooks: hooks,
    context_base: {
      options: options,
      backend: backend_name
    }
  )
end