Sha256: 1837378632f47379a8a73b2b2f403b56e5495d167be93d8001fe8f80f842f351

Contents?: true

Size: 1.73 KB

Versions: 3

Compression:

Stored size: 1.73 KB

Contents

if defined?(Ethon)
  module Ethon
    class Easy

      attr_accessor :action_name

      module Http
        alias_method :orig_http_request, :http_request
        def http_request(url, action_name, options = {})
          @action_name = action_name # remember this for compact logging
          if HttpLog.url_approved?(url)
            HttpLog.log_request(action_name, url)
            HttpLog.log_headers(options[:headers])
            HttpLog.log_data(options[:body]) #if action_name == :post
          end

          orig_http_request(url, action_name, options)
        end
      end

      module Operations
        alias_method :orig_perform, :perform
        def perform
          return orig_perform unless HttpLog.url_approved?(url)

          response_code = nil
          bm = Benchmark.realtime do
            reponse_code = orig_perform
          end

          # Not sure where the actual status code is stored - so let's
          # extract it from the response header.
          status   = response_headers.scan(/HTTP\/... (\d{3})/).flatten.first
          encoding = response_headers.scan(/Content-Encoding: (\S+)/).flatten.first
          content_type = response_headers.scan(/Content-Type: (\S+(; charset=\S+)?)/).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/)[1..-1]

          HttpLog.log_compact(@action_name, @url, @return_code, bm)
          HttpLog.log_status(status)
          HttpLog.log_benchmark(bm)
          HttpLog.log_headers(headers)
          HttpLog.log_body(response_body, encoding, content_type)
          return_code
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
httplog-1.0.2 lib/httplog/adapters/ethon.rb
httplog-1.0.1 lib/httplog/adapters/ethon.rb
httplog-1.0.0 lib/httplog/adapters/ethon.rb