Where in my Rails app do classes responsible for doing 3rd party API calls go?

387 views Asked by At

My application interfaces with a number of internal APIs in order to periodically import data and I was wondering where the appropriate place to put this code lives.

Some common places i've seen are /app/models/third_party_api.rb OR /lib/apis/third_party_api.rb but i have no idea what the common convention is.

Thank you

1

There are 1 answers

0
Ismael Abreu On BEST ANSWER

You can place that code at many places. app/models, app/api_clients/<api_name>, lib/api_clients/<api_name>

Or probably the best is to create a gem for each api wich you can use in your Gemfile, and if you don't want to publish them you can just place it at vendor/gems/<gem_name> and use it with

gem 'gem_name', path: 'vendor/gems/gem_name'

And you have your api client separated from the rest of the project, with it's own test suite and easy to reuse later on other projects. I took this approach in a recent project and I'm very happy with it.