Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 活动资源响应,如何获取它们_Ruby_Http Headers_Response_Activeresource - Fatal编程技术网

Ruby 活动资源响应,如何获取它们

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

我有一个可以查询数据的活动资源。它返回记录,计数,任何我要求的

例如:product=product.find(123)

响应头应该包含一个自定义属性,比如“HTTP_PRODUCT_COUNT=20”,我想检查一下响应

IRB最有效的方法是什么?我没有可能提供底层响应的Rails或其他框架


我是否需要通过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