Question

How can I detect that an active resource find() call returned HTTP 206 instead of a typical HTTP 200?

I know ActiveResource throws various exceptions for HTTP 3xx-5xx response codes, but how can you figure out what 200-level response code you received?

Was it helpful?

Solution

See Active Resource responses, how to get them for how to get the last response for the thread. You can then test the response code as needed:

class MyConn < ActiveResource::Connection
  def handle_response(resp)
    # Store in thread (thanks fivell for the tip).
    # Use a symbol to avoid generating multiple string instances.
    Thread.current[:active_resource_connection_last_response] = resp
    super
  end
  # this is only a convenience method. You can access this directly from the current thread.
  def last_resp
    Thread.current[:active_resource_connection_last_response]
  end
end

class MyResource < ActiveResource::Base
  class << self
    attr_writer :connection
  end
end

myconn = MyConn.new MyResource.connection.site
MyResource.connection = myconn  # replace with our enhanced version
object = MyResource.find(id)
response_code = MyResource.last_resp.code
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top