Class: RestClient::MockHTTPResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/cacheability/restclient.rb

Overview

A class that mocks the behaviour of a Net::HTTPResponse class. It is required since RestClient::Response must be initialized with a class that responds to :code and :to_hash.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rack_response) ⇒ MockHTTPResponse

Returns a new instance of MockHTTPResponse.



9
10
11
12
13
14
# File 'lib/cacheability/restclient.rb', line 9

def initialize(rack_response)
  @code, @headers, io = rack_response
  @body = ""
  io.each{|block| @body << block}
  io.close if io.respond_to?(:close)
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



8
9
10
# File 'lib/cacheability/restclient.rb', line 8

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.



8
9
10
# File 'lib/cacheability/restclient.rb', line 8

def code
  @code
end

#headersObject (readonly)

Returns the value of attribute headers.



8
9
10
# File 'lib/cacheability/restclient.rb', line 8

def headers
  @headers
end

Instance Method Details

#to_hashObject



16
17
18
19
20
21
22
# File 'lib/cacheability/restclient.rb', line 16

def to_hash
  @headers.inject({}) {|out, (key, value)|
    # In Net::HTTP, headers values are arrays
    out[key] = value.split(", ")
    out
  }
end