Consuming RESTful api with activeResource

Hi,

I’m being asked to use this api to retrieve the data because the ror
application won’t have any database to store the users information, the
database will only be accessed by this api.

For example for creating a new user I have to send a post request with a
json format to the url: api.server.com/api/v/user/subs

So in my model I have this:

class User << ActiveResource::Base
self.site = “http://api.server.com/api/v
self.format = :json
end

and when I try this:
u = User.new
u.save

I got 400 Bad request because the request is send to this location

POST http://api.server.com:80/api/v/users.json

I guess the first problem is the rails conventions for pluralize
collections, so it write users instead of user like is needed in the
url, the second problem is that after user I need to and, in this action
of create a new user, the /subs and I don’t know how to do this.

After that problem I have another issue in the body of the request it
sends the json like this {“user”:{“email”:“mail”}}
but the api is expectin something like this {“email”:“mail”}

After saying all this I want to know if it is a proper way to make
AResource to behave the way I need or if already exist a plugin/gem to
work with this kind of APIs or if a need to come with a new solution and
leave behind AR

So far I came with a solution for two of this issues:

  1. Url using a pluralized name of the object User solved with:

self.collection_name = “user”

  1. Removing the extension from the URL, I solve this adding a Module
    that I found googling around to override some methods of AR.

I thought that I had found the solution for another problem, removing
the element_name from the json adding:

ActiveResource::Base.include_root_in_json = nil

But my json still have this node

{“user”:{“email”:“hi’\n”}}

Maybe you can use the Custom Methods
(http://api.rubyonrails.org/classes/ActiveResource/CustomMethods.html)
to
try if it works.

And the try to use the “normal” ActiveResource …

There is the remove_root method, is this what you want?

2012/10/11 arturo drlt [email protected]

I thought that I had found the solution for another problem, removing
Posted via http://www.ruby-forum.com/.


Fernando A.

Xavael wrote in post #1079637:

Maybe you can use the Custom Methods
(http://api.rubyonrails.org/classes/ActiveResource/CustomMethods.html)
to
try if it works.

And the try to use the “normal” ActiveResource …

Thx for that module it will be very helpful

Fernando A. wrote in post #1079656:

There is the remove_root method, is this what you want?

2012/10/11 arturo drlt [email protected]

I thought that I had found the solution for another problem, removing
Posted via http://www.ruby-forum.com/.


Fernando A.
www.fernandoalmeida.net

Yeah I want something like that, with this I think I have all my issues
solved for now thx guys.

Or do you want the ActiveResource::Base.include_root_in_json = false ?

2012/10/13 Fernando A. [email protected]


Fernando A.
www.fernandoalmeida.net


Fernando A.