Class: FakeWeb::Responder

Inherits:
Object
  • Object
show all
Defined in:
lib/fake_web/responder.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, uri, options, times, &block) ⇒ Responder

Returns a new instance of Responder.



6
7
8
9
10
11
12
# File 'lib/fake_web/responder.rb', line 6

def initialize(method, uri, options, times, &block)
  self.method = method
  self.uri = uri
  self.options = options
  self.times = times ? times : 1
  self.response_block = block
end

Instance Attribute Details

#methodObject

Returns the value of attribute method.



4
5
6
# File 'lib/fake_web/responder.rb', line 4

def method
  @method
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/fake_web/responder.rb', line 4

def options
  @options
end

#response_blockObject

Returns the value of attribute response_block.



4
5
6
# File 'lib/fake_web/responder.rb', line 4

def response_block
  @response_block
end

#timesObject

Returns the value of attribute times.



4
5
6
# File 'lib/fake_web/responder.rb', line 4

def times
  @times
end

#uriObject

Returns the value of attribute uri.



4
5
6
# File 'lib/fake_web/responder.rb', line 4

def uri
  @uri
end

Instance Method Details

#response(request) {|response| ... } ⇒ Object

Yields:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fake_web/responder.rb', line 14

def response(request, &block)
  if has_baked_response?
    response = baked_response
  else
    if self.response_block
      code, msg = meta_information
      response = Net::HTTPResponse.send(:response_class, code.to_s).new(uri, code.to_s, msg)
      response.instance_variable_set(:@body, self.response_block.call(params_for(request)))
    else
      code, msg = meta_information
      response = Net::HTTPResponse.send(:response_class, code.to_s).new("1.0", code.to_s, msg)
      response.instance_variable_set(:@body, content)
    end
  end

  response.instance_variable_set(:@read, true)
  response.extend FakeWeb::Response

  optionally_raise(response)

  yield response if block_given?

  response
end