Search code examples
ruby-on-railsrubyactiveresource

Where and how does Active Resource execute its HTTP requests?


I can't find the place where Active Resource initiates its connections. I was expecting Connection.request to call into a networking library, but instead it merely calls into ActiveSupport::Notifications.instrument, which appears to be some kind of messaging service.

Does anybody know how this is working? I couldn't find the code that was listening for the message. ActiveSupport::Notifications is completely new to me, so perhaps there's an obvious place that the listener would be located.

  def request(method, path, *arguments)
    result = ActiveSupport::Notifications.instrument("request.active_resource") do |payload|
      payload[:method]      = method
      payload[:request_uri] = "#{site.scheme}://#{site.host}:#{site.port}#{path}"
      payload[:result]      = http.send(method, path, *arguments)
    end

The method definition is here on GitHub


Solution

  • I believe the answer lies in the http.send() call that is assigned into payload[:result].

    From further in the file:

      # Creates new Net::HTTP instance for communication with the
      # remote service and resources.
      def http
        configure_http(new_http)
      end
    
      def new_http
        if @proxy
          Net::HTTP.new(@site.host, @site.port, @proxy.host, @proxy.port, @proxy.user, @proxy.password)
        else
          Net::HTTP.new(@site.host, @site.port)
        end
      end
    
      def configure_http(http)
        http = apply_ssl_options(http)
    
        # Net::HTTP timeouts default to 60 seconds.
        if @timeout
          http.open_timeout = @timeout
          http.read_timeout = @timeout
        end
    
        http
      end
    

    The Net::HTTP comes from the require 'net/https' from line 4.