Wednesday, October 27, 2010

Create a Rhodes application which will sync data from rails application using Rhosync

In this blog we will create a Rhodes application which will synchronize your data from rails application through Rhosync.
We need to create 3 applications
  1. Rails Application: It will feed data in json or XML format.
  2. Rhodes Application: This code will be installed in mobiles.
  3. Rhosync Application: These will sync the data from rails App to Rhodes Application in your phone
Steps to create rails Application:
I will just create a rails application using scaffold:
  1. rails employee_details
  2. cd employee_details
  3. Ruby script/generate scaffold employee name:string phno:integer address:text
  4. Update your database.yml 
  5. Rake db:migrate
  6. Add these code in your employees controller (you can skip this step if you want to only show employee details and does not do crud operations.)
              protect_from_forgery :except => [:create, :update, :delete]
    
    7. Add this line under response in controller:
             
             format.json { render :json => @employee }

    8. Once it is done we will run it on port 3000:

              http://localhost:3000\employees.json

         we will get output in json format.


Steps to create Rhodes application:
  1. Rhogen app employee
  2. Cd employee
  3. Rhogen model employee name,phno,address
  4. Add link in index.erb to your index page
  5. Enable sync in model Employee  
                enable :sync


    6.   Edit rhoconfig.txt file and set
             syncserver = 'http://localhost:9292/application
    7.    rake run:bb


Steps to create Rhosync application:
  1. Rhosync app employee_server
  2. Rhosync source employee
  3. Then employee.erb incude json and rest client
          require 'json'
          require 'rest_client'

     4. Update the query method

  def query(params=nil)
    parsed=JSON.parse(RestClient.get("http://localhost:3000/employees.json").body)
    @result={}
    parsed.each { |item|@result[item["employee"]["id"].to_s]=item["employee"] } if parsed
    @result
  end

Now you can test this by login to your simulator.





Tuesday, October 12, 2010

Bug in rhodes-translator- 0.0.1 template

As metadata in Rhodes is very new, few of the templates in rhodes_translater gem has bugs.
I tried to use select box using metadata. But it's field was not saving the field value.
There is small bug in select.rb in rhodes_translator gem so I modified this file. So to fixed this issue:
Replace the file select.rb in
C:\Ruby187\lib\ruby\gems\1.8\gems\rhodes-translator- 0.0.1\lib\rhodes_translator\templates

with this new select.rb
(Download it from link below)


Saturday, October 9, 2010

Creating a Rhodes Application which will synchronize your tweets to your phone

In this blog I will create a Rhodes application which will sync your tweets through rhosync.

I am currently using Rhodes 2.1.0 and Rhosync 2.0.6.

If you want to some complex operation you can read twitter API docs:

http://dev.twitter.com/doc/get/statuses/user_timeline

Step 1.) Create a Rhodes Application

1.) Go to command prompt and create Rhodes application

>rhogen app twitter-api

>cd twitter-api

>rhogen model twitter text

2.) Now update index.erb( Adding a link to index page of Twitter controller

link_to “Twitter”, :controller => :Twitter


3.) Enable sync in Twitter model.

Update twitter.rb and add the following line

enable sync

4.) Update rhoconfig.txt(Setting the path for rhosync server)

syncserver = ‘http://localhost:9292/application’

5.) Start the simulator

rake run:bb

Step 2.) Create Rhosync Application

1.) Go to command promt

>rhosync app twitter –server

> cd twitter-server

>rhosync source twitter

2.) Update the source adapter twitter.rb

require ‘json’

require ‘open-uri’

Add this code in query method:

def query(params=nil)

parsed=""

open('http://api.twitter.com/1/statuses/user_timeline.json?screen_name=nalwayaabhishek') do |f|

parsed=JSON.parse(f.read)

end

@result = {}

parsed.each do |item|

item_hash = {}

item_hash['text'] = item['text']

@result[item['id'].to_s] = item_hash

end

@result

end

3.) Start redis server

rake redis:start

4.) Start rhosync server

rake rhosync:start

By default it will start on port 9292.


Once the simulater is started Login with any username and password. And sync the data.

Now you can see your tweets even if you are offline.