Module: NetRecorder::NetHTTP

Defined in:
lib/http.rb

Class Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

:nodoc:



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

def self.extended(base) #:nodoc:
  base.class_eval do
    alias :alias_for_request :request
    @@fakes = fakes_cache || []

    # request is overridden and the request and response are stored as a hash that can be written to a cache file
    def request(req, body = nil, &block)
      response = alias_for_request(req, body)

      unless NetRecorder.recording?
        yield response and return if block
        return response
      end
      
      scope = NetRecorder.scope || 'global'
      path = "http://#{req.bauth if req.bauth}#{req['host']}#{req.path}"
      
      existing_fake = @@fakes.detect do |fake| 
        fake[Fake::REQUEST] == path && 
        fake[Fake::RESPONSE][scope] && 
        fake[Fake::RESPONSE][scope][:method] == req.method
      end
      
      if existing_fake
        existing_fake[Fake::RESPONSE][scope][:response] << {:response => response}
      else
        @@fakes << [path, {scope => {:method => req.method, :response => [{:response => response}]}}]
      end

      yield response if block
      response
    end
    
    # returns the fakes hash
    def self.fakes
      @@fakes
    end
    
    def self.clear_netrecorder_cache! #:nodoc:
      @@fakes = []
    end
  end
end