Posts Ruby on Rails: link_to routes scoping twitter urls usernames views
by bseanvt
6 comments
Scope Routes/URLs By Username (like Twitter) in Your Rails Application
There are a few things that need to be taken care of before you can get this to work. The first thing (although, any of the following steps can be done in any order) to take care of involves your User model. You need to override the to_param method, so that Rails will appropriately use the username attribute rather than user_id when constructing paths.
#in app/models/user.rb
def to_param
"#{self.username}"
end
Next we move onto routing our resources. Here it gets a little tricky because Rails is building paths for us. Remeber, you can get a list of all currently defined routes in your application by running the routes rake task
rake routes
We need to set the path_prefix option on any of our resources we want scoped by the username. For instance, in this example, I have set up a Status model and statuses_controller, whose urls shall be scoped by the username. You can apply the path_prefix to any number of other resources in your routes config file. They symbol used is arbitrary, but will be made available in the params hash, in this case params[:user_id]. You also need to exclude the show action on your users resources declaration. The reason is that, otherwise, Rails will include the controller name in the path like /users/username, which doesn’t look as clean as just /username. You then need to redefine this route explicitly (last line in the routes config shown here).
#in app/config/routes.rb map.resources :statuses, :path_prefix => '/:user_id' map.resources :users, :except => [:show] map.user '/:username' :controller => 'users', :action => 'show'
Finally, you get to call these routes in your views or use them in your controllers. You use the same link_to, url_for methods to generate paths. When constructing the resources you have setup with the path_prefix declartion, remember you need the user model as the first argument, followed by said resource.
<%= link_to(status.title, status_path(status.user,status) %> # or in a controller redirect_to status_path( status.user, status )
That’s pretty much it. If anyone has another way of doing this let me know!
Grab a Twitter Status without the Twitter API
Quick and dirty way to grab the users status messages without having to go through the twitter api (not having to authenticate that is). You can grab the RSS feed and indicate the number of statuses returned with the count param. I wrote this function for my blog in PHP. It uses the curl extension so if it’s not installed type
apt-get install php5-curl
Otherwise you’ll have to use the fsockopen function. Then it’s just a matter of parsing the XML and getting at what data you want. I just want one status so I’m accessing the element directly, rather than looping over the returned data set.
<php
function latest_twitter_status_message(){
// grab the rss feed link from some user, like me and indicate the count in the url
$host = "http://twitter.com/statuses/user_timeline/11036982.rss?count=1";
// using curl
$ch = curl_init($host);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$doc = new SimpleXmlElement($data, LIBXML_NOCDATA);
return "{$doc->channel->item[0]->description}";
}
Updating Your Twitter Status with cURL and a Bash Function
I’m usually at the command line so I wrote a little a bash function so that i can type
tweet this is really neat but kind of pointless
and it will update my twitter status! some characters trip it up but in general it’s useful for most of my tweets. The tweet function just spits out the arguments passed to it for the status parameter for the API call to twitter.
Add the following to the .bash_profile file and reload the terminal (don’t forget to add your email and pwd where appropriate).
tweet() {
curl -u your_twitter_email_addr:your_twitter_passwd -d status="$*" http://twitter.com/statuses/update.xml
}
*** Twitter still uses http basic authentication for their API. However, they are moving away from it in favor of oAuth. So I’m not sure how long this fun will last :{
Programming: development environment installation java mac os x scala twitter
by bseanvt
2 comments
Installing Scala on Mac OS X Leopard
I followed the instructions from here http://arvinderkang.com/2009/09/01/installing-scala-on-snow-leopard/ but I used version 2.7.6 (rather than .5) I installed scala under /usr/local/scala so I had to include it to my path. Type
vim .bash_profile export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/scala/scala-2.7.6.final/bin:$PATH"
Now to download and install…
curl -O http://www.scala-lang.org/downloads/distrib/files/scala-2.7.6.final-installer.jar sudo java -jar scala-2.7.6.final-installer.jar
This will launch an installer. Just follow the instructions and when it asks for the destination folder, remember that it’s in /usr/local/scala
Simple as pie!


