1

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

Crollster
  • 2,751
  • 2
  • 23
  • 34
Nick Urban
  • 3,568
  • 2
  • 22
  • 36

1 Answers1

1

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.

sarnold
  • 102,305
  • 22
  • 181
  • 238
  • Thanks. That seems obvious in retrospect! I must have been distracted by the ActiveSupport::Notifications.instrument call. It leaves open the question of what .instrument actually does with the block, however. Is it executed immediately? – Nick Urban Nov 09 '11 at 01:43