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.



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

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



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

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



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

def body
  @body || ''
end

#body=(body) ⇒ Object



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

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

#evaluate(request_signature) ⇒ Object



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

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



58
59
60
# File 'lib/webmock/response.rb', line 58

def exception
  @exception
end

#exception=(exception) ⇒ Object



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

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



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

def headers
  @headers
end

#headers=(headers) ⇒ Object



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

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

#options=(options) ⇒ Object



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

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:



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

def raise_error_if_any
  raise @exception if @exception
end

#should_timeoutObject



74
75
76
# File 'lib/webmock/response.rb', line 74

def should_timeout
  @should_timeout == true
end

#statusObject



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

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

#status=(status) ⇒ Object



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

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