Forum: Ruby on Rails Consuming RESTful api with activeResource

Posted by arturo drlt (arturo-dlrt)
on 2012-10-10 23:52
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
Posted by arturo drlt (arturo-dlrt)
on 2012-10-11 17:09
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"

2) 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"}}
Posted by Xavael (Guest)
on 2012-10-12 21:17
(Received via mailing list)
Maybe you can use the Custom Methods
(http://api.rubyonrails.org/classes/ActiveResource/...) 
to
try if it works.

And the try to use the "normal" ActiveResource ...
Posted by Fernando Almeida (Guest)
on 2012-10-13 00:38
(Received via mailing list)
There is the remove_root method, is this what you want?

https://github.com/rails/rails/blob/6b4bbb427455ec...


2012/10/11 arturo drlt <lists@ruby-forum.com>

> I thought that I had found the solution for another problem, removing
> Posted via http://www.ruby-forum.com/.
>
--
Fernando Almeida
www.fernandoalmeida.net
Posted by Fernando Almeida (Guest)
on 2012-10-13 03:43
(Received via mailing list)
Or do you want the ActiveResource::Base.include_root_in_json = false ?

https://github.com/rails/activeresource/blob/maste...


2012/10/13 Fernando Almeida <fernando@fernandoalmeida.net>

>>
>>
>> --
>
> --
> Fernando Almeida
> www.fernandoalmeida.net
>
>


--
Fernando Almeida
www.fernandoalmeida.net
Posted by arturo drlt (arturo-dlrt)
on 2012-10-15 18:22
Xavael wrote in post #1079637:
> Maybe you can use the Custom Methods
> (http://api.rubyonrails.org/classes/ActiveResource/...)
> to
> try if it works.
>
> And the try to use the "normal" ActiveResource ...

Thx for that module it will be very helpful


Fernando Almeida wrote in post #1079656:
> There is the remove_root method, is this what you want?
>
> 
https://github.com/rails/rails/blob/6b4bbb427455ec...
>
>
> 2012/10/11 arturo drlt <lists@ruby-forum.com>
>
>> I thought that I had found the solution for another problem, removing
>> Posted via http://www.ruby-forum.com/.
>>
> --
> Fernando Almeida
> www.fernandoalmeida.net

Yeah I want something like that, with this I think I have all my issues 
solved for now thx guys.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.