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.

23 comments:

  1. Hi Abhishek, nice tutorial . Will try it out.
    Thanks

    ReplyDelete
  2. great article..can you please explain this code;

    parsed.each do |item|

    item_hash = {}

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

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

    end

    @result

    end

    ReplyDelete
  3. Yes sure Femi..
    First of all we need to understand what values we are getting from twitter. To see the output from Twitter visit the following URL from your browser:
    http://api.twitter.com/1/statuses/user_timeline.json?screen_name=nalwayaabhishek&count=2

    you can use
    http://json.parser.online.fr/

    to read and understand this JSON Output from Twitter.

    This output will store in parsed variable :
    parsed =[
    { "text" =>"your tweet 1", "created_at" => "some time" .....},
    { "text" =>"your tweet 2", "created_at" => "some time" .....},
    { "text" =>"your tweet 3", "created_at" => "some time" .....}
    ]

    parsed.each do |item|

    item variable will have value

    { "text" =>"your tweet 1", "created_at" => "some time" .....}
    if we use item["text"] it will have stored your tweet

    Now we are storing all this value in a array @result
    @result = { "1" => {"text" => "your tweet 1" },
    "2" => {"text" => your tweet 2"},
    "3" => {"text" =>"your tweet 3"}
    }

    Whatever value you store in @result variable in query method is sync to your local database of your rhodes application.

    ReplyDelete
  4. thanks for this but i see that the column in the model is called 'twitter'..

    i dont see where you stuff what you get from the JSON field of 'text' into this model..IT seems you are putting it in an id field.

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

    i'm a bit confused here..

    ReplyDelete
  5. @Femni
    we need to convert your tweet data in following format to store in your rhodes application:

    @result = { "1" => {"text" => "your tweet 1" },
    "2" => {"text" => your tweet 2"},
    "3" => {"text" =>"your tweet 3"}
    }

    As we seen we need a distinct ID to store value in @result. In current example i have written 1,2 and 3 id for the three rows.
    But actually when we will get this values from twitter. It will be your tweet ID...example you get 22247690997862401

    so @result["22247690997862401"] = {"text" => "your tweet 1" }

    ReplyDelete
  6. One more important thing. I have used item_hash variable as a hash which allow us to add one more column to @result. Thats mean if we want to add other column like "created_at" or "retweet_count" to @result it is very simple. we have to just add one line in our code

    item_hash['created_at'] = item['created_at']

    after this the following data will sync :

    @result = { "1" => {"text" => "your tweet 1" , "created_at" => "Sun Nov 08 16:57:01 +0000 2009"},
    "2" => {"text" => your tweet 2" , "created_at" => "Sun Nov 08 20:57:01 +0000 2009"},
    "3" => {"text" =>"your tweet 3", , "created_at" => "Mon Nov 09 16:57:01 +0000 2010"}
    }

    ReplyDelete
  7. Hi Guyz

    I am new to Rhodes and I found your blog great and very helpful. I want to know, from where can i learn Rhodes. I mean tutorials or a book.

    Thanks in Advance.

    ReplyDelete
  8. Hi Davender,

    As of now the official tutorial are the only major source for reference : http://wiki.rhomobile.com/index.php/Rhomobile

    I think you can check out the webinar available on www.rhomobile.com they are very helpful.

    And if you are in Pune and available today I am speaking about Rhombile in Rails meetup today you can attend the event :

    http://www.meetup.com/PuneRailsMeetup/events/16130152/

    ReplyDelete
  9. You make a RhoSync adapter with the manipulation of the twitter data in that adapter. Is it also "acceptible" to do that manipulation inside the twitter controller? Why would you create an adapter over just adding that code in the controller of the app?

    Not at all being critical, I am just learning to use Rhodes and I want to ensure that I am doing things correctly.

    Thanks

    ReplyDelete
  10. @cookie we don't have controller in Rhosync application. We have source adapter in rhosync application. Also the code which I have written will be in query method.
    http://wiki.rhomobile.com/index.php/Rhosync_Tutorial#A_RhoSync_Query

    we have MVC model in Rhodes application.

    ReplyDelete
  11. Thank you, very nice and easy to follow.

    ReplyDelete
  12. can we use AJAX in Ruby rhodes. how?

    ReplyDelete
  13. @Rahul you can use JQtouch and Sencha as Javascript framework. Refer this URL: http://docs.rhomobile.com/rhodes/ui#javascript-frameworks

    ReplyDelete
  14. how could i use JSON to retrieve data from server and use that data in javascript.Is it possible.Give me some code snippet if u can.

    ReplyDelete
  15. @Rahul It depend if you are using RhoSync or not :

    If you using RhoSync :
    Then 2 point of Step 2 in this blog will store all these JSON objects in your Twitter table in RHodes application( Whatever Hash we store in @result of query method is store in corresponding model).
    Then you can use Rhom to retrieve the values and can use anywhere.

    It you are not using Rhom:
    You can use asynchttp calls, there are examples and tutorial at :
    http://docs.rhomobile.com/rhodes/connect-to-web-services#asynchttp-example

    Let me know which way you are using so that I can explain you in more detail.

    ReplyDelete
  16. how to connect to a free webservice in source adapter

    ReplyDelete
  17. you can connect Webservice easily with your source adapter. You have to give output of your webservice in JSON or XML format. And then connecting will be similar to step 2) -2 of above example.

    ReplyDelete
  18. Hi,
    I am new to Rhodes, I am writing an app for a API which uses OAuth authentication. Is there any way to do this using Rhodes or should I go with Rhosync, if so could you please explain.
    Thanks in advance.

    ReplyDelete
  19. Hi Surendran
    I did not get the chance to use Oauth I have used basic auth and it work good for me.....

    If you want to use Oauth this discussion may help you:

    http://groups.google.com/group/rhomobile/browse_thread/thread/91676dbed962be13/ad11aa5e56f170f8?lnk=gst&q=Oauth#ad11aa5e56f170f8

    Do share your experience with Oauth?

    ReplyDelete
  20. Hi Abhi

    i am new to rhodes.
    i need to connect the twitter and get the token url to post the message form the application.
    r u having any tutorial for this...
    Thanks in advance.

    ReplyDelete
  21. Really awesome and great job Abhishek :)

    ReplyDelete
  22. Hi Please i need an project that should show any one notification for every 5 sec please help....

    ReplyDelete
    Replies
    1. My mail id is ashokramcse@gmail.com please send me...plz

      Delete