Question

I am writing an app which is a "connector" between two APIs in rails 4.

 -----              --------                               ------
|     | Webhooks   |        |  API Calls through a client |      |
| API | ---------> | My APP | --------------------------> | API' |
|     |            |        |                             |      |
 -----              --------                               ------

It is a one way connector, there is no database, just data transformation and API calls.

I want to use the models to do the API calls inside. i.e Product model:

  • find a product
  • save a product
  • etc.. the same thing that I can do with the database but with an API instead

The trickiest part is the API authentication. I have a before_filter before each webhooks to retrieve the good account and the API key and I need to find a way to transmit my API client to the model to execute the call. The API account could be different between 2 webhooks.

so two questions:

  • is it the right way to do it (having API calls in a model)? in order to avoid code duplication and have skinny controllers
  • is there a way with Rails to pass some data from the controller to the model without having a special parameter for each method for my API client?

thanks in advance

Était-ce utile?

La solution

Yes, your logic should still be encapsulated in a Product "model". It's conceptually the same as activerecord and activeresource. I'm confused about the API calls though. Are you saying that you have to pass through the API authentication from the webhook? Is it different depending on the model or depending on who triggered the webhook or something else entirely? It's not really clear what you are trying to do so an example would be helpful.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top