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
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.
Hi Abhishek, nice tutorial . Will try it out.
ReplyDeleteThanks
great article..can you please explain this code;
ReplyDeleteparsed.each do |item|
item_hash = {}
item_hash['text'] = item['text']
@result[item['id'].to_s] = item_hash
end
@result
end
Yes sure Femi..
ReplyDeleteFirst 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.
thanks for this but i see that the column in the model is called 'twitter'..
ReplyDeletei 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..
@Femni
ReplyDeletewe 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" }
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
ReplyDeleteitem_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"}
}
Hi Guyz
ReplyDeleteI 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.
Hi Davender,
ReplyDeleteAs 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/
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?
ReplyDeleteNot at all being critical, I am just learning to use Rhodes and I want to ensure that I am doing things correctly.
Thanks
@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.
ReplyDeletehttp://wiki.rhomobile.com/index.php/Rhosync_Tutorial#A_RhoSync_Query
we have MVC model in Rhodes application.
Thank you, very nice and easy to follow.
ReplyDeletecan we use AJAX in Ruby rhodes. how?
ReplyDelete@Rahul you can use JQtouch and Sencha as Javascript framework. Refer this URL: http://docs.rhomobile.com/rhodes/ui#javascript-frameworks
ReplyDeletehow 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@Rahul It depend if you are using RhoSync or not :
ReplyDeleteIf 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.
how to connect to a free webservice in source adapter
ReplyDeleteyou 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.
ReplyDeleteHi,
ReplyDeleteI 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.
Hi Surendran
ReplyDeleteI 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?
Hi Abhi
ReplyDeletei 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.
Really awesome and great job Abhishek :)
ReplyDeleteHi Please i need an project that should show any one notification for every 5 sec please help....
ReplyDeleteMy mail id is ashokramcse@gmail.com please send me...plz
Delete