【问题标题】:rails method chaining contextrails 方法链接上下文
【发布时间】:2013-04-04 17:50:48
【问题描述】:

我有可能是一个基本的 Q,但它在设置中看起来很复杂。我有一个包含一些类的模块。一个类包含 API 调用的方法。其他类描述服务器。例如 Dev 有它的属性。服务器类继承包含所有 API 调用的类。我使用服务器类的一个实例来使用这些方法之一,然后对其应用 EventMachine 方法。这是服务器类的一个子集:

 class PulseDev < ApiMethods
   def base_uri
    "http://myserver.com/api"
   end
 end

以及方法类中的一个动作:

 Class ApiMethods
   def get_json_api_post_response(url, post_obj={})
     http = EM::Synchrony.sync EventMachine::HttpRequest.new(self.base_uri+"#{url}").post(:body => post_obj)
     process_response self.class.post(url, :body => post_obj).body
   end

       def process_response(result)
         response = ActiveSupport::JSON.decode(result)
           if response["code"].to_i == 200
             ToolResult.new(true, response["result"], 200)
           else
             ToolResult.new(false, response["result"], response["code"])
           end
         end
        end

 Class ToolResult < Struct.new(:success, :result, :code)
 end

我在控制器中调用它:

 http = ApiMethods::Dev.new.get_json_api_post_response('/handshake', @j)

好的,我的错误是未定义的方法 `post' for ApiMethods::PulseDev:Class 它指向我的 get_json_api_post_response 方法中的帖子。

我的问题是:我知道它在 ApiMethods::Dev 的上下文中,这就是 self.base_uri 工作的原因,但是我应该如何处理该 process_response 方法中的帖子,以便它与 EventMachine 相关联?有没有关于我没有看到的方法链接的东西?在错误输出中,我可以验证 http 正在显示 EventMachine 对象,因此新方法似乎正在工作。谢谢你的时间,山姆

【问题讨论】:

    标签: ruby ruby-on-rails-3 eventmachine chaining


    【解决方案1】:

    答案是更仔细地查看错误消息。 process_response 方法是实际调用 EventMachine 方法并处理其 :body 的方法。所以它是用一个不需要的调用写的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-08
      • 2012-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多