As Rails developers we are all in love with keeping things simple. As Einstein said: “Make everything as simple as possible, but not simpler”. You may not agree with the theory of relativity, but you should really agree on this one.
A couple of months ago I started my final major project: an iPhone based application. It’s been a really great journey so far and I’ve learned so many amazing things about iPhone development. I found it very similar in some ways with Rails, being the biggest one the way they both keep things as simple as they should be. Well they both use the MVC architecture, but right now I’m talking beyond that stuff. It’s more about an ideology on how to build stuff, web and mobile applications in this case.
As an idea here at moove-it we thought of having an iPhone application for our faltauno.com project, kinda like facebook does. So the research began on how this could be done, the first (and probably definitely) answer appeared quickly. The guys at iphoneonrails.com have developed ObjectiveResource.
“ObjectiveResource is an Objective-C port of Ruby on Rails’ ActiveResource. It provides a way to serialize objects to and from Rails’ standard RESTful web-services (via XML or JSON) and handles much of the complexity involved with invoking web-services of any language from the iPhone.”. What’s not to love in that sentence? I won’t get into it since I haven’t used that much, and who could explain better than themselves?. In case you are thinking it will be too complex to get started, you can download the whole package with an example application that does all the basic stuff you are probably thinking on trying to do right now.
The example is a typical Rails application that can be handled using an iPhone application, which is also inside the example, you should of course have installed XCode. You then start the rails app and the iPhone simulator running the other one, and something kind of magic starts happening. What amaze me the most is the simplicity of the code you’ll need to write (of course
). Really try it out it’s worth it.
Don’t forget to tell us about your experience!
Rails & iPhone Applications: Simple Mix
software is not everything in life – championships in faltauno.com “Copa CUTI” 1
Hello everyone … in moove-iT we develop and management faltauno.com.
It is a social network specializes in organizing matches, managing a team and administer and manage a championship.
On August 1 begins the first championship was organized by faltauno.com “Copa CUTI” (www.cuti.org.uy).
This championship brings together all the software companies of uruguay.
We invite everyone to visit and see what are the best !
Ruby Benchmarks 2
One of the most popular rants against ruby are based on its not so good performance.
Actually has been made improvements on this topic, specially in the new Ruby implementation (Ruby 1.9) based on the YARV virtual machine.
JRuby (Ruby over JVM implementation) has been focused in its performance from its lastest releases. Here at moove-it we are exploring the posibility of use Rails over JRuby in some JEE application servers, so we need some facts about JRuby performance (and other topics like gems compatibility, etc)
The Ruby community has put a set of benchmarks at ruby1.9 trunk: http://svn.ruby-lang.org/repos/ruby/trunk/benchmark/
So, we test with these benchmarks and here are the results!.
The code that run the benchmarks is like that:
block_to_benchmark = lambda { load BENCHMARKS_DIR + '/' + filename}
Benchmark.measure &block_to_benchmark
(yes, we are using the benchmark module bundled with the ruby standard lib)
|
Benchmark |
Ruby1.8 |
Ruby1.9 |
JRuby1.3.0 |
Ruby1.8 / Ruby1.9 |
Ruby1.8 / JRuby1.3.0 |
| bm_app_fib.rb | 9.02 | 4.00 | 3.83 | 2.25 | 2.36 |
| bm_app_mandelbrot.rb | 3.36 | 0.81 | 1.49 | 4.14 | 2.26 |
| bm_app_pentomino.rb | 144.24 | 91.97 | 105.64 | 1.57 | 1.37 |
| bm_app_raise.rb | 6.94 | 6.98 | 1.63 | -1.01 | 4.27 |
| bm_app_strconcat.rb | 2.94 | 1.63 | 1.30 | 1.81 | 2.27 |
| bm_app_tak.rb | 12.27 | 5.66 | 4.06 | 2.17 | 3.02 |
| bm_app_tarai.rb | 9.81 | 4.83 | 3.27 | 2.03 | 3.00 |
| bm_app_uri.rb | 6.59 | 3.77 | 3.69 | 1.75 | 1.79 |
| bm_io_file_create.rb | 7.17 | 3.12 | 7.70 | 2.30 | -1.07 |
| bm_io_file_read.rb | 2.27 | 0.61 | 0.64 | 3.71 | 3.54 |
| bm_io_file_write.rb | 1.77 | 10.98 | 0.31 | -6.22 | 5.64 |
| bm_loop_for.rb | 2.97 | 7.81 | 7.50 | -2.63 | -2.53 |
| bm_loop_generator.rb | 149.64 | 3.00 | 10.95 | 49.88 | 13.66 |
| bm_loop_times.rb | 4.88 | 6.88 | 7.95 | -1.41 | -1.63 |
| bm_loop_whileloop.rb | 11.23 | 3.67 | 9.42 | 3.06 | 1.19 |
| bm_loop_whileloop2.rb | 2.33 | 0.72 | 1.89 | 3.24 | 1.23 |
| bm_so_array.rb | 8.41 | 7.33 | 16.17 | 1.15 | -1.92 |
| bm_so_binary_trees.rb | 4.50 | 2.14 | 2.84 | 2.10 | 1.58 |
| bm_so_concatenate.rb | 2.49 | 1.95 | 3.03 | 1.27 | -1.22 |
| bm_so_exception.rb | 7.62 | 10.42 | 2.63 | -1.37 | 2.90 |
| bm_so_fasta.rb | 13.59 | 11.77 | 16.03 | 1.16 | -1.18 |
| bm_so_lists.rb | 2.27 | 1.38 | 1.48 | 1.65 | 1.53 |
| bm_so_mandelbrot.rb | 44.55 | 32.49 | 49.28 | 1.37 | -1.11 |
| bm_so_matrix.rb | 2.69 | 2.13 | 1.84 | 1.26 | 1.46 |
| bm_so_meteor_contest.rb | 52.55 | 25.17 | 22.02 | 2.09 | 2.39 |
| bm_so_nbody.rb | 35.80 | 26.61 | 16.78 | 1.35 | 2.13 |
| bm_so_nested_loop.rb | 6.09 | 6.88 | 8.64 | -1.13 | -1.42 |
| bm_so_nsieve.rb | 26.89 | 13.11 | 24.69 | 2.05 | 1.09 |
| bm_so_nsieve_bits.rb | 62.50 | 46.05 | 42.55 | 1.36 | 1.47 |
| bm_so_object.rb | 11.56 | 11.52 | 3.11 | 1.00 | 3.72 |
| bm_so_partial_sums.rb | 80.13 | 228.91 | 31.22 | -2.86 | 2.57 |
| bm_so_pidigits.rb | 10.33 | 10.44 | 7.03 | -1.01 | 1.47 |
| bm_so_random.rb | 4.59 | 12.88 | 1.86 | -2.80 | 2.47 |
| bm_so_sieve.rb | 0.84 | 0.34 | 0.63 | 2.45 | 1.35 |
| bm_so_spectralnorm.rb | 41.86 | 92.88 | 20.03 | -2.22 | 2.09 |
| bm_vm1_block.rb | 26.22 | 13.44 | 27.44 | 1.95 | -1.05 |
| bm_vm1_const.rb | 19.02 | 6.27 | 17.64 | 3.03 | 1.08 |
| bm_vm1_ensure.rb | 20.06 | 5.11 | 16.19 | 3.93 | 1.24 |
| bm_vm1_ivar.rb | 17.95 | 9.50 | 18.78 | 1.89 | -1.05 |
| bm_vm1_ivar_set.rb | 19.22 | 9.83 | 21.67 | 1.96 | -1.13 |
| bm_vm1_length.rb | 22.95 | 7.44 | 18.16 | 3.09 | 1.26 |
| bm_vm1_neq.rb | 20.81 | 6.58 | 14.98 | 3.16 | 1.39 |
| bm_vm1_not.rb | 14.91 | 5.58 | 12.64 | 2.67 | 1.18 |
| bm_vm1_rescue.rb | 15.72 | 4.64 | 21.38 | 3.39 | -1.36 |
| bm_vm1_simplereturn.rb | 23.84 | 9.66 | 16.05 | 2.47 | 1.49 |
| bm_vm1_swap.rb | 50.25 | 5.73 | 24.38 | 8.76 | 2.06 |
| bm_vm2_array.rb | 10.72 | 19.27 | 5.42 | -1.80 | 1.98 |
| bm_vm2_case.rb | 5.06 | 1.66 | 4.00 | 3.06 | 1.27 |
| bm_vm2_eval.rb | 32.20 | 200.98 | 69.41 | -6.24 | -2.16 |
| bm_vm2_method.rb | 15.45 | 9.45 | 11.36 | 1.63 | 1.36 |
| bm_vm2_mutex.rb | 5.47 | 6.34 | 7.30 | -1.16 | -1.33 |
| bm_vm2_poly_method.rb | 20.61 | 12.16 | 21.05 | 1.70 | -1.02 |
| bm_vm2_poly_method_ov.rb | 5.00 | 1.66 | 4.53 | 3.02 | 1.10 |
| bm_vm2_proc.rb | 12.00 | 3.86 | 6.75 | 3.11 | 1.78 |
| bm_vm2_regexp.rb | 5.89 | 19.25 | 5.63 | -3.27 | 1.05 |
| bm_vm2_send.rb | 5.05 | 2.11 | 4.38 | 2.39 | 1.15 |
| bm_vm2_super.rb | 5.75 | 3.17 | 4.44 | 1.81 | 1.30 |
| bm_vm2_unif1.rb | 5.20 | 1.99 | 3.61 | 2.62 | 1.44 |
| bm_vm2_zsuper.rb | 6.87 | 3.48 | 5.49 | 1.97 | 1.25 |
| bm_vm3_thread_create_join.rb | 1.95 | 7.70 | 19.13 | -3.94 | -9.79 |
| bm_vm3_gc.rb | 292.30 | 266.14 | 0.36 | 1.10 | 814.20 |
.
A looser conclusion may be that Ruby 1.9 is 95% faster than Ruby1.8, and JRuby 1.3.0 is 10% faster than 1.8, in general the new implementations are faster than Ruby1.8, especially 1.9 (twice as faster).
The benchmarks were under WindowsXP SP3, 4GB RAM, and a Intel Core 2 duo 2.0GHz. Happy hacking!
Using Facebooker to make a Rails site with Facebook Connect (Part 3) 3
Welcome to part 3 of the facebooker trilogy
I’m kind of tyred of writing the same title over and over again and changing just the last number, problem is there are so many things we can do with this stuff we could write tons of this. I could put in the title what we will talk about specifically in this post, but the idea is to follow this post/tutorials in order. This will probably be the last part of this set of posts, the following ones will refer in it’s specifically to what will be done.
So far we explained how to configure facebooker, sign up, invite friends and bit of the theory. So today we’ll publish something in the users dashboard, so him and all his friends can see it his wall. So go to
developers.facebook.com/tools.php and select “Feed Template Console”, select the application you want and click next.
Give me a minute to explain you what we are doing.
We are going to register our feeds in facebook, and then we are going to call them by a number they will give us.
You can do this using facebooker, or you can register them in the link I gave you above. I think that using the facebook tools is way easier and also facebooker may be a bit out of date regarding this area due to all the legal and business changes in facebook. Also not event facebook has yet clear what they want: “At this time, the policy on automatically publishing one line stories has not been finalized”.
Anyway, complete the step guide for feeds in facebook and keep the number of the template you created. BTW while completing it you will find this kind of stuff “{*actor*}” without quotes. These are tokens that allow to put variable stuff (like names, links, etc.), you can read about them while creating the templates since they are well explained over there.
Now, you have the story. You need the users approval to publish it, so what facebook does is shows the story to the user and gives him otions to skip or publish. If we are in luck the users hits “publish”.
So we now obviously want to show it to the user so he can choose. Here’s the code
FB.ensureInit(function() {
var body_general = "Join them in my facebook connect site"
var template_data =
{
"actor": "<%= @_logged_user.name %>",
"friends":"<%= @event.users.collect{|u| u.name_or_alias}.join(', ') %>",
"event": "<%= @event.description %>",
"place": "<%= @event.place %>",
"time" : "<%= @event.start.strftime('%m/%d/%Y %I:%M %p') %>"
};
var user_message_prompt = "<%= @match.comment %>";
FB.Connect.showFeedDialog('the_number_of_your_template', template_data, [], body_general, null, FB.RequireConnect.require, FB.RequireConnect.promptConnect, user_message_prompt);
});
In the template data you have to complete the tokens you created when publishing your story. You can everything pretty well explained in this couple of links:
http://wiki.developers.facebook.com/index.php/JS_API_M_FB.Connect.ShowFeedDialog
http://wiki.developers.facebook.com/index.php/Feed.publishUserAction
http://wiki.developers.facebook.com/index.php/Publishing_Feed_Stories_to_Facebook
Pay attention to the “FB.ensureInit(function(){”, I don’t know why I couldn’t find anywhere in facebook that mention this should be there. If you don’t add this nothing will happen, maybe it’s obvios for some people, but it wasn’t for me. I’m sory I can’t remember where I find this, but you can read here what it does.
Well, that’s kind of the big picture of what you have to do to publish stories, remember this isn’t supposed to be a complete tutorial of everything you can do, just a guide based on my personal experience to get started.
Your facebook connect site should be quite complete now that you can signup, invite your friends and write stuff that will appear directly in facebook. Not to mention if you have used all the other resources facebook give us and facebooker facilitate us.
Thanks for reading!
Using Facebooker to make a Rails site with Facebook Connect (Part 2)
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!
Trick to improve performance in rails, less requests with static resources
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
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) 13
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
. 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
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
Building a social network on Rails 7
So you want to build your own social network site on rails? As you probably know, there are many plugins around to add social functionality to your site, and there are also some open source platforms that provide you with complete social network functionality. Some of them are Lovd by Less, comunity engine, tog, etc. These platforms, as rails premises, are based in the idea of not reinventing the wheel. In this post we’ll be referring about tog our experience installing it and trying it out.
Why are we using tog? Actually we are not using it, we are just doing a proof of concept trying out the different platforms and getting to know each of them. The idea behind this is to have enough knowledge to, according to our clients requirements, select the best solution for them and for us in order to save our clients some money by reducing our development time.
What is tog? Tog is a non-intrusive, extensible open source platform that helps you to add social network features to your Ruby on Rails apps. We aim to convert the tedious and long process of creating a community site on a straightforward process that will give your site social capabilities in minutes. (from www.toghq.com/)
Installing tog may not be a simple procedure for a Rails beginner, but the install process is very well explained in their wiki. After following the procedure you will have installed a very complete and expandable social network. It is composed by many plugins developed by tog and some by 3rd parties. After installing the basic plugins you can install more plugins that will easily adapt to the default site.
Tog is today in version 0.4, which means you will probably find some bugs and things to fix. Here are some we ran into:
- After running the togify command the following error may appear: “error There has been a problem trying to unpack the tog_core tarball downloaded from github. Remove the changes made on your app by togify and try again. Sorry for the inconveniences. We found the solution in the project assembla here
- In the migration create_posts from plugin tog_conversation the line t.datetime :published_at is missing
Another thing you shouldn’t forget is to set up your email conf, since some plugins may depend on the user activation like the message ones (I tried to manually activate some users, and then had to manually fix some things than an observer was supposed to do after activation)
The greatest thing about tog is the ease of extending it. You can read about it here. Basically you can override any behavior you want just by creating models, controllers or views like you would normally do in your apps. On the cons side, it may be a bit complex finding out what functionality is where, to override with your own. The idea is to not touch the plugins and use new controllers, models, etc to extend. This will allow plugins to be updated when they need to, without affecting your site’s working functionality.
In conclusion, I think tog is a very promising platform that may be a great solution for building social networks on the go, allowing us to extend them to meet our needs. This may be a good solution for a user who just wants his site running. If you want to customize too much about it, you should consider other platform or starting from scratch.
We are right now working on adding Facebook Connect to a demo site we are building with tog, please come back to know how that went







