Usage Without ActiveRecord: HTTP Services
This is a commonly requested example. Instead of using a full-fledged
client like ActiveRecord
or Trample
, we’ll show low-level usage that
could apply to a variety of HTTP clients.
Remember, we always start with a “base scope” and modify that scope depending on incoming request parameters. This same pattern could apply to simply ruby hashes.
Let’s start by specifying a Null
adapter - a pass-through adapter that
won’t do anything without us explicitly overriding:
Every time we get a request to sort, paginate, etc we’ll need to modify our hash. Here we’ll simply merge parameters in the format our HTTP client will accept:
Finally, we need to tell the resorce how to resolve the query. In our case, this means passing the built-up parameters into a method on our HTTP client.
Note that #resolve must return an array of Model
instances. These
can be simple POROs, as you see above.
The final request would look something like this:
In our controller, if we used the lower-level jsonapi_scope method to introspect our results, we’d see an array of Post
instances:
If we found ourselves typing similar Resource
code - always merging in
the same paramters to the hash - we’d probably want to package all this
up into an
Adapter.