aboutsummaryrefslogtreecommitdiffstats
path: root/activeresource
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2010-01-13 22:59:27 +0100
committerJosé Valim <jose.valim@gmail.com>2010-01-14 01:07:03 +0100
commit116df09c3e4a57a6b0a57a5134c451687113ece1 (patch)
tree066f99faac94657761377003516e64e7a6f926ac /activeresource
parent7c3573f32757e9c4c6b6140499a3e7dfe2d335b1 (diff)
downloadrails-116df09c3e4a57a6b0a57a5134c451687113ece1.tar.gz
rails-116df09c3e4a57a6b0a57a5134c451687113ece1.tar.bz2
rails-116df09c3e4a57a6b0a57a5134c451687113ece1.zip
Add notifications to ActiveResource.
Diffstat (limited to 'activeresource')
-rw-r--r--activeresource/lib/active_resource/connection.rb13
-rw-r--r--activeresource/lib/active_resource/railtie.rb11
-rw-r--r--activeresource/lib/active_resource/railties/subscriber.rb15
-rw-r--r--activeresource/test/cases/subscriber_test.rb39
4 files changed, 68 insertions, 10 deletions
diff --git a/activeresource/lib/active_resource/connection.rb b/activeresource/lib/active_resource/connection.rb
index 193be89a82..ee700700ae 100644
--- a/activeresource/lib/active_resource/connection.rb
+++ b/activeresource/lib/active_resource/connection.rb
@@ -103,14 +103,13 @@ module ActiveResource
with_auth { request(:head, path, build_request_headers(headers, :head, self.site.merge(path))) }
end
-
private
# Makes a request to the remote service.
def request(method, path, *arguments)
- logger.info "#{method.to_s.upcase} #{site.scheme}://#{site.host}:#{site.port}#{path}" if logger
- result = nil
- ms = Benchmark.ms { result = http.send(method, path, *arguments) }
- logger.info "--> %d %s (%d %.0fms)" % [result.code, result.message, result.body ? result.body.length : 0, ms] if logger
+ result = ActiveSupport::Notifications.instrument!("active_resource.request",
+ :method => method, :path => path, :site => site) do
+ http.send(method, path, *arguments)
+ end
handle_response(result)
rescue Timeout::Error => e
raise TimeoutError.new(e.message)
@@ -274,10 +273,6 @@ module ActiveResource
{HTTP_FORMAT_HEADER_NAMES[http_method] => format.mime_type}
end
- def logger #:nodoc:
- Base.logger
- end
-
def legitimize_auth_type(auth_type)
return :basic if auth_type.nil?
auth_type = auth_type.to_sym
diff --git a/activeresource/lib/active_resource/railtie.rb b/activeresource/lib/active_resource/railtie.rb
index 4f264c82b8..1b9307d472 100644
--- a/activeresource/lib/active_resource/railtie.rb
+++ b/activeresource/lib/active_resource/railtie.rb
@@ -1,2 +1,11 @@
require "active_resource"
-require "rails" \ No newline at end of file
+require "rails"
+
+module ActiveResource
+ class Railtie < Rails::Railtie
+ plugin_name :active_resource
+
+ require "active_resource/railties/subscriber"
+ subscriber ActiveResource::Railties::Subscriber.new
+ end
+end \ No newline at end of file
diff --git a/activeresource/lib/active_resource/railties/subscriber.rb b/activeresource/lib/active_resource/railties/subscriber.rb
new file mode 100644
index 0000000000..42c108d702
--- /dev/null
+++ b/activeresource/lib/active_resource/railties/subscriber.rb
@@ -0,0 +1,15 @@
+module ActiveResource
+ module Railties
+ class Subscriber < Rails::Subscriber
+ def request(event)
+ result, site = event.payload[:result], event.payload[:site]
+ info "#{event.payload[:method].to_s.upcase} #{site.scheme}://#{site.host}:#{site.port}#{event.payload[:path]}"
+ info "--> %d %s %d (%.1fms)" % [result.code, result.message, result.body.to_s.length, event.duration]
+ end
+
+ def logger
+ ActiveResource::Base.logger
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/activeresource/test/cases/subscriber_test.rb b/activeresource/test/cases/subscriber_test.rb
new file mode 100644
index 0000000000..7100b02872
--- /dev/null
+++ b/activeresource/test/cases/subscriber_test.rb
@@ -0,0 +1,39 @@
+require "abstract_unit"
+require "fixtures/person"
+require "rails/subscriber/test_helper"
+require "active_resource/railties/subscriber"
+
+module SubscriberTest
+ Rails::Subscriber.add(:active_resource, ActiveResource::Railties::Subscriber.new)
+
+ def setup
+ @matz = { :id => 1, :name => 'Matz' }.to_xml(:root => 'person')
+ ActiveResource::HttpMock.respond_to do |mock|
+ mock.get "/people/1.xml", {}, @matz
+ end
+
+ super
+ end
+
+ def set_logger(logger)
+ ActiveResource::Base.logger = logger
+ end
+
+ def test_request_notification
+ matz = Person.find(1)
+ wait
+ assert_equal 2, @logger.logged(:info).size
+ assert_equal "GET http://somewhere.else:80/people/1.xml", @logger.logged(:info)[0]
+ assert_match /\-\-\> 200 200 106/, @logger.logged(:info)[1]
+ end
+
+ class SyncSubscriberTest < ActiveSupport::TestCase
+ include Rails::Subscriber::SyncTestHelper
+ include SubscriberTest
+ end
+
+ class AsyncSubscriberTest < ActiveSupport::TestCase
+ include Rails::Subscriber::AsyncTestHelper
+ include SubscriberTest
+ end
+end \ No newline at end of file