Trick to improve performance in rails, less requests with static resources 2

Posted by Pablo Ifran on April 08, 2009

Reducing the number of request made to the server improves the performance of a web application in about 80%.

There are many techniques that allow us to reduce the amount of requests that are made on a page, among them are: the sprites, put the stylesheets on top of the page, javascripts compress, among others.

But what’s offered by Rails to improve the performance of our web application?

It offers a great plugin called bundle_fu (http://code.google.com/p/bundle-fu/)
It allows us with a single request obtain all the javascripts and with another request all the stylesheets  (it also offers the possibility of compress javascripts).
Using this plugin is really easy but it’s very powerfull

<% bundle do -%>
  <%= javascript_include_tag :default -%>
  <%= javascript_include_tag "javascript1" -%>
  <%= javascript_include_tag "javascript2" -%>
  <%= javascript_include_tag "javascript3" -%>
  <%= stylesheet_link_tag "style1" -%>
  <%= stylesheet_link_tag "style2" -%>
  <%= stylesheet_link_tag "style3" -%>
  ...
<% end %>

All these javascripts and stylesheets are converted in only two files when the request is processed.

moove-iT@locosxrails

Posted by Martin Cabrera on April 07, 2009

Locos x Rails is the first conference in the Southern Cone dedicated to the ground-breaking Ruby on Rails framework. Locos por Rails Conference 2009 will be held on April 3rd and 4th in Buenos Aires, Argentina. South America’s most popular travel destination is the perfect backdrop for two days of local and international presentations, networking, and fun.

Part of the moove-iT development team attended to this event. Bellow you will find some nice pictures!

See more photos on facebook group and keep the thread news at locosxrails twitter.

Using Facebooker to make a Rails site with Facebook Connect (Part 1) 14

Posted by Augusto Guido on April 06, 2009

I think that from the title you can pretty much guess what this post will be about, specially if you are familiar with these magic words: Rails, Facebooker, Facebook Connect. Ok they are not that magic, but you can do lots of fun stuff with them. In case you don’t live in the facebook planet I will briefly explain them, since there is a lot of info out there about them I wont get into detail. I won’t explain Rails for obvios reaons.

Facebook Connect

It’s something (who knows what and who cares anyway?) from Facebook that allows you to use your facebook login to login into other web sites.

Facebooker

It’s a gem for Ruby, and also a plugin for Rails that converts the results from the facebook API into ruby objects so you can interact with the API using just ruby.

Let the magic begin…

We want to have a site that handles users like we would normally do, except we won’t handle nor model them. We will get them from facebook. The first thing we want to do is join the developers group in facebook, go to http://www.facebook.com/developers/ and create a new application. You will then have to configure some couple of things, the main one is “Connect URL” in which you should put the url of your site like: “http://my_new_fb_app.com/”. Do not forgett the last slash, it won’t work if it’s not there (and you can spend hours trying to figure out what’s wrong). You have many other setting but we won’t get to them here.

As an advice, you may want to create two apps so that you can have one for development and one for production. the development one for example can point to http://localhost:3000/

Other thing you may see is that facebook gives you some strange numbers after you create your app, now is when we start with facebooker. Follow this tutorial until point 5, we won’t care too much for the other stuff, but you can read it. I want you to know how to install facebooker (and to actually do it),  and then generate and complete the facebooker.yml file.

Now what?

After you have that completed the first thing we wan’t is to show this little pretty blue button  facebook connect. This is the login button, and when you click it a pop up from facebook asking your password and email should appear. So, let’s make that happen.

Add this to your application controller

before_filter :set_facebook_session
helper_method :facebook_session

Add the followng lines to one of your views (typically a login page)

<%= fb_connect_javascript_tag %>
<%= init_fb_connect "XFBML"%>
<%= fb_login_and_redirect(facebook_login_users_path) %>

Now you should be seeing the button after you refresh. The facebook_login_users_path is the url you want to redirect your users after they login. As usual in Rails facebook_login is the action and users the controller.

After the user logges in you will have a facebook_session variable abailable to do almost whatever you want. For example you can:

facebook_session.user, this will return you the facebook user and all of it’s methods. Everything you can do with it it’s here http://facebooker.rubyforge.org/classes/Facebooker/User.html. The typicall things you may want are facebook_session.user.first_name, facebook_session.user.last_name, facebook_session.user.first_name.id (this will return an id facebook provides. You can access with it the user any other time).

Other interesting thins you can do is facebook_session.friends to get all the user friends or  facebook_session.user.friends_with_this_app will return all the users that use this application and are already your friends in facebook.

Well, that’s it for now. In part 2 we will discuss a bit about XFBML (facebook markup language) and show you how to invite friends to your app, show stories in their dashboard, etc. Also a little example of using javascript to call the Facebook API.

Interfacing with Twitter 6

Posted by Gian Zas on March 31, 2009

twitter_birdTweet tweet tweet! All the farm is chating about Twitter, all the farm is using twitter and all the farm is building applications on top of it.

Luckily, Twitter seems to be a programmer’s best friend, why? you may be asking… because of its wonderful API. Well designed and documented, what else could you wish?

The Twitter API is RESTful, but it doesn’t only mean that you can access to it over HTTP, sending parameters in GET or POST requests, it also means that it is designed complying with the principles of REST. A plus to this API is that it supports JSON, XML, RSS and Atom data formats and user authentication is reached by Basic Auth.

Due to the popularity of the service, a bunch of libraries have been developed and are heavily used to interact with Twitter, but if you like to explore the other end of the telephone line you are reading the right post ;) . As we’ve said the API is accessible through HTTP, so let’s go and fire up your browser and point to:

http://twitter.com/statuses/user_timeline.xml?screen_name=twitterapi

Ok, what do you see? the last tweets of the user whose screen name is twitterapi formatted in xml. Another popular operation is:

http://twitter.com/statuses/friends_timeline.json?

That url returns (after successful authentication) the latest tweets posted by the authenticated user and it’s friends.

Because requesting a URL and satisfying basic authentication mechanisms are easy tasks in the day-to-day programming languages, invoking the API is a piece of cake. As Python has been one of our favorite languages, we’ll show you how to retrieve user statuses and post a new one. Here we go!

import simplejson as jsonimport urllibimport urllib2
GET_STATUS_URL = 'http://twitter.com/statuses/user_timeline.json?'UPDATE_STATUS_URL = 'http://twitter.com/statuses/update.json?'

First we must declare the necessary imports (simplejson must be downloaded and installed before) and declare the URLs to be accessed.

def get_statuses(screen_name):    data = {'screen_name' : screen_name}    url = GET_STATUS_URL + urllib.urlencode(data)    f = urllib2.urlopen(url)    response = ''.join(f.readlines())    return json.loads(response)

get_statuses retrieves the last tweets of #{screen_name}, it simply adds the screen_name parameter to the url, then requests it and finally parse the response using the json library. To print the tweets retrieved in a simple manner:

    statuses = get_statuses(username)    for status in statuses:        print "# " + status['user']['screen_name'] + ": " + status['text']

To post a tweet we simply do a POST request to the UPDATE_STATUS_URL passing the status (the tweet’s text) parameter.

def update_status(username, password, status):    __authenticate(username, password)    data = {'status' : status}    f = urllib2.urlopen(UPDATE_STATUS_URL, urllib.urlencode(data))    response = ''.join(f.readlines())    return json.loads(response)

Username and password corresponds to the twitter user (you for example) whose status will be updated, obviously the user (ex: you again) must have a twitter account.

Because the API uses basic authentication we call to the __authenticate function:

def __authenticate(username, password):    passman = urllib2.HTTPPasswordMgrWithDefaultRealm()    passman.add_password(None, TWITTER_HOST, username, password)    auth_handler = urllib2.HTTPBasicAuthHandler(passman)    opener = urllib2.build_opener(auth_handler)    urllib2.install_opener(opener)

For a good explanation on how this chunk of code works, please refer to  this great tutorial

As you’ve seen, in a small amount of code we are interacting with Twitter, where to use it, how to display the results and what ideas bring
to life is up to you!

My personal information belongs to Mark :S

Posted by Ariel Ludueña on February 17, 2009

facebook Today I logged to my facebook and my home was like always.. nothing new… nothing told me that the ownership and proprietary rights had changed, and nobody told me that my personal information now belongs to Mark (including all my beautiful pictures!)
What happened with the “Accept” and “Not accept” buttons? (maybe Mark forgot about this…)

I was thinking about why this happened…
Maybe Mark needs money! How much does the data of the largest social network in the world cost? Surely a lot of money. Imagine if the data belongs to the social network rather than the users themselves.
Maybe Mark has development problems! If a user is deleted from facebook, what happens with all the information linked to his friends? (delete in cascade? :P )

I don’t know what happens here and I don’t care. Obviously this is annoying because you don’t have control of anything and this change was not clear at all.

This is the paragraph of the controversy:

You hereby grant Facebook an irrevocable, perpetual, non-exclusive, transferable, fully paid, worldwide license (with the right to sublicense) to (a) use, copy, publish, stream, store, retain, publicly perform or display, transmit, scan, reformat, modify, edit, frame, translate, excerpt, adapt, create derivative works and distribute (through multiple tiers), any User Content you (i) Post on or in connection with the Facebook Service or the promotion thereof subject only to your privacy settings or (ii) enable a user to Post, including by offering a Share Link on your website and (b) to use your name, likeness and image for any purpose, including commercial or advertising, each of (a) and (b) on or in connection with the Facebook Service or the promotion thereof

UPDATE 17/02: Facebook has rolled back to it’s previous TOS, surely because of all the negative reactions this caused in the online community. This happens when this type of changes, which are inherently controversial, are poorly communicated and explained AFTER being implemented. This is a sensitive issue, and before I am stripped apart of the ownership of MY data I would like to be at least told about it. (source: http://www.techcrunch.com/2009/02/17/facebook-backtracks-under-community-pressure-goes-back-to-old-tos-for-now/)