Class: Billy::StubHandler

Inherits:
Object
  • Object
show all
Includes:
Handler
Defined in:
lib/billy/handlers/stub_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#stubsObject



41
42
43
# File 'lib/billy/handlers/stub_handler.rb', line 41

def stubs
  @stubs ||= []
end

Instance Method Details

#handle_request(method, url, headers, body) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/billy/handlers/stub_handler.rb', line 8

def handle_request(method, url, headers, body)
  if handles_request?(method, url, headers, body)
    if (stub = find_stub(method, url))
      query_string = Addressable::URI.parse(url).query || ''
      params = CGI.parse(query_string)
      stub.call(method, url, params, headers, body).tap do |response|
        Billy.log(:info, "puffing-billy: STUB #{method} for '#{url}'")
        return { status: response[0], headers: response[1], content: response[2] }
      end
    end
  end

  nil
end

#handles_request?(method, url, _headers, _body) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/billy/handlers/stub_handler.rb', line 23

def handles_request?(method, url, _headers, _body)
  !find_stub(method, url).nil?
end

#resetObject



27
28
29
# File 'lib/billy/handlers/stub_handler.rb', line 27

def reset
  self.stubs = []
end

#stub(url, options = {}) ⇒ Object



31
32
33
34
35
# File 'lib/billy/handlers/stub_handler.rb', line 31

def stub(url, options = {})
  new_stub = ProxyRequestStub.new(url, options)
  stubs.unshift new_stub
  new_stub
end

#unstub(stub) ⇒ Object



37
38
39
# File 'lib/billy/handlers/stub_handler.rb', line 37

def unstub(stub)
  stubs.delete stub
end