Ruby 活动资源响应,如何获取它们
我有一个可以查询数据的活动资源。它返回记录,计数,任何我要求的 例如:product=product.find(123) 响应头应该包含一个自定义属性,比如“HTTP_PRODUCT_COUNT=20”,我想检查一下响应 IRB最有效的方法是什么?我没有可能提供底层响应的Rails或其他框架Ruby 活动资源响应,如何获取它们,ruby,http-headers,response,activeresource,Ruby,Http Headers,Response,Activeresource,我有一个可以查询数据的活动资源。它返回记录,计数,任何我要求的 例如:product=product.find(123) 响应头应该包含一个自定义属性,比如“HTTP_PRODUCT_COUNT=20”,我想检查一下响应 IRB最有效的方法是什么?我没有可能提供底层响应的Rails或其他框架 我是否需要通过monkeypatching调用或其他方式攻击Net::HTTP或ActiveResource本身?这里有一种方法可以不用monkeypatching class MyConn < Ac
我是否需要通过monkeypatching调用或其他方式攻击Net::HTTP或ActiveResource本身?这里有一种方法可以不用monkeypatching
class MyConn < ActiveResource::Connection
attr_reader :last_resp
def handle_response(resp)
@last_resp=resp
super
end
end
class Item < ActiveResource::Base
class << self
attr_writer :connection
end
self.site = 'http://yoursite'
end
# Set up our own connection
myconn = MyConn.new Item.connection.site
Item.connection = myconn # replace with our enhanced version
item = Item.find(123)
# you can also access myconn via Item.connection, since we've assigned it
myconn.last_resp.code # response code
myconn.last_resp.to_hash # header
class MyConn 如果将@last\u resps[Thread.current]=resp改为Thread.current[“active\u resource\u connection\u last\u response”]=resp改为我不需要清除last\u resps的方法,你也可以试试这个gem,不是吗;我应该想到的。我将把这一点纳入我的回答中,并提到你的名字。我专门为这种需要开发了gem
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
module ActiveResource
class Connection
alias_method :origin_handle_response, :handle_response
def handle_response(response)
Thread.current[:active_resource_connection_headers] = response
origin_handle_response(response)
end
def response
Thread.current[:active_resource_connection_headers]
end
end
end