Consume Rails RESTful CRUD from RhoMobile App (iPhone, Android)

Visnupriya

51 sec read

Thought I would share a sample RhoMobile (for iPhone and Android) app that consumes a remote RoR based RESTful services.
It can handle Create, Read, Update and Delete from RhoMobile itself.
RoR based RESTful API is at http://spritle-rhomobile-2.heroku.com/developers
Sample RhoMobile app code is at https://github.com/spritle/restful_crud_rhomobile_sample

Note:

The client code does not uses RHOM model fully. In the next version we will refactor the code to store the data locally on device for basic offline access.

I used Rho::AsyncHttp for GET, POST, PUT and DELETE operations.
Example:
/GET
[source language=”ruby”]
response = Rho::AsyncHttp.get(:url => Rho::RhoConfig.RESTFUL_URL + “developers.json”,
:headers => {“Content-Type” => “application/json”})
@result = response[“body”]
[/source]
/POST
[source language=”ruby”]
name = @params[‘developer’][‘name’]
role = @params[‘developer’][‘role’]
body = ‘{“developer” : {“name” : “‘+ name +'”,”role” :”‘+ role +'” } }’
@result = Rho::AsyncHttp.post(:url => Rho::RhoConfig.RESTFUL_URL + “developers.json”,
:body => body, :http_command => “POST”, :headers => {“Content-Type” => “application/json”})
[/source]
/PUT
[source language=”ruby”]
response = Rho::AsyncHttp.post(:url => Rho::RhoConfig.RESTFUL_URL + “developers/#{id}.json”,
:body => body, :http_command => “PUT”,:headers => {“Content-Type” => “application/json”})
[/source]
/DELETE
[source language=”ruby”]
id = @params[“developer_id”].to_s
response = Rho::AsyncHttp.post(:url => Rho::RhoConfig.RESTFUL_URL + “developers/#{id}.json”,
:http_command => “DELETE”,
:headers => {“Content-Type” => “application/json”})
[/source]
Sample iPhone screenshot




Related posts:

3 Replies to “Consume Rails RESTful CRUD from RhoMobile App (iPhone, Android)”

  1. Hi Visnu, can you refactor the code a bit with jQuery search filter, so that instead of listing all developers it will allow user to search using textbox and it will try to match the developer name containing that search string. 2nd part is once it has filtered, the results will be displayed as before, in addition, it will ask the user whether he wants to save to device Rhom, if yes, it will try to first search SQLite database, in device, by developers ID. If found, it will update the data in device with that retrieved from webservice, if not found, it will create/add to SQLite database in device.
    Hey Visnu/Balaji et al, cmon this must be a simple enough task for you guys, and you already mentioned about the refactoring version in this blog, which never happened in the last year, hope my request will not fall on dead ears this time.

Leave a Reply

Your email address will not be published. Required fields are marked *