Search code examples
rubyruby-on-rails-3eventmachinechaining

rails method chaining context


I have what is probably a basic Q, but it appears complex in the setup. I have a module that has some classes. One class contains methods for API calls. Other classes describe a server. Dev for instance has its attributes. The server classes inherit the class that contains all the API calls. I use an instance of the server class to use one of these methods and then apply EventMachine methods to it. Here's a subset of a server class:

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

And an action in the methods class:

 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

And my invocation of it in the controller:

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

OK, my error is undefined method `post' for ApiMethods::PulseDev:Class and it points to the post in my get_json_api_post_response method.

My question is: I get that it's within the context of the ApiMethods::Dev which is why self.base_uri works but how should I handle the post inside that process_response method so that it's tied to EventMachine? Is there something about method chaining I'm not seeing? In the error output I can verify that http is showing the EventMachine object so the new method seems to be working. Thanks for your time, sam


Solution

  • The answer is to look more carefully at the error msg. The process_response method is the one actually calling the EventMachine method and processing its :body. So it was written with an unneeded call.