How do I make an API for my rails app?

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

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.

Really? Maybe updates? Maybe it’s a game that has a 3d client for
it? :wink: lots

Blog: http://random8.zenunit.com/
Learn rails: http://sensei.zenunit.com/

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

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

Thanks for the replies Mukund, Julian, and Comopasta!

@Mukund - I consider it to be a wonderful app :wink: 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 :wink:

Thanks again for the replies!

-Tony

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

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 :slight_smile:

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.

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. :slight_smile:

As for the iPhone UI, you may want these:

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

Google iUI JavaScript Library: Google Code Archive - Long-term storage for Google Code Project Hosting.

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

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. :slight_smile: