rubycoloredglasses


I'm Jason, a web application developer in East Tennessee.


Troubleshooting ActiveResource Requests

I’m currently working on an app which integrates with the HighRise API using the Highrise Ruby wrapper gem. The classes defined by the gem rely on ActiveResource, the Ruby library included with Rails for interfacing with RESTful resources like the HighRise API.

Sometimes I’m not sure if the requests being made via the commands I’m using are making the right calls on the HighRise API.

I found this thread on StackOverflow: How do I view the HTTP response to an ActiveResource request?. The solution I found helpful was this one on overriding ActiveResource::Connection so that it outputs the debug output. This worked for me even with Rails 3.1.

Just in case this article with the code disappears, here is the initializer code you can use to view the HTTP session data from the console. I’ve added an if statement to make sure the HTTP data is only outputted to the standard output in development mode, for security purposes, as well as requires the environment variable ‘HTTPDEBUG’.

# /config/initializers/connection.rb
class ActiveResource::Connection

  # Creates new Net::HTTP instance for communication with
  # remote service and resources.
  def http
    http = Net::HTTP.new(@site.host, @site.port)
    http.use_ssl = @site.is_a?(URI::HTTPS)
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl
    http.read_timeout = @timeout if @timeout

    # Here's the addition that allows you to see the output
    if Rails.env == 'development' && ENV['HTTPDEBUG']
      http.set_debug_output $stderr
    end

    return http
  end
end

You can open the Rails console using ‘HTTPDEBUG=true rails c’ to activate the console with debugging output displayed in the console mode.

comments powered by Disqus