File: http_client.rb

package info (click to toggle)
ruby-samuel 0.3.3-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, trixie
  • size: 172 kB
  • sloc: ruby: 735; makefile: 2
file content (51 lines) | stat: -rw-r--r-- 1,507 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
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
43
44
45
46
47
48
49
50
51
module Samuel
  module DriverPatches
    module HTTPClient
      def self.included(klass)
        methods_to_wrap = %w(initialize do_get_block do_get_stream)
        methods_to_wrap.each do |method|
          klass.send(:alias_method, "#{method}_without_samuel", method)
          klass.send(:alias_method, method, "#{method}_with_samuel")
        end
      end

      def initialize_with_samuel(*args)
        initialize_without_samuel(*args)
        @request_filter << LoggingFilter.new(self)
      end

      def do_get_block_with_samuel(req, proxy, conn, &block)
        begin
          do_get_block_without_samuel(req, proxy, conn, &block)
        rescue Exception => e
          Samuel::Diary.record_response(self, req, e)
          raise
        end
      end

      def do_get_stream_with_samuel(req, proxy, conn)
        begin
          do_get_stream_without_samuel(req, proxy, conn)
        rescue Exception => e
          Samuel::Diary.record_response(self, req, e)
          raise
        end
      end

      class LoggingFilter
        def initialize(http_client_instance)
          @http_client_instance = http_client_instance
        end

        def filter_request(request)
          Samuel::Diary.record_request(@http_client_instance, request)
        end

        def filter_response(request, response)
          Samuel::Diary.record_response(@http_client_instance, request, response)
          nil # this returns command symbols like :retry, etc.
        end
      end
    end
  end
end