7

I'm trying to use Models in my Rails application that retrieve information from an external API. What I would like to do is access my data models (which may consist of information resulting from multiple API calls) in a way similar to what an ActiveRecord model would provide (specifically associations, and the same style of chain-able query methods).

My initial instinct was to recreate the parts of ActiveRecord that I wanted and incorporate this API. Not wanting to 'reinvent the wheel' and seeing exactly how much work would be required to add more functionality have made me take a step back and reevaluate how to approach this.

I have found ways to use ActiveRecord without a table (see: Railscast #193 Tableless Model and the blog post here) and looked into ActiveRecord. Because ActiveModel only seems to include Validations I'm not sure that's very helpful in this situation. The workaround to using ActiveRecord without a table seems like the best option, but I suspect there's a cleaner way of doing this that I'm just not seeing.

Here is a gist containing some of the code written when I was trying to recreate the ActiveRecord functionality, borrowing heavily from the ActiveRecord source itself.

My question boils down to: I can get the functionality I want (chaining query methods, relations) by either implementing the workaround to ActiveRecord specified above or recreating the functionality myself, but are these really ideal solutions?

7
  • Take a look at the code in github.com/adelevie/parse_resource which acts as a wrapper to Parse.com's API. Perhaps it will be helpful. Commented Mar 9, 2013 at 17:20
  • I know there are adapters for the different databases behind AR, maybe you could write a kind of "APIAdapter"? Commented Mar 9, 2013 at 17:27
  • @JonathanAllard - That may be a viable route, but I don't yet know enough about how ActiveRecord does what it does to say for sure. As far as I can tell the 'adapter' would need to either come between the ActiveRecord finder methods and ARel, the ORM query-generator, or as an adapter to ARel itself. A thread here leads me to believe the latter option may be more trouble than the former. Commented Mar 9, 2013 at 18:05
  • I think there's a danger of this approach leading to a bad design your system. One of the benefits an API provides is a simpler representation of the remote service which abstracts away underlying complexity. If the client needs to worry about joins, wheres, etc. when making a request then it's going to be harder to use that API. It's a trade-off between flexibility and simplicity. Commented Mar 9, 2013 at 21:28
  • @AndyWaite, sorry I could have been more clear. I'm consuming the API and wanted to have the models representing data in my application (which will be backed by the API) accessible in a way similar to models that extend ActiveRecord::Base. Commented Mar 10, 2013 at 2:51

1 Answer 1

3

Remember that Rails is still just Ruby underneath.

You could represent the external API as instantiated classes within your application.

class Event
  def self.find(id)
    #...External http call to get some JSON...#
    new(json_from_api)
  end

  def initialize(json)
    #...set up your object here...#
  end


  def attendees
    #...external http call to get some JSON and then assemble it 
    #...into an array of other objects
  end
end

So you end up writing local abstractions to create ruby objects from api calls, you can probably also mix in ActiveModel, or Virtus into it, so you can use hash assignment of attributes, and validations for forms etc.

Take a look at an API abstraction I did for the TfL feed for the tube. service_disruption

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. At the time, this helped me realize that all I really needed was a nice way to abstract the interface.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.