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
# File 'lib/webmock/response.rb', line 16

def initialize(options = {})
  if options.is_a?(IO) || options.is_a?(String)
    self.options = read_raw_response(options)
  else
    self.options = options
  end
end

Instance Method Details

#==(other) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/webmock/response.rb', line 92

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



35
36
37
# File 'lib/webmock/response.rb', line 35

def body
  @body || ''
end

#body=(body) ⇒ Object



39
40
41
42
43
# File 'lib/webmock/response.rb', line 39

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

#evaluate(request_signature) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/webmock/response.rb', line 83

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



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

def exception
  @exception
end

#exception=(exception) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/webmock/response.rb', line 57

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



24
25
26
# File 'lib/webmock/response.rb', line 24

def headers
  @headers
end

#headers=(headers) ⇒ Object



28
29
30
31
32
33
# File 'lib/webmock/response.rb', line 28

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

#options=(options) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/webmock/response.rb', line 73

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:



65
66
67
# File 'lib/webmock/response.rb', line 65

def raise_error_if_any
  raise @exception if @exception
end

#should_timeoutObject



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

def should_timeout
  @should_timeout == true
end

#statusObject



45
46
47
# File 'lib/webmock/response.rb', line 45

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

#status=(status) ⇒ Object



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

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