Add shortcut methods on Faraday for using a default Faraday::Connection instance

This commit is contained in:
rick 2010-04-27 10:21:57 -04:00
parent f033ad4730
commit 3cbff597d3
3 changed files with 86 additions and 21 deletions

View File

@ -3,10 +3,60 @@ require 'rack/utils'
module Faraday
class << self
attr_accessor :default_adapter
attr_writer :default_connection
end
self.default_adapter = :net_http
def self.default_connection
@default_connection ||= Connection.new
end
# use the method signature from Faraday::Connection
def self.build(options = {}, &block)
default_connection.build(options, &block)
end
def self.get(url = nil, headers = nil, &block)
default_connection.get(url, headers, &block)
end
def self.post(url = nil, body = nil, headers = nil, &block)
default_connection.post(url, body, headers, &block)
end
def self.put(url = nil, body = nil, headers = nil, &block)
default_connection.put(url, body, headers, &block)
end
def self.head(url = nil, headers = nil, &block)
default_connection.head(url, headers, &block)
end
def self.delete(url = nil, headers = nil, &block)
default_connection.delete(url, headers, &block)
end
def self.in_parallel?
default_connection.in_parallel?
end
def self.in_parallel(manager)
default_connection.in_parallel(manager)
end
def self.proxy(arg = nil)
default_connection.proxy(arg)
end
def self.url_prefix=(url)
default_connection.url_prefix = url
end
def self.path_prefix=(value)
default_connection.path_prefix = value
end
module AutoloadHelper
def register_lookup_modules(mods)
(@lookup_module_index ||= {}).update(mods)

View File

@ -68,19 +68,6 @@ module Faraday
run_request :delete, url, nil, headers, &block
end
def run_request(method, url, body, headers)
if !METHODS.include?(method)
raise ArgumentError, "unknown http method: #{method}"
end
Request.run(self, 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
end
def in_parallel?
!!@parallel_manager
end
@ -109,11 +96,6 @@ module Faraday
end
end
# return the assembled Rack application for this instance.
def to_app
@builder.to_app
end
# Parses the giving url with Addressable::URI and stores the individual
# components in this connection. These components serve as defaults for
# requests made by this connection.
@ -145,6 +127,24 @@ module Faraday
@path_prefix = value
end
# return the assembled Rack application for this instance.
def to_app
@builder.to_app
end
def run_request(method, url, body, headers)
if !METHODS.include?(method)
raise ArgumentError, "unknown http method: #{method}"
end
Request.run(self, 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
end
# Takes a relative url for a request and combines it with the defaults
# set on the connection instance.
#

View File

@ -3,7 +3,7 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'helper'))
if Faraday::TestCase::LIVE_SERVER
module Adapters
class LiveTest < Faraday::TestCase
Faraday::Adapter.all_loaded_constants.each do |adapter|
(Faraday::Adapter.all_loaded_constants + [:default]).each do |adapter|
define_method "test_#{adapter}_GET_retrieves_the_response_body" do
assert_equal 'hello world', create_connection(adapter).get('hello_world').body
end
@ -100,6 +100,7 @@ if Faraday::TestCase::LIVE_SERVER
resp1, resp2 = nil, nil
connection = create_connection(adapter)
adapter = real_adapter_for(adapter)
connection.in_parallel(adapter.setup_parallel_manager) do
resp1 = connection.get('json')
@ -117,8 +118,22 @@ if Faraday::TestCase::LIVE_SERVER
end
def create_connection(adapter)
Faraday::Connection.new LIVE_SERVER do |b|
b.use adapter
if adapter == :default
conn = Faraday.default_connection
conn.url_prefix = LIVE_SERVER
conn
else
Faraday::Connection.new LIVE_SERVER do |b|
b.use adapter
end
end
end
def real_adapter_for(adapter)
if adapter == :default
Faraday::Adapter.lookup_module(Faraday.default_adapter)
else
adapter
end
end
end