Class: WebMock::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/webmock/response.rb

Direct Known Subclasses

DynamicResponse, RackResponse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Response

Returns a new instance of Response.



16
17
18
19
20
21
22
23
24
25
# File 'lib/webmock/response.rb', line 16

def initialize(options = {})
  case options
  when IO, StringIO
    self.options = read_raw_response(options)
  when String
    self.options = read_raw_response(StringIO.new(options))
  else
    self.options = options
  end
end

Instance Method Details

#==(other) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/webmock/response.rb', line 95

def ==(other)
  self.body == other.body &&
    self.headers === other.headers &&
    self.status == other.status &&
    self.exception == other.exception &&
    self.should_timeout == other.should_timeout
end

#bodyObject



38
39
40
# File 'lib/webmock/response.rb', line 38

def body
  @body || ''
end

#body=(body) ⇒ Object



42
43
44
45
46
# File 'lib/webmock/response.rb', line 42

def body=(body)
  @body = body
  assert_valid_body!
  stringify_body!
end

#evaluate(request_signature) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/webmock/response.rb', line 86

def evaluate(request_signature)
  self.body = @body.call(request_signature) if @body.is_a?(Proc)
  self.headers = @headers.call(request_signature) if @headers.is_a?(Proc)
  self.status = @status.call(request_signature) if @status.is_a?(Proc)
  @should_timeout = @should_timeout.call(request_signature) if @should_timeout.is_a?(Proc)
  @exception = @exception.call(request_signature) if @exception.is_a?(Proc)
  self
end

#exceptionObject



56
57
58
# File 'lib/webmock/response.rb', line 56

def exception
  @exception
end

#exception=(exception) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/webmock/response.rb', line 60

def exception=(exception)
  @exception = case exception
  when String then StandardError.new(exception)
  when Class then exception.new('Exception from WebMock')
  when Exception then exception
  end
end

#headersObject



27
28
29
# File 'lib/webmock/response.rb', line 27

def headers
  @headers
end

#headers=(headers) ⇒ Object



31
32
33
34
35
36
# File 'lib/webmock/response.rb', line 31

def headers=(headers)
  @headers = headers
  if @headers && !@headers.is_a?(Proc)
    @headers = Util::Headers.normalize_headers(@headers)
  end
end

#options=(options) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/webmock/response.rb', line 76

def options=(options)
  options = WebMock::Util::HashKeysStringifier.stringify_keys!(options)
  HashValidator.new(options).validate_keys('headers', 'status', 'body', 'exception', 'should_timeout')
  self.headers = options['headers']
  self.status = options['status']
  self.body = options['body']
  self.exception = options['exception']
  @should_timeout = options['should_timeout']
end

#raise_error_if_anyObject

Raises:



68
69
70
# File 'lib/webmock/response.rb', line 68

def raise_error_if_any
  raise @exception if @exception
end

#should_timeoutObject



72
73
74
# File 'lib/webmock/response.rb', line 72

def should_timeout
  @should_timeout == true
end

#statusObject



48
49
50
# File 'lib/webmock/response.rb', line 48

def status
  @status || [200, ""]
end

#status=(status) ⇒ Object



52
53
54
# File 'lib/webmock/response.rb', line 52

def status=(status)
  @status = status.is_a?(Integer) ? [status, ""] : status
end