Skip to content

Commit

Permalink
refactor how Requests are ran
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mislav committed May 8, 2011
1 parent b47fb29 commit 7fa58f2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
5 changes: 4 additions & 1 deletion lib/faraday/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,15 @@ def run_request(method, url, body, headers)
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
Expand Down
27 changes: 12 additions & 15 deletions lib/faraday/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ class Request < Struct.new(:path, :params, :headers, :body)
: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 = {})
Expand Down Expand Up @@ -63,22 +65,17 @@ def []=(key, value)
# :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,
:parallel_manager => connection.parallel_manager,
: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
4 changes: 2 additions & 2 deletions test/env_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ def test_request_create_stores_proxy_options
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

Expand Down

0 comments on commit 7fa58f2

Please sign in to comment.