Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Short answer: it depends on your JSON service.</p> <p>Is the service RESTful, for starters? If so, you're in luck. ActiveResource is <a href="https://github.com/rails/rails/commit/f1637bf2bb00490203503fbd943b73406e043d1d" rel="nofollow">dead</a>, so I wouldn't advise using it, but the code itself would provide a good starting point for creating a set of wrapper methods like <code>create</code> and <code>find</code> to access your API and manipulate records the way ActiveRecord creates and queries records in the DB. That's relatively easy if your API is RESTful because <a href="http://css.dzone.com/news/rest-with-rails-part-1" rel="nofollow">rails is built around RESTful-ness</a>, so the mapping between the two becomes much cleaner.</p> <p><a href="http://css.dzone.com/news/rest-with-rails-part-iii-using" rel="nofollow">This article</a> sums it up well:</p> <blockquote> <p>Rails makes it extremely easy to build Web services that follow the REST principles and work equally well with Web browsers and the programmable Web. In fact, a lot of that simplicity comes directly from following these principles. We didn’t have to tell our client how to create, read, update or delete the resource, those all followed from using the proper HTTP methods. All we had to do is point our client at the right place.</p> </blockquote> <p>If your service is <em>not</em> RESTful, which judging from the comments on other questions I think is perhaps the case, then you'll have your work cut out for you. ActiveModel will not do this work for you: <code>create</code> is defined in <a href="https://github.com/rails/rails/blob/master/activerecord/lib/active_record/persistence.rb" rel="nofollow">ActiveRecord::Persistence</a>, and <code>find</code> is defined in <a href="https://github.com/rails/rails/blob/master/activerecord/lib/active_record/relation/finder_methods.rb" rel="nofollow">ActiveRecord::FinderMethods</a>. They're not in ActiveModel. ActiveResource is able to reproduce them fairly easily because it makes assumptions about the type of service it is interfacing with (i.e. that it is RESTful, plus a few other things).</p> <p>What ActiveModel offers is all the other stuff that makes rails so useful for dealing with models: its validation system, serialization methods, dirty tracking of attribute changes, callbacks (<code>before_save</code>, <code>after_save</code>, etc.), translation/localization, and so on. These are all very useful functions to have, but they still leave you with the problem of wrapping your API calls.</p> <p>So here's what I would recommend, based on my albeit limited experience (see my final note about that):</p> <ol> <li>First, take a close look at your API and figure out how close it is to being RESTful. If it is <em>not</em> RESTful, you'll have to think about how to work around this so you can treat it like a RESTful service. A good reference on how to do this is O'Reilly's <a href="http://shop.oreilly.com/product/9780596529260.do" rel="nofollow">RESTful Web Services</a> (specifically Chapter 2, "Writing Web Service Clients".)</li> <li>Create an API wrapper around it that implements <code>create</code>, <code>find</code> and whatever other ActiveRecord-like functionality you want to have. You'll probably want to use a gem like <a href="https://github.com/technoweenie/faraday/" rel="nofollow">Faraday</a> or <a href="https://github.com/jnunemaker/httparty/" rel="nofollow">HTTParty</a> for actually making requests to the API rather than working directly with Net::HTTP. (I've never used DataMapper so can't comment on that.)</li> <li>Include whatever elements of ActiveModel you want to use to make your wrapper class more like a rails model: validations, serialization etc. In Rails 4, you can actually include everything using the new barebones <a href="http://blog.plataformatec.com.br/2012/03/barebone-models-to-use-with-actionpack-in-rails-4-0/" rel="nofollow">ActiveModel::Model</a>.</li> </ol> <p>That will get you much of the way to using your JSON service as a Rails model.</p> <p>For the record, my experience in actually implementing this kind of thing is limited to building an API wrapper (in the works) for accessing an XML API. The wrapper has a class which includes ActiveModel validators for validating queries to the API. I've found the ActiveModel validators really useful for guaranteeing that the API is accessed correctly, but the wrapper is only for getting records from the API, not for actually creating or updating them, so the interface is quite a lot easier than what I expect you'll be building.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload