Module: Ethon::Easy::Operations

Defined in:
lib/httplog/adapters/ethon.rb

Instance Method Summary collapse

Instance Method Details

#httplog_add_callbackObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/httplog/adapters/ethon.rb', line 44

def httplog_add_callback
  # Hack to perform this callback before the cleanup
  @on_complete ||= []
  @on_complete.unshift -> (*) do
    # Not sure where the actual status code is stored - so let's
    # extract it from the response header.
    encoding = response_headers.scan(/Content-Encoding: (\S+)/i).flatten.first
    content_type = response_headers.scan(/Content-Type: (\S+(; charset=\S+)?)/i).flatten.first

    # Hard to believe that Ethon wouldn't parse out the headers into
    # an array; probably overlooked it. Anyway, let's do it ourselves:
    headers = response_headers.split(/\r?\n/).drop(1)

    @http_log.merge!(
      encoding: encoding,
      content_type: content_type,
      response_headers: headers,
      response_body: response_body
    )
  end
end

#orig_performObject



17
# File 'lib/httplog/adapters/ethon.rb', line 17

alias orig_perform perform

#performObject



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/ethon.rb', line 18

def perform
  return orig_perform unless HttpLog.url_approved?(url)

  httplog_add_callback

  bm = Benchmark.realtime { orig_perform }

  url = @http_log[:url]
  url = "#{url}?#{@http_log[:params]}" if @http_log[:params]

  HttpLog.call(
    method: @http_log[:method],
    url: url,
    request_body: @http_log[:body],
    request_headers: @http_log[:headers],
    response_code: @return_code,
    response_body: @http_log[:response_body],
    response_headers: @http_log[:response_headers].map { |header| header.split(/:\s/) }.to_h,
    benchmark: bm,
    encoding: @http_log[:encoding],
    content_type: @http_log[:content_type],
    mask_body: HttpLog.masked_body_url?(@http_log[:url])
  )
  return_code
end