wit.ai Ruby Gem, repeated calls, call doesn't end - ruby-on-rails

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

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..

Related

Handling exceptions in Rails API request

I have an API with an endpoint that makes a request to a third-party service. I am trying to pass the error from the module to my controller so I can return the error in my API response. This issue is that I cannot seem to access the error.
# controller
def download_pdf
doc = SignedDocument.find(params[:id])
begin
data = EsignGenie.downloadDocumentFromFolder(doc.folder_id, doc.document_number)
render json: {data: data}, status: :ok
rescue Exception => e
render json: e, status: :unprocessable_entity
end
end
#module
def self.downloadDocumentFromFolder(folder_id, document_number)
url = "https://www.esigngenie.com/esign/api/folders/document/download?folderId=#{folder_id}&docNumber=#{document_number}"
begin
response = RestClient::Request.execute(method: :get, url: url, raw_response: true, headers: {Authorization: "Bearer #{ENV['ESIGNGENIE_AUTH']}"} )
rescue => e
# the exception I'd like to pass to the controller
p "e.http_body", e.http_body
throw e
end
data = Base64.encode64(File.open(response.file).read)
end
What am I doing wrong here?

rails receiving webhook between two localhosts

I have an rails app that sends a webhook (localhost:3000) with data. Before, I used a online webhook monitor (Pipedream) but I want now to build a second rails app to receive the data (localhost:3000 -> localhost:3300). I've created a route /webhook/receive and the controller for it but get nothing from localhost:3000.
What I'm doing wrong?
Thats on my first app:
def send_create_webhook(webhook_url)
company = User.find_by(email: 'test1#test.de').company_account
webhook_url = 'tcp://0.0.0.0:3300/data/receive'
SendWebhookJob.perform_later(webhook_url, {
subscription: {
type: "#{#tool.title} successfully subscribed",
date: "#{Time.zone.now}",
subscribed: Time.now.strftime("%Y-%d-%m %H:%M:%S %Z"),
user: {
company: "#{company.title}",
name: current_user.name,
email: current_user.email,
}
}
})
end
class SendWebhookJob < ApplicationJob
queue_as :default
WEBHOOK_RETRIES = 3
def perform(webhook_url, data, retry_count = 0)
p "COUNT #{retry_count} : #{Time.zone.now} : Sending data to #{webhook_url}"
# make a request
begin
response = HTTP.post(webhook_url, json:data)
successful = response.code == 200
rescue StandardError => e
successful = false
end
if successful
p "SUCCESSFUL WEBHOOK"
elsif retry_count < WEBHOOK_RETRIES
wait = 2 ** retry_count
SendWebhookJob.set(wait: wait.seconds).perform_later(webhook_url, data,retry_count + 1)
end
end
end
The second app I just created:
resources :data do
get :receive
post :receive
end
def DataController < ApplicationController
require "http"
def receive
response = HTTP.get('tcp://0.0.0.0:3000')
end
end

Best practices for Ruby on Rails service

I'm writing some mobile otp validation service and below is my service class
require 'nexmo'
class NexmoServices
def initialize api_key = nil, api_secret = nil, opts = {}
api_key = api_key || Rails.application.secrets.nexmo_api_key
api_secret = api_secret || Rails.application.secrets.nexmo_secret_key
#nexmo_client = Nexmo::Client.new(
api_key: api_key,
api_secret: api_secret,
code_length: 6
)
#brand = 'CryptoShop'
end
def send_verification_code opts
#nexmo_client.verify.request(number: opts[:number], brand: #brand)
end
def check_verification_code opts
#nexmo_client.verify.check(request_id: opts[:request_id], code: opts[:verification_code])
end
def cancel_verification_code opts
#nexmo_client.verify.cancel(opts[:request_id])
end
end
in the controller i'm calling the service methods like below
class NexmoController < ApplicationController
def send_verification_code
response = NexmoServices.new.send_verification_code params[:nexmo]
if response.status == '0'
render json: response.request_id.to_json
else
render json: response.error_text.to_json
end
end
def cancel_verification_code
response = NexmoServices.new.cancel_verification_code params[:nexmo]
if response.status == '0'
render json: response.to_json
else
render json: response.error_text.to_json
end
end
end
I have read that usually there will be call method inside service class and controller will call that. service method call will take care of the rest.
My case im instantiating service objects for all the methods if you see my controller(NexmoService.new).
is it correct??? I want to know the best practise must be followed in this scenario.
Thanks,
Ajith

How to send a get to a server and wait a get in my app in Ruby on Rails

I am using Zapier to search some information in google sheets. I used Webhocks to send a GET to his server with a JSON information. The response of GET is an "OK" and I can't custom this.
So, they will execute a task, find what a I want and return a value, but the response must be a GET in my server, and I don't know how to intercept this response in my route.
I'm trying to study Rails Rack to intercept de request in my app, but I don't know how to send the response to the event that sent the first GET.
How is my middleware:
class DeltaLogger
def initialize app
#app = app
end
def call env
Rails.logger.debug "#{env['QUERY_STRING']}"
#status, #headers, #response = #app.call(env)
[#status, #headers, #response]
end
end
Thanks!
Example
So, to get the value returned from Zapier, I created two routes and a global class cache.
class Zapier
require 'httparty'
def initialize
#answer = ""
#id = 0
end
def request(uri, task)
last_event = Event.last
puts last_event.inspect
if last_event.nil?
last_id = 0
else
last_id = last_event.event_id
end
event_id = last_id + 1
Event.find_or_create_by(event_id: event_id)
result = HTTParty.post(uri.to_str,
:body => {id: event_id, task: task}.to_json,
:headers => {'content-Type' => 'application/json'})
#answer = ""
#id = event_id
end
def response(event_id, value)
if event_id != #id
#answer = ""
else
#answer = value
end
end
def get_answer
#answer
end
end
And my controller:
class ZapierEventsController < ApplicationController
require 'zapier_class'
before_action :get_task, only: [:get]
before_action :get_response, only: [:set]
##zapier ||= Zapier.new
def get
##zapier.request('https://hooks.zapier.com',#task)
sleep 10 #Wait for response
#value = ##zapier.get_answer
render json: { 'value': #value }, status:
end
def set
##zapier.response(#id, #value)
render json: { 'status': 'ok' }, status: 200
end
def get_task
#task = params["task"]
end
def get_response
#id = Integer(params["id"])
#value = params["value"]
end
end
Now i have to make a Task Mananger

How to render a jsonapi-resources response in an custom controller action?

I have implemented my own object creation logic by overriding the create action in a JSONAPI::ResourceController controller.
After successful creation, I want to render the created object representation.
How to render this automatically generated JSON API response, using the jsonapi-resources gem?
Calling the super method does also trigger the default resource creation logic, so this does not work out for me.
class Api::V1::TransactionsController < JSONAPI::ResourceController
def create
#transaction = Transaction.create_from_api_request(request.headers, params)
# render automatic generated JSON API response (object representation)
end
end
You could do something like this:
class UsersController < JSONAPI::ResourceController
def create
user = create_user_from(request_params)
render json: serialize_user(user)
end
def serialize_user(user)
JSONAPI::ResourceSerializer
.new(UserResource)
.serialize_to_hash(UserResource.new(user, nil))
end
end
this way you will get a json response that is compliant with Jsonapi standards
render json: JSON.pretty_generate( JSON.parse #transaction )
def render_json
result =
begin
block_given? ? { success: true, data: yield } : { success: true }
rescue => e
json_error_response(e)
end
render json: result.to_json
end
def json_error_response(e)
Rails.logger.error(e.message)
response = { success: false, errors: e.message }
render json: response.to_json
end
render_json { values }

Resources