Class: DevSuite::RequestLogger::Adapter::NetHttp

Inherits:
Base show all
Defined in:
lib/dev_suite/request_logger/adapter/net_http.rb

Instance Method Summary collapse

Methods inherited from Utils::Construct::Component::Base

component_key

Instance Method Details

#disableObject

Disables the logging by restoring the original Net::HTTP request method



41
42
43
44
45
46
47
48
# File 'lib/dev_suite/request_logger/adapter/net_http.rb', line 41

def disable
  ::Net::HTTP.class_eval do
    # Restore the original request method by aliasing it back from _original_request
    # This effectively removes the logging functionality and returns Net::HTTP to its original state
    alias_method(:request, :_original_request)
    remove_method(:_original_request)
  end
end

#enableObject

Enables the logging of Net::HTTP requests by monkey patching the request method



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dev_suite/request_logger/adapter/net_http.rb', line 8

def enable
  ::Net::HTTP.class_eval do
    # Alias the original request method with a unique name (_original_request)
    # This preserves the original functionality so it can still be called after logging is added
    alias_method(:_original_request, :request)

    # Override the request method to add logging functionality
    def request(request, body = nil, &block)
      start_time = Time.now

      Logger.log_request(self, request)

      response = nil
      begin
        # Call the original request method (now aliased as _original_request) to perform the actual HTTP request
        response = _original_request(request, body, &block)
      ensure
        end_time = Time.now
        response ||= Net::HTTPResponse.new("1.1", "500", "Internal Server Error")
        response.instance_variable_set(:@start_time, start_time)
        response.instance_variable_set(:@end_time, end_time)
        response.define_singleton_method(:start_time) { @start_time }
        response.define_singleton_method(:end_time) { @end_time }

        Logger.log_response(self, response)
      end

      response
    end
  end
end