Module: Supergood::Vendor::HTTPrb

Defined in:
lib/supergood/vendors/http.rb

Class Method Summary collapse

Class Method Details

.existing_patch?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/supergood/vendors/http.rb', line 53

def self.existing_patch?
  defined?(HTTP::Client) && HTTP::Client.method_defined?(:original_perform)
end

.patchObject



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
# File 'lib/supergood/vendors/http.rb', line 4

def self.patch
  if !self.existing_patch?

    block = lambda do |x|
      alias original_perform perform
      def perform(original_request_payload, original_options)
        request = {
          headers: original_request_payload.headers.to_hash,
          method: original_request_payload.verb.upcase.to_s,
          body: Supergood::Utils.safe_parse_json(original_request_payload.body.source),
          url: original_request_payload.uri.to_s,
          path: original_request_payload.uri.path,
          search: original_request_payload.uri.query,
          domain: Supergood::Utils.get_host_without_www(original_request_payload.uri.host)
        }
        Supergood.intercept(request) do
          original_response = original_perform(original_request_payload, original_options)
          status, statusText = original_response.status.to_s.split(' ')
          {
            headers: original_response.headers.to_hash,
            status: status,
            statusText: statusText,
            body: Supergood::Utils.safe_parse_json(original_response),
            original_response: original_response
          }
        end
      end
    end

    if defined?(HTTP::Client)
      HTTP::Client.class_eval(&block)
    end

  end
end

.unpatchObject



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/supergood/vendors/http.rb', line 40

def self.unpatch
  if self.existing_patch?
    block = lambda do |x|
      alias perform original_perform
    end

    if defined?(HTTP::Client)
      HTTP::Client.class_eval(&block)
      HTTP::Client.undef_method :original_perform
    end
  end
end