0

I am creating a Rails application, which rather than using a database for the backend, will communicate with an external REST service.

This will work something like this:

Model.find(1)    # GET /model/1
Model.delete(1)  # DELETE /model/1
...

The business logic necessary to turn method calls into REST requests belongs in my model. However, there are several different servers that can be queried. Where do I put the connection logic so that:

  1. the queries are spread equally between servers?
  2. if a server becomes unavailable, the request is retried using a different server?

I am assuming this logic doesn't belong in the model, but I'm not sure where.

Any advice much appreciated.

gjb
  • 6,237
  • 7
  • 43
  • 71

1 Answers1

2

Have you considered using Active Resource? It is probably meant for almost exactly this use case, if I understand correctly.

Chirantan
  • 15,304
  • 8
  • 49
  • 75
  • Is it possible to alter the JSON that Active Resource expects? For example, Active Resource assumes that there will be an 'id' in the response, but the field is called something else. – gjb Feb 02 '12 at 16:32
  • 1
    This might help: http://stackoverflow.com/questions/1069899/consuming-non-rest-apis-in-rails-with-activeresource – Tom L Feb 02 '12 at 16:47
  • 2
    @gjb You need to override the `to_json` and `from_json` methods in the ActiveResource, call super and resassigned the id manually. http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html#method-i-from_json – Chirantan Feb 02 '12 at 16:58