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

Posted by Augusto Guido on April 29, 2009

Hey, thanks for coming back for part 2. I know it took some time to start writing this second part, it’s just that facebook keeps getting better and keeps taking my time away (don’t tell Conrado).

If I remember correctly in Part 1 we ended up with facebooker installed, configured and running. We even added the facebook connect button and explained how to use some of the great facebooker helpers. As promised in my last post we are going to explain a bit how the magic happens with XFBML, invite friends and publish feed items.

XFBML

Facebook uses XFBML as a way for you to incorporate FBML (Facebook Markup Language, an extension to HTML) into an HTML page on a Facebook Connect site or an iframe application. read more here.

This is a typical XFBML tag, it brings up the profile picture of the user with the uid=”12345″.

<fb:profile-pic uid="12345" facebook-logo="true" linked="false" width="300" height="400"></fb:profile-pic>

What’s happening here? Facebook is turning this into a typicall HTML <img> tag. They do this using a Javascript cross-domain communications library. You can read more here if you are interested. This is all done for you when using Facebooker.

Boring right? Let’s invite some friends to our connect site to keep things more interesting.

Inviting Friends

If you went through the facebooker helpers you are probably thinking about using fb_multi_friend_selector to select friends. Well you are right! We’ll be using that helper, but we will need it inside another helper that is fb_request_form. There’s also another helper that could help us that is fb_multi_friend_request, which is basically the first two together, but we are going to use the first option. Here’s the resulting code:

<% fb_serverfbml do %>
<script type="text/fbml">
<fb:fbml>
<% content_for("invite_user") do %>
<%= "Check out my brand new FB Connect site.  Lots of good stuff in there! #{fb_req_choice('Check it out!', login_users_url)}" %>
<% end %>
<% fb_request_form("GetUnbored","invite_user", login_users_url) do %>
<%= fb_multi_friend_selector("Invite your friends to check out this site", :showborder => true,
:exclude_ids => facebook_session.user.friends_with_this_app.map(&:id).join(","), :condensed => false) %>
<% end %>
</fb:fbml>
</script>
<% end %>

I don’t want to explain something that’s already out there in many places, I will just describe a bit what’s going on. We put things inside a fb_serverfbml, because we are in a facebook connect site and need users interaction with facebook directly (that is when selecting their friends). The content_for(“invite_user”) is the content that will be show in the fb_request_form. The fb_request_form is a facebook form used when we need to submit information to facebook.

And the fb_multi_friend_selector is the nice facebook like friend selector. You can choose condensed => true to show an ugly smaller one. The exclude_ids => facebook_session.user.friends_with_this_app.map(&:id).join(“,”) is pretty great, it makes the friend selector not to show the friends who are already using our facebook connect site.

Anyway, you can get much more things done, here are some of the facebooker helpers to do anything you like with them ;) . And of course the facebook developers wiki is a great place for starting and becoming a guru. I will add in other post the publishing feeds part.

Enjoy!

Transferring directory trees over ftp

Posted by Gian Zas on April 19, 2009

Imagine all the people having to live without ssh and an assigned job related to hosting migrations with low bandwidth access, what a painfull world…

Ok, get up! it was only a nightmare, but if a strange reason causes you must accomplish that task you may wish get/put directories recursively from/to a ftp server.

The bad news are that the FTP protocol doesn’t supports this operation, you can get only an individual file or a group of files that expands some wildcard expression, but you can’t get/put recursively an entire directory.

Luckily ncftp saves the day, it’s a free ftp client (free as in beer and free as non-private), that supports many features like background processing and directory tree copy.

So, to GET the contents a whole directory tree just invoke ncftpget command:

$> ncftpget -R -u <user_account> ftp.moove-it.com /home/gian/migration_h /remote_directory

* -R copy a whole directory
* ftp.xxx.x is the remote ftp server
* /home/yyy is the local destination
* and /remote_xx is the ftp directory to be transfered

PUT a directory is trivial too, just invoke the ncftpput command:

$> ncftput -R -u <user_account> ftp2.moove-it.com /home/gian/migration_h /remote_directory

be happy!

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.

Improving the ruby Array class without degrade performance

Posted by Pablo Ifran on April 06, 2009

Improving the ruby Array class

Many times, when you are iterating over an array you want to know the last or the first element to do something. Unfortunately ruby don’t provide any method to help you doing this task.
So, if you want a trick to do this, you can add this few lines in a rails project inside an initalizer.

Put this code in initializers/array.rb

class Array

  #     array.each_with_first {|item, first| block }   ->   array
  #
  #
  # Call a block ones per element, passing
  # the element and the first element of the array as parameters.
  #
  #    a = [ "a", "b", "c" ]
  #    a.each_with_first {|x, f| print "#{x} -- #{f} | " }
  #
  # produces:
  #
  #    a -- a | b -- a | c -- a |
  #
  def each_with_first(&block)
    first = self.first

    self.each do |element|
      yield element, first
    end
  end

  #     array.each_with_last {|item, last| block }   ->   array
  #
  #
  # Call a block ones per element, passing
  # the element and the last element of the array as parameters.
  #
  #    a = [ "a", "b", "c" ]
  #    a.each_with_last {|x, f| print "#{x} -- #{f} | " }
  #
  # produces:
  #
  #    a -- c | b -- c | c -- c |
  #
  def each_with_last(&block)
    last = self.last

    self.each do |element|
      yield element, last
    end
  end

  #     array.each_with_first_and_last {|item, first, last| block }   ->   array
  #
  #
  # Call a block ones per element, passing
  # the element, the first and the last element of the array as parameters.
  #
  #    a = [ "a", "b", "c" ]
  #    a.each_with_last {|x, f, l| print "#{x} -- #{f} -- #{l} | " }
  #
  # produces:
  #
  #    a -- a -- c | b -- a -- c | c -- a -- c |
  #
  def each_with_first_and_last(&block)
    first = self.first
    last = self.last

    self.each do |element|
      yield element, first, last
    end
  end

end

Hotmail sends my mails to junk! why? 2

Posted by Ariel Ludueña on April 03, 2009

spamOk, first of all i’m a good guy and I’m not a spammer. I’m simply developing a SN and I want to send mails between its members.
I have everything nicely configurated (SPF, DomainKeys, reverse DNS for the mail server, all the stuff). Why do my mails go to the junk folder?

Hotmail has as “smart” guy called the SmartScreen filter. This guy is responsible for filtering the mails based on different patterns: email content, IP reputation, … if this guy marks you as a possible spammer you’re in trouble.
The only way that you have is to talk with the hotmail guys.

To do that, submit a form with your mail server information => https://support.msn.com/eform.aspx?productKey=senderid&ct=eformts
Hotmail guys will contact you for further information regarding your site and your mail policy.
After 4 or 5 mails between you and hotmail support, if it’s all okay, they will  help you with a mitigation to your IP :)
“I am pleased to inform you that we have taken steps to implement a temporary mitigation to your mail delivery problem on your IP (206.212.246.210). The mitigation will take 24-48 hours to reflect fully within our system.”

Congrats!

I also suggest to enroll to the Sender Score program at http://www.senderscorecertified.com (the only white list that Hotmail uses).

Good luck!