code style for Adapter::Rack

This commit is contained in:
Mislav Marohnić 2012-04-15 12:26:04 +02:00
parent 1dcfad746d
commit 96ba8eb2d7

View File

@ -1,37 +1,34 @@
require 'timeout' require 'timeout'
module Faraday module Faraday
class Adapter class Adapter
# This adapter wraps around a rack app, similar to how Rack::Test # Sends requests to a Rack app.
# allows you to test rack apps.
# #
# @example # Examples
# class RackApp
# def call(env)
# [200, {'Content-Type' => 'text/html'}, ["hello world"]]
# end
# end
# #
# Faraday.new do |builder| # class MyRackApp
# builder.adapter :rack, RackApp # def call(env)
# end # [200, {'Content-Type' => 'text/html'}, ["hello world"]]
# end
# end
#
# Faraday.new do |conn|
# conn.adapter :rack, MyRackApp
# end
class Rack < Faraday::Adapter class Rack < Faraday::Adapter
dependency 'rack/test' dependency 'rack/test'
begin begin
if RUBY_VERSION < '1.9' require 'system_timer' if RUBY_VERSION < '1.9'
require 'system_timer'
end
rescue LoadError rescue LoadError
$stderr.puts "Faraday: you may want to install system_timer for reliable timeouts" warn "Faraday: you may want to install system_timer for reliable timeouts"
ensure ensure
SystemTimer = Timeout unless defined? ::SystemTimer SystemTimer = Timeout unless defined? ::SystemTimer
end end
# @param app [Faraday::Middleware] def initialize(faraday_app, rack_app)
# @param rack rack application to wrap super(faraday_app)
def initialize(app, rack) mock_session = ::Rack::MockSession.new(rack_app)
super(app)
mock_session = ::Rack::MockSession.new(rack)
@session = ::Rack::Test::Session.new(mock_session) @session = ::Rack::Test::Session.new(mock_session)
end end
@ -56,14 +53,14 @@ module Faraday
else else
execute_request(env, rack_env) execute_request(env, rack_env)
end end
save_response(env, response.status, response.body, response.headers) save_response(env, response.status, response.body, response.headers)
@app.call env @app.call env
end end
# @private
def execute_request(env, rack_env) def execute_request(env, rack_env)
@session.request(env[:url].to_s, rack_env) @session.request(env[:url].to_s, rack_env)
end end
end end
end end
end end