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?
Here is a fast walkthrough.
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.
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.
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:
You can find a nice recap of different strategies here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With