Receiving data from API to Rails API and send it back with data

155 Views Asked by At

Rails devs.

What I currently need is:

  1. Recieve data from active Campaign API into my rails controller.
  2. Generate a token using probably secureRandom.
  3. Send that token back to Active Campaign

But I'm getting a bit confused about how should I order these things. (Kinda like the egg or the chicken situation)

When I receive data from Active campaign, it comes sort like this:

{
    "contact": {
        "id": "1",
        "email": "[email protected]",
        "first_name": "John",
        "last_name": "Doe",
        "phone": "",
        "orgname": "",
        "customer_acct_name": "",
        "tags": "Form-quote-step-1"
    }
}

What I am not sure about is if do I need a permit.params for what I receive from Active Campaign to my rails controller?

The second is that in case I need to validate those parameters. I will need to create a token, and save that token on my database so then I can send it back to Active campaign on a request, that token goes.

I know for certain that I can do these things on the same controller. I just can't figure out how.

This is what I got so far: I apologize for the pseudocode, but again I'm kinda confused

 def create

 end

  private

    def update_contact
       # Here I'm thinking of sending the request to active campaign endpoint using `httparty`. 
    end

    def create_token
        #I'm thinking of adding my token here once the parameters are validated. Then I should generate it and save it.
    end

  def create_active_params
    #In case I need this, validate them before I create and save my token
    params.require(:contact).permit(
            :id, :email, :first_name, :last_name, :phone, :orgname, :customer_acct_name, :tags
        )
  end

Is my approach on the right way of things? Any help would be appreciated.

1

There are 1 best solutions below

0
Rabin Poudyal On

Generally, strong params are used for validating data from users, not API endpoints. Regarding generating a token and sending it back, if the logic is isolated it will be better. For example, you can create a service class and perform all your logic over there. That class will return your desired token and you send that token back from the controller. In summary:

  1. Instead of strong params for validation - you can create your custom validation class (strong params gets really complicated in nested json)
  2. From the controller, call the service that will provide token.
  3. Send the token back.