2012-11-12 2 views
3

활성 리소스 find() 호출이 일반적인 HTTP 200 대신 HTTP 206을 반환했음을 어떻게 감지합니까?Rails ActiveResource가 HTTP 206 응답 코드를 감지했습니다.

저는 ActiveResource가 HTTP 3xx-5xx 응답 코드에 대한 다양한 예외를 throw하지만, 어떤 200 수준 응답 코드를 수신했는지 파악할 수 있습니까?

+0

https://github.com/Fivell/activeresource-response 이제이 밖으로 노력이 보석 – Fivell

답변

4

스레드에 대한 마지막 응답을받는 방법은 Active Resource responses, how to get them을 참조하십시오. 필요에 따라 다음 응답 코드를 테스트 할 수 있습니다

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 
+0

을 시도합니다. 이것이 간단한 응답 코드를 얻는 데는 실망합니다. : – Oleksi

+0

ActiveResource 클래스에 connection =이 존재하지 않는 것처럼 보입니다. 어떤 아이디어일까요? – Oleksi

+0

그래서이 문제를 해결하기 위해'self.connection = (value); @connection = value; end'를 추가했습니다. – Oleksi