Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What can I use to allow an android app to communicate with a rails app

I'm trying to create an application that basically lets a user on an android device send a message that instantly appears in the rails application. Does anyone have suggestions to what I would use to do this?

like image 556
Teddy Avatar asked Dec 12 '22 17:12

Teddy


1 Answers

Here is a fast walkthrough.

Preparing the base application

Let's create a new rails app

$ rails new simple-message
$ cd simple-message/

We are now going to create a RESTful resource called Message, that will manage the messages coming from your mobiles.

$ rails generate scaffold Message title:string content:text

Create the table that will contain the messages:

$ rake db:migrate

Start the boundled server and point your browser to http://0.0.0.0:3000/messages

From there you can manually create new messages, that will show up as a list.

Your API is already there

For every page you can navigate with your browser there is a corresponding view that accepts programmatic calls from other clients.

If you browse http://0.0.0.0:3000/messages.xml you will get an xml containing all you messages. With curl:

$ curl http://localhost:3000/messages.xml
<?xml version="1.0" encoding="UTF-8"?>
<messages type="array">
  <message>
    <created-at type="datetime">2010-11-27T18:24:36Z</created-at>
    <title>Just a message</title>
    <updated-at type="datetime">2010-11-27T18:24:36Z</updated-at>
    <id type="integer">1</id>
    <content>With some content</content>
  </message>
</messages>

Its time to add a new message:

$ curl -X POST -H 'Content-type: text/xml' -d '<message><title>Hello</title><content>How are you</content></message>' http://0.0.0.0:3000/messages.xml
<?xml version="1.0" encoding="UTF-8"?>
<message>
  <created-at type="datetime">2010-11-27T18:40:44Z</created-at>
  <title>Hello</title>
  <updated-at type="datetime">2010-11-27T18:40:44Z</updated-at>
  <id type="integer">2</id>
  <content>How are you</content>
</message>

Your Android client will have to act as curl to add new messages.

Securing your web app

Now you need some authentication to allow only certain user to act as an admin and to restric the use of your API. Before digging for the various way to implement authentication in Rails:

  • should your user authenticate before posting a message?
  • should the web app accept authentication from other services (i.e. Twitter, Facebook, Google, OAuth, OpenID...) or against your own user database?
  • are your API open to other clients, or just your Android client can post messages?
  • do you need to know who posted the message (i.e. the user login)?
  • do you want to block a single user or an application to post messages to your web app?

You can find a nice recap of different strategies here.

like image 102
lbz Avatar answered Jan 04 '23 23:01

lbz