Forum: Ruby on Rails How do I make an API for my rails app?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E4655b2aa2768140847acf487dc2b03c?d=identicon&s=25 Tony Tony (slythic)
on 2009-02-03 01:07
Hi all,

I'm currently close to releasing my first rails app. Many thanks to the
countless people that have helped me with my questions on this forum.
Couldn't have done it without you.

So being close to release I started thinking of neat things I could do,
like making an iphone app. I haven't tried making my own iphone app yet
and don't look too forward to it honestly. So I was thinking of making
an API for my rails app and let others with more experience than me give
it a shot.

In theory an API shouldn't be too hard but I wanted to get the opinion
of others. I've tried googling for this but there seems to be no good
posts that I can find.

Many thanks for any advice/suggestions.


-Tony
29ebf90af6107d2eb39b587c7972639c?d=identicon&s=25 Mukund (Guest)
on 2009-02-03 05:57
(Received via mailing list)
What does your Rails application do that other developers will be
interested in?   If you have some neat functionality that you want
others to use as a library, then release it as a gem.   If your entire
application does something wonderful, put in a SOAP or  REST interface
to it so it can be hosted as a web service.

I fail to see the connection between an iPhone application and the
Rails application.  An iPhone application does give you a wider
audience thanks to the app store.   So it is worth a shot if you a
have a good idea for an application.
3131fcea0a711e5ad89c8d49cc9253b4?d=identicon&s=25 Julian Leviston (Guest)
on 2009-02-03 06:39
(Received via mailing list)
Really? Maybe updates? Maybe it's a game that has a 3d client for
it? ;) lots


Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/
13d7a3ab12ec57e0d372d7115362cb91?d=identicon&s=25 comopasta Gr (comopasta)
on 2009-02-03 11:42
> So being close to release I started thinking of neat things I could do,
> like making an iphone app. I haven't tried making my own iphone app yet
> and don't look too forward to it honestly. So I was thinking of making
> an API for my rails app and let others with more experience than me give
> it a shot.

So you have rails app that does CRUD operations and you would like to do
those as well from a native mobile application instead than the browser.
Did I get it?

If so what I did is to implement my app restfully and use XML to execute
the CRUD operations from the terminal. I didn't do the terminal
implementation though...

Is this close to what you were asking?

Regards
E4655b2aa2768140847acf487dc2b03c?d=identicon&s=25 Tony Tony (slythic)
on 2009-02-03 14:47
Thanks for the replies Mukund, Julian, and Comopasta!

@Mukund - I consider it to be a wonderful app ;-) I see this app being
similar and no more complicated than twitterific or tweetie on the
iphone. I'm thinking a SOAP/REST interface is what I will need. Also,
the increased visibility of the app store wouldn't hurt!


@Julian - That game with a 3D client sounds like it would be awesome!
lol


@Comopasta - Yep, you got the idea! I have CRUD operations but I also
have some custom methods/actions that I'd like the API to access via a
native mobile app instead of the browser.


Does anyone know how taxing this is on the server? I think this would be
a lot easier as there are no stylesheets/javascript/images/etc. to
request. Unless of course there are millions of requests, in which case
it wouldn't be too bad either ;-)


Thanks again for the replies!



-Tony
E4655b2aa2768140847acf487dc2b03c?d=identicon&s=25 Tony Tony (slythic)
on 2009-02-03 15:53
I've done some quick searches on REST API as SOAP isn't recommended by
the rails community.

Came up with this site:
http://hinchcliffe.org/archive/2008/01/10/16613.aspx  I'll take a more
in depth look later on.

If anyone else has something to contribute, feel free!


-Tony
08182d53361d7ecbb602cb62c8fafe1a?d=identicon&s=25 Billee D. (Guest)
on 2009-02-03 20:05
(Received via mailing list)
Hopefully I am not misunderstanding your intent here, so please excuse
me if I am missing something.

If you want an API for a Rails app then a RESTful interface is the way
to go. In your controllers you need to add a respond_to directive
inside each method for XML output:

respond_to do |format|
  format.html # index.html.erb
  format.xml  { render :xml => @pages }
end

And then add the appropriate XML view to each action that you want to
have an RESTful interface to. You may also want to consider adding a
JSON format to the respond_to block as well for the sake of
convenience.

You will also need to set your routes so they are mapped as Rails
resources:

map.resources :resource

Typically if you use the scaffold or resource generators built into
Rails these are generated for you and all you have to do is supply the
appropriate view templates. However you should learn how to build
things yourself to give you a better understanding of what it takes to
generate resources in Rails.

I'm sure I might be missing something in my haste, but this is the
general gist of developing a RESTful interface.

And yes please stay away from SOAP as was mentioned. Rails has a
better way than SOAP. :-)

As for the iPhone UI, you may want these:

iPhoney: http://www.marketcircle.com/iphoney/

Google iUI JavaScript Library: http://code.google.com/p/iui/

If possible, you may want to pickup a copy of the following book as
well:

http://www.pragprog.com/titles/fr_arr/advanced-rails-recipes

This covers some advanced topics, but two of the recipes in the title
explain how to create a RESTful resource (or resources) and how to
support an iPhone UI using the Google iUI library linked to above.

Hope this helps at least a little. :-)
E4655b2aa2768140847acf487dc2b03c?d=identicon&s=25 Tony Tony (slythic)
on 2009-02-03 20:21
Billee D. wrote:
> Hopefully I am not misunderstanding your intent here, so please excuse
> me if I am missing something.
>


Hi Billee and thank you for the help!

So far I have all of that pretty much learned. My question (and maybe
I'm misunderstanding this greatly) is how to make an API for native
mobile apps to access. XML might be part of the answer but I'm not so
sure about that.

Most applications with an API require a key (usually a unique and random
16-26 character alphanumerical code) to confirm you are authorized to
access the account. I'm sure this key could be substituted by a username
and password (like twitterific for example). Once this native mobile app
is authorized to access the account it can do pretty much anything
someone on the website is allowed to do. Create, Remove, Update, and
Destroy (CRUD) operations and access some custom actions as well such as
adding friends.

Thanks.


-Tony
13d7a3ab12ec57e0d372d7115362cb91?d=identicon&s=25 comopasta Gr (comopasta)
on 2009-02-04 09:38
> Most applications with an API require a key (usually a unique and random
> 16-26 character alphanumerical code) to confirm you are authorized to
> access the account. I'm sure this key could be substituted by a username
> and password (like twitterific for example). Once this native mobile app
> is authorized to access the account it can do pretty much anything
> someone on the website is allowed to do. Create, Remove, Update, and
> Destroy (CRUD) operations and access some custom actions as well such as
> adding friends.

Hi, yeah keys are one option. I'm using restful_authentication and I
modified the plugin by adding some methods to handle the XML calls from
the mobile. Regarding the keys I actually reused the cookie
functionality of the same plugin, so for the mobile I set a long
expiration for the cookie and the client can use the API for as long
that is valid (logged in). I don't have the code here but I could post
my changes here, I would maybe get some feedback about my approach as
well :-)

As Billee D. mentioned adding the XML response featyre to the rails app
is very simple and JSON is also a possibility. I think it also depends
on what is easier to parse and consume for you on the terminal side.

Cheers.
This topic is locked and can not be replied to.