0

I'm using a Ruby Gem for wit.ai, and have created an API in ROR that queries the wit.ai client.

But unexpectedly the method in the API keeps on calling the client method repeatedly, even after getting the response.

Because of which the API method never renders the JSON response.

How can I resolve this issue? Why is this happening?

It works perfectly fine if I do the same thing from Rails Console.

ApiController

 module Api

   module V0

     class ApiController < ApplicationController

        def response
            q = params[:q]
            response = Api::V0::ApiModel.handle_response q
            render :json => response, :status => 200
         end
     end
   end
end

ApiModel

module Api

   module V0

      class ApiModel

        def self.handle_response q
            response = ChatbotHelper.query q
            if response['type'] == "msg"
                message = response["msg"]
                json = {"message" => message}
            else
                json = response
            end
            json
        end
      end
   end
 end

ChatbotHelper

module ChatbotHelper

   def self.init

    actions = {
        send: -> (request, response) {

            puts "REQUEST #{request} RESPONSE #{response}"
            puts("#{response['text']}")
            response
        },
        getData: -> (context){

        },
    }

    @client = Wit.new(access_token: "XYZ", actions: actions)

end

def self.query q
    begin
        self.init
        response = self.get_response q
    rescue SocketError
        response = {"message": "SocketError"}
    end
    response
end

def self.get_response q
    puts "GET RESPONSE"
    response = @client.converse("b", q, {})
    response
end
end
red-devil
  • 1,064
  • 1
  • 20
  • 34
  • use a debugger and step through your code to find where the loop is happening – max pleaner Jul 27 '16 at 09:56
  • Its the call to the wit.ai client which is happening again and again. No bug related to the code. Something similar to polling. Looks like something related with wit.ai. Couldn't find a work around – red-devil Jul 27 '16 at 10:38
  • Debugging this by creating another API call, I realised its the issue with `render :json => response, :status => 200` itself.. any idea what's happening? – red-devil Jul 27 '16 at 11:34
  • Ahh! it was happening due to the name of the function in the API Controller, i.e. `response`.. Seems like its an inbuilt function for ROR.. – red-devil Jul 27 '16 at 11:46

1 Answers1

0

Ahh! it was happening due to the name of the function in the API Controller, i.e. response.. Seems like its an inbuilt function for ROR..

red-devil
  • 1,064
  • 1
  • 20
  • 34