Class: Zenrows::InstrumentedClient Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Wrapper around HTTP clients that executes hooks on requests

Decorates an HTTP client (from http.rb or Net::HTTP) to add hook execution before, during, and after requests.

Examples:

client = InstrumentedClient.new(http_client,
  hooks: hooks_registry,
  context_base: { backend: :http_rb, options: { js_render: true } }
)
response = client.get("https://example.com")

Author:

  • Ernest Bursa

Since:

  • 0.3.0

Constant Summary collapse

HTTP_METHODS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

HTTP methods to instrument

Since:

  • 0.3.0

i[get post put patch delete head options].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http, hooks:, context_base:) ⇒ InstrumentedClient

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create a new instrumented client

Parameters:

  • http (Object)

    Underlying HTTP client (HTTP::Client, NetHttpClient, etc.)

  • hooks (Hooks)

    Hook registry to use

  • context_base (Hash)

    Base context to merge into all requests

Since:

  • 0.3.0



37
38
39
40
41
# File 'lib/zenrows/instrumented_client.rb', line 37

def initialize(http, hooks:, context_base:)
  @http = http
  @hooks = hooks
  @context_base = context_base
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, **kwargs, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Delegate unknown methods to underlying client

Parameters:

  • method (Symbol)

    Method name

  • args (Array)

    Method arguments

  • kwargs (Hash)

    Keyword arguments

  • block (Proc)

    Block to pass

Returns:

  • (Object)

    Result of delegated method

Since:

  • 0.3.0



113
114
115
116
117
118
119
# File 'lib/zenrows/instrumented_client.rb', line 113

def method_missing(method, *args, **kwargs, &block)
  if @http.respond_to?(method)
    @http.public_send(method, *args, **kwargs, &block)
  else
    super
  end
end

Instance Attribute Details

#context_baseHash (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Base context for all requests.

Returns:

  • (Hash)

    Base context for all requests

Since:

  • 0.3.0



30
31
32
# File 'lib/zenrows/instrumented_client.rb', line 30

def context_base
  @context_base
end

#hooksHooks (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Hook registry.

Returns:

  • (Hooks)

    Hook registry

Since:

  • 0.3.0



27
28
29
# File 'lib/zenrows/instrumented_client.rb', line 27

def hooks
  @hooks
end

#httpObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Underlying HTTP client.

Returns:

  • (Object)

    Underlying HTTP client

Since:

  • 0.3.0



24
25
26
# File 'lib/zenrows/instrumented_client.rb', line 24

def http
  @http
end

Instance Method Details

#delete(url, **options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Instrumented DELETE request

Parameters:

  • url (String)

    URL to request

  • options (Hash)

    Request options

Returns:

  • (Object)

    HTTP response

Since:

  • 0.3.0



84
85
86
# File 'lib/zenrows/instrumented_client.rb', line 84

def delete(url, **options)
  instrument(:delete, url, options) { @http.delete(url, **options) }
end

#get(url, **options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Instrumented GET request

Parameters:

  • url (String)

    URL to request

  • options (Hash)

    Request options

Returns:

  • (Object)

    HTTP response

Since:

  • 0.3.0



48
49
50
# File 'lib/zenrows/instrumented_client.rb', line 48

def get(url, **options)
  instrument(:get, url, options) { @http.get(url, **options) }
end

#head(url, **options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Instrumented HEAD request

Parameters:

  • url (String)

    URL to request

  • options (Hash)

    Request options

Returns:

  • (Object)

    HTTP response

Since:

  • 0.3.0



93
94
95
# File 'lib/zenrows/instrumented_client.rb', line 93

def head(url, **options)
  instrument(:head, url, options) { @http.head(url, **options) }
end

#options(url, **options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Instrumented OPTIONS request

Parameters:

  • url (String)

    URL to request

  • options (Hash)

    Request options

Returns:

  • (Object)

    HTTP response

Since:

  • 0.3.0



102
103
104
# File 'lib/zenrows/instrumented_client.rb', line 102

def options(url, **options)
  instrument(:options, url, options) { @http.options(url, **options) }
end

#patch(url, **options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Instrumented PATCH request

Parameters:

  • url (String)

    URL to request

  • options (Hash)

    Request options

Returns:

  • (Object)

    HTTP response

Since:

  • 0.3.0



75
76
77
# File 'lib/zenrows/instrumented_client.rb', line 75

def patch(url, **options)
  instrument(:patch, url, options) { @http.patch(url, **options) }
end

#post(url, **options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Instrumented POST request

Parameters:

  • url (String)

    URL to request

  • options (Hash)

    Request options

Returns:

  • (Object)

    HTTP response

Since:

  • 0.3.0



57
58
59
# File 'lib/zenrows/instrumented_client.rb', line 57

def post(url, **options)
  instrument(:post, url, options) { @http.post(url, **options) }
end

#put(url, **options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Instrumented PUT request

Parameters:

  • url (String)

    URL to request

  • options (Hash)

    Request options

Returns:

  • (Object)

    HTTP response

Since:

  • 0.3.0



66
67
68
# File 'lib/zenrows/instrumented_client.rb', line 66

def put(url, **options)
  instrument(:put, url, options) { @http.put(url, **options) }
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if method is supported

Parameters:

  • method (Symbol)

    Method name

  • include_private (Boolean) (defaults to: false)

    Include private methods

Returns:

  • (Boolean)

    true if method is supported

Since:

  • 0.3.0



126
127
128
# File 'lib/zenrows/instrumented_client.rb', line 126

def respond_to_missing?(method, include_private = false)
  @http.respond_to?(method, include_private) || super
end