Permalink
Browse files

ability to specify a custom Connection subclass in `build`

  • Loading branch information...
1 parent bddd8f9 commit 9e677079c03d9654720ef3de9278d948004372f5 @mislav committed Jun 12, 2011
Showing with 37 additions and 1 deletion.
  1. +9 −1 lib/faraday_stack.rb
  2. +28 −0 test/factory_test.rb
View
@@ -28,7 +28,15 @@ class << self
end
def self.build(url = nil, options = {})
- Faraday::Connection.new(url, options) do |builder|
+ klass = nil
+ if url.is_a?(Hash) then options = url.dup
+ elsif url.is_a?(Class) then klass = url
+ else options = options.merge(:url => url)
+ end
+
+ klass ||= options.delete(:class) || Faraday::Connection
+
+ klass.new(options) do |builder|
builder.request :url_encoded
builder.request :json
yield builder if block_given?
View
@@ -0,0 +1,28 @@
+require 'test_helper'
+require 'forwardable'
+
+class FactoryTest < Test::Unit::TestCase
+ extend Forwardable
+ def_delegator FaradayStack, :build
+
+ class CustomConnection < Faraday::Connection
+ end
+
+ def test_default_connection
+ assert_instance_of Faraday::Connection, FaradayStack.default_connection
+ end
+
+ def test_build_subclass
+ assert_instance_of CustomConnection, build(CustomConnection)
+ end
+
+ def test_build_url
+ conn = FaradayStack.build('http://example.com')
+ assert_equal 'example.com', conn.host
+ end
+
+ def test_build_url_in_options
+ conn = FaradayStack.build(:url => 'http://example.com')
+ assert_equal 'example.com', conn.host
+ end
+end

0 comments on commit 9e67707

Please sign in to comment.