connection_ext.rb 801 B

1234567891011121314151617181920212223242526272829303132
  1. module ActiveResource
  2. class Connection
  3. attr_reader :response
  4. module ResponseCapture
  5. def handle_response(response)
  6. @response = super
  7. end
  8. end
  9. module RequestNotification
  10. def request(method, path, *arguments)
  11. super.tap do |response|
  12. notify_about_request(response, arguments)
  13. end
  14. rescue => e
  15. notify_about_request(e.response, arguments) if e.respond_to?(:response)
  16. raise
  17. end
  18. def notify_about_request(response, arguments)
  19. ActiveSupport::Notifications.instrument("request.active_resource_detailed") do |payload|
  20. payload[:response] = response
  21. payload[:data] = arguments
  22. end
  23. end
  24. end
  25. prepend ResponseCapture
  26. prepend RequestNotification
  27. end
  28. end