Class: RestClient::Rack::Compatibility

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

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Compatibility

Returns a new instance of Compatibility.



7
8
9
# File 'lib/restclient/components.rb', line 7

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/restclient/components.rb', line 11

def call(env)
  status, header, body = @app.call(env)
  net_http_response = RestClient::MockNetHTTPResponse.new(body, status, header)
  content = ""
  net_http_response.body.each{|line| content << line}
  request = env['restclient.hash'][:request]
  response = case RestClient::Response.method(:create).arity
  when 4 then RestClient::Response.create(content, net_http_response, request, self)
  else        RestClient::Response.create(content, net_http_response, request)
  end
  if block = env['restclient.hash'][:block]
    block.call(response)
    # only raise error if response is not successful
  elsif !(200...300).include?(response.code) && e = env['restclient.hash'][:error]
    raise e
  else
    response
  end
end