アクティブなリソースfind()
呼び出しが通常の HTTP 200 ではなく HTTP 206 を返したことをどのように検出できますか?
ActiveResource が HTTP 3xx-5xx 応答コードに対してさまざまな例外をスローすることは知っていますが、受け取った 200 レベルの応答コードをどのように把握できますか?
アクティブなリソースfind()
呼び出しが通常の HTTP 200 ではなく HTTP 206 を返したことをどのように検出できますか?
ActiveResource が HTTP 3xx-5xx 応答コードに対してさまざまな例外をスローすることは知っていますが、受け取った 200 レベルの応答コードをどのように把握できますか?
スレッドの最後の応答を取得する方法については、Active Resource 応答、それらを取得する方法を参照してください。その後、必要に応じて応答コードをテストできます。
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