1

I'm looking for the "rails" design pattern for code that fetches data from other websites.

I have a rails controller in my app that fetches data not from the database, but from external API's or scraped from the web.

Where's the "rails" place to put this code.

For quick implementation, I just stuck it in a model, but the model doesn't interact with the database - or support standard model functionality - so that feels wrong, but my understanding of rails and ruby isn't yet solid enough to know where it should go.

The way the code works roughly is controller calls model.fetchData args the model uses HTTParty or similar to make the call processes data passes it back to the controller

Any advice?

ChrisJ
  • 2,486
  • 21
  • 40

1 Answers1

1

Broadly-speaking I think there are two possible ways to do this:

  1. Create a plain ruby class to contain the methods for making requests to the API(s) and processing responses from it(them). You can include the HTTParty module in this class with include HTTParty. The usual place to put this code is in lib/ (make sure that wherever you put it, the path is in autoload_paths).
  2. If you're doing anything really complex, or the API itself is complex, you might want to consider creating a separate gem to handle interaction with the API(s). The term for this type of gem is an "API wrapper" -- if you look around, you'll see there are lots of them out there for popular services (Twitter, LinkedIn, Flickr, etc.)

Notice I haven't mentioned activerecord. If you're not going to be saving anything to the DB, I don't see any need to even create any activerecord models. You can get by with just controllers and views, and then (if needed) pick and choose components from activemodel (validations, internationalization, etc.) to make your ruby API wrapper class feel more like a Rails model. For example, one thing that I've done in an app I'm working on is to apply validations to query strings before actually making requests to an external API, which is a bit like running validations on database queries before querying a DB. See this article by Yehuda Katz for more on how to make plain ruby objects feel like activerecord models.

Hope that helps. I answered another question very similar to this one just yesterday, you might want to have a look at that answer as well: Using rails to consume web services/apis

Community
  • 1
  • 1
Chris Salzberg
  • 27,099
  • 4
  • 75
  • 82