Class: Net::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/httplog/adapters/net_http.rb

Instance Method Summary collapse

Instance Method Details

#connectObject



44
45
46
47
48
# File 'lib/httplog/adapters/net_http.rb', line 44

def connect
  HttpLog.log_connection(@address, @port) if !started? && HttpLog.url_approved?("#{@address}:#{@port}")

  orig_connect
end

#orig_connectObject



6
# File 'lib/httplog/adapters/net_http.rb', line 6

alias orig_connect connect

#orig_requestObject



5
# File 'lib/httplog/adapters/net_http.rb', line 5

alias orig_request request

#request(req, body = nil, &block) ⇒ Object



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
39
40
41
42
# File 'lib/httplog/adapters/net_http.rb', line 8

def request(req, body = nil, &block)
  url = "http://#{@address}:#{@port}#{req.path}"

  bm = Benchmark.realtime do
    @response = orig_request(req, body, &block)
  end
  body_stream  = req.body_stream
  request_body = if body_stream
                   body_stream.to_s # read and rewind for RestClient::Payload::Base
                   body_stream.rewind if body_stream.respond_to?(:rewind) # RestClient::Payload::Base has no method rewind
                   body_stream.read
                 elsif req.body.nil? || req.body.empty?
                   body
                 else
                   req.body
                 end

  if HttpLog.url_approved?(url) && started?
    HttpLog.call(
      method: req.method,
      url: url,
      request_body: request_body,
      request_headers: req.each_header.collect,
      response_code: @response.code,
      response_body: @response.body,
      response_headers: @response.each_header.collect,
      benchmark: bm,
      encoding: @response['Content-Encoding'],
      content_type: @response['Content-Type'],
      mask_body: HttpLog.masked_body_url?(url)
    )
  end

  @response
end