Isaac Y
Isaac Y

Reputation: 660

Structuring a Rails App to work with Remote Database via API

I am about to embark on the creation of one of my first meaningful rails apps, and am a bit unsure of how to structure things.

Here's my situation. I am using a SAAS inventory software that keeps track of approximately 4000 products. I need an app that can perform routine maintenance functions on the products. For example, to give you an idea:

My questions are as follows:

  1. I'm not sure exactly how to structure the data in this app. Should I query the API each time I need to retrieve the products? Or should I build a local copy of the inventory in a local database, for faster querying? If I am to build a local copy of the database, what would be an efficient way of keeping an up to date version of each product without consuming too much server resources? Obviously I can't pull 4000 products via the API at once...and I also don't want a cron job to be running every minute of the day.

  2. Where should I place the code for my remote API functions? Do I create a class, module, or something else?

Thanks for any advice.

Upvotes: 0

Views: 55

Answers (2)

Siim Liiser
Siim Liiser

Reputation: 4348

If your API follows REST then it can very easily be integrated into rails using ActiveResource. If the API is more complicated, you could use ActiveModel and roll your own implementation. Just create a base class of your own that handles connection and authentication and extend your models from that. Given the limited description of your problem, there's not much more I can say.

Upvotes: 1

davids
davids

Reputation: 6371

First of all, do you really need a web interface? I don't see so in your app description. If what you want to build is a maintenance set of tools that would be executed periodically, why would you use Rails?

The way of structuring data depends on your needs and resources. Does your SAAS provide instant notifications on products update, such as webhooks? Does your SAAS API let you fetch only products that were updated after a certain date? What's more important to you, speed or working with up to date data? Answering those questions to yourself should help you to decide what the best approach is.

Regarding the app design, it looks that you could write a gem where you could have different modules to manage data, execute operations with that data and connect to the third party service, and use that gem in scripts that would execute periodically. I would recommend you reading a book on Object Oriented design that I truly believe it'd help you, Practical Object-Oriented Design in Ruby. Just one last word, I'm taking too many assumptions here, no one but you know better your requirements, and the design of your software depends on that.

Upvotes: 1

Related Questions