4

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?

Teddy
  • 579
  • 1
  • 4
  • 17
  • 1) Is your Rails app already in place? 2) Are those messages described as a restful model? 3) There is any authentication layer to honour before adding a new message (Devise, HTTP authentication, ...)? – lbz Nov 27 '10 at 22:57

3 Answers3

13

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.

Community
  • 1
  • 1
lbz
  • 9,560
  • 2
  • 30
  • 35
0

The best way to do this is by creating an API for your rails app. Then you can use HTTP post request from your Android Application.

Community
  • 1
  • 1
Raunak
  • 6,427
  • 9
  • 40
  • 52
0

If you respect the REST paradigms it's as easy as a POST to an URL.

Pasta
  • 1,750
  • 2
  • 14
  • 25