Created
May 4, 2012 19:28
-
-
Save sfaxon/2597181 to your computer and use it in GitHub Desktop.
ActiveResource faraday middleware access
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/Rakefile b/Rakefile | |
index 2e18dd9..d4e6157 100755 | |
--- a/Rakefile | |
+++ b/Rakefile | |
@@ -25,6 +25,13 @@ namespace :test do | |
end | |
end | |
+namespace :test do | |
+ task :single do | |
+ puts "ruby -w -Ilib:test test/cases/#{ENV['FILE']}" | |
+ system "ruby -w -Ilib:test test/cases/#{ENV['FILE']}" | |
+ end | |
+end | |
+ | |
spec = eval(File.read('activeresource.gemspec')) | |
Gem::PackageTask.new(spec) do |p| | |
diff --git a/lib/active_resource/base.rb b/lib/active_resource/base.rb | |
index 3a8cfae..146f2f2 100644 | |
--- a/lib/active_resource/base.rb | |
+++ b/lib/active_resource/base.rb | |
@@ -413,6 +413,40 @@ module ActiveResource | |
@known_attributes ||= [] | |
end | |
+ def middleware | |
+ if connection.builder.locked? | |
+ connection(true).builder | |
+ else | |
+ connection.builder | |
+ end | |
+ end | |
+ | |
+ # An instance of ActiveResource::Connection that is the base \connection to the remote service. | |
+ # The +refresh+ parameter toggles whether or not the \connection is refreshed at every request | |
+ # or not (defaults to <tt>false</tt>). | |
+ def connection(refresh = false) | |
+ if defined?(@connection) || superclass == Object | |
+ if refresh || @connection.nil? | |
+ @connection = Faraday.new(site) do |builder| | |
+ # Fill in other options here on builder if possible or use a hash | |
+ # in the args to Faraday.new | |
+ # | |
+ # @connection.proxy = proxy if proxy | |
+ # @connection.user = user if user | |
+ # @connection.password = password if password | |
+ # @connection.auth_type = auth_type if auth_type | |
+ # @connection.timeout = timeout if timeout | |
+ # @connection.ssl_options = ssl_options if ssl_options | |
+ builder.use(ActiveResource::Response::RaiseError) | |
+ builder.adapter adapter, *adapter_args, &adapter_block | |
+ end | |
+ end | |
+ @connection | |
+ else | |
+ superclass.connection | |
+ end | |
+ end | |
+ | |
# Set the adapter to use. | |
# | |
# TODO finish docs. | |
@@ -420,21 +454,24 @@ module ActiveResource | |
# TODO there is a better way to do this.... | |
# | |
def set_adapter(adapter, *args, &block) | |
- @@adapter = adapter | |
- @@adapter_args = args | |
- @@adapter_block = block | |
+ @adapter = adapter | |
+ @adapter_args = args | |
+ @adapter_block = block | |
end | |
def adapter | |
- @@adapter ||= :net_http | |
+ @adapter ||= :net_http | |
+ end | |
+ def adapter=(adapter) | |
+ @adapter = adapter | |
end | |
def adapter_args | |
- @@adapter_args ||= nil | |
+ @adapter_args ||= nil | |
end | |
def adapter_block | |
- @@adapter_block ||= nil | |
+ @adapter_block ||= nil | |
end | |
# Gets the URI of the REST resources to map for this class. The site variable is required for | |
@@ -595,32 +632,6 @@ module ActiveResource | |
end | |
end | |
- # An instance of ActiveResource::Connection that is the base \connection to the remote service. | |
- # The +refresh+ parameter toggles whether or not the \connection is refreshed at every request | |
- # or not (defaults to <tt>false</tt>). | |
- def connection(refresh = false) | |
- if defined?(@connection) || superclass == Object | |
- if refresh || @connection.nil? | |
- @connection = Faraday.new(site) do |builder| | |
- # Fill in other options here on builder if possible or use a hash | |
- # in the args to Faraday.new | |
- # | |
- # @connection.proxy = proxy if proxy | |
- # @connection.user = user if user | |
- # @connection.password = password if password | |
- # @connection.auth_type = auth_type if auth_type | |
- # @connection.timeout = timeout if timeout | |
- # @connection.ssl_options = ssl_options if ssl_options | |
- builder.use(ActiveResource::Response::RaiseError) | |
- builder.adapter adapter, *adapter_args, &adapter_block | |
- end | |
- end | |
- @connection | |
- else | |
- superclass.connection | |
- end | |
- end | |
- | |
def headers | |
@headers ||= {} | |
diff --git a/test/singleton_test.rb b/test/singleton_test.rb | |
index cfed9c8..5cb9fec 100644 | |
--- a/test/singleton_test.rb | |
+++ b/test/singleton_test.rb | |
@@ -1,3 +1,4 @@ | |
+require 'debugger' | |
require 'abstract_unit' | |
require 'fixtures/weather' | |
require 'fixtures/inventory' | |
@@ -5,17 +6,20 @@ require 'fixtures/inventory' | |
class SingletonTest < ActiveSupport::TestCase | |
def setup_weather | |
weather = { :status => 'Sunny', :temperature => 67 } | |
- Weather.set_adapter(:test) do |stub| | |
- stub.get ('/weather.json') {[200, {}, weather.to_json]} | |
- stub.get ('/weather.json?degrees=fahrenheit') {[200, {}, weather.merge(:temperature => 100).to_json]} | |
- stub.post ('/weather.json') {[201, {'Location' => '/weather.json'}, weather.to_json]} | |
- stub.delete ('/weather.json') {[200, {}, nil]} | |
- stub.put ('/weather.json') {[204, {}, nil]} | |
+ | |
+ Weather.adapter = :test | |
+ Weather.middleware.swap(Faraday::Adapter::Test, Faraday::Adapter::Test) do |stub| | |
+ stub.get('/weather.json?degrees=fahrenheit') {[200, {}, weather.merge(:temperature => 100).to_json]} | |
+ stub.get('/weather.json') {[200, {}, weather.to_json]} | |
+ stub.post('/weather.json') {[201, {'Location' => '/weather.json'}, weather.to_json]} | |
+ stub.delete('/weather.json') {[200, {}, nil]} | |
+ stub.put('/weather.json') {[204, {}, nil]} | |
end | |
end | |
def setup_weather_not_found | |
- Weather.set_adapter(:test) do |stub| | |
+ Weather.adapter = :test | |
+ Weather.middleware.swap(Faraday::Adapter::Test, Faraday::Adapter::Test) do |stub| | |
stub.get ('/weather.json') {[404, {}, nil]} | |
end | |
end | |
@@ -23,7 +27,8 @@ class SingletonTest < ActiveSupport::TestCase | |
def setup_inventory | |
inventory = {:status => 'Sold Out', :total => 10, :used => 10}.to_json | |
- Inventory.set_adapter(:test) do |stub| | |
+ Inventory.adapter = :test | |
+ Inventory.middleware.swap(Faraday::Adapter::Test, Faraday::Adapter::Test) do |stub| | |
stub.get ('/products/5/inventory.json') { [200, {}, inventory] } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment