refactor how Requests are ran

The Request class is not responsible for running itself anymore.
Its `run` methods (both class and instance) are gone.

The actual running of the request is handled by Connection in the
`run_request` method.
This commit is contained in:
Mislav Marohnić 2011-05-08 16:24:13 -07:00
parent b47fb2922f
commit 7fa58f2009
3 changed files with 18 additions and 18 deletions

View File

@ -187,12 +187,15 @@ module Faraday
raise ArgumentError, "unknown http method: #{method}"
end
Request.run(self, method) do |req|
request = Request.create(method) do |req|
req.url(url) if url
req.headers.update(headers) if headers
req.body = body if body
yield req if block_given?
end
env = request.to_env(self)
self.app.call(env)
end
# Takes a relative url for a request and combines it with the defaults

View File

@ -22,16 +22,18 @@ module Faraday
:url_encoded => :UrlEncoded,
:multipart => :Multipart
def self.run(connection, request_method)
req = create
yield req if block_given?
req.run(connection, request_method)
attr_reader :method
def self.create(request_method)
new(request_method).tap do |request|
yield request if block_given?
end
end
def self.create
req = new(nil, {}, {}, nil)
yield req if block_given?
req
def initialize(request_method)
@method = request_method
self.params = {}
self.headers = {}
end
def url(path, params = {})
@ -63,11 +65,11 @@ module Faraday
# :user - Proxy server username
# :password - Proxy server password
# :ssl - Hash of options for configuring SSL requests.
def to_env_hash(connection, request_method)
def to_env(connection)
env_params = connection.params.merge(params)
env_headers = connection.headers.merge(headers)
{ :method => request_method,
{ :method => method,
:body => body,
:url => connection.build_url(path, env_params),
:request_headers => env_headers,
@ -75,10 +77,5 @@ module Faraday
:request => connection.options.merge(:proxy => connection.proxy),
:ssl => connection.ssl}
end
def run(connection, request_method)
env = to_env_hash(connection, request_method)
connection.app.call(env)
end
end
end

View File

@ -47,10 +47,10 @@ class EnvTest < Faraday::TestCase
end
def env_for(connection)
env_setup = Faraday::Request.create do |req|
env_setup = Faraday::Request.create(:get) do |req|
yield req
end
env_setup.to_env_hash(connection, :get)
env_setup.to_env(connection)
end
end