Implementing reCAPTCHA in your rails app

Adding reCaptcha to rails is quite easy

First create an account with reCAPTCHA and get your API keys.

Secondly get the ambethia-recaptcha gem from github http://github.com/ambethia/recaptcha You can either install it as a gem by adding

config.gem “ambethia-recaptcha”, :lib => “recaptcha/rails”, :source => “http://gems.github.com”

to your environment.rb file within the config block, then from your rails app root you run the following command

sudo rake gems:install

Or you can install it as a plugin and this does not require you to add the gem config line into your environment.rb file, simply go to your rails app root and run the command

script/plugin install git://github.com/ambethia/recaptcha.git

You need to also add the API keys and there are several ways of doing this but I go with the simplest, simple add the following line to the bottom of the environment.rb file (make sure its not in the config block)

ENV['RECAPTCHA_PUBLIC_KEY']  = ’6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy’
ENV['RECAPTCHA_PRIVATE_KEY'] = ’6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxxx’

replace the strings with your API keys else your captcha wont work.

Finally you can start using reCAPTCHA by adding the following help to your forms in the view

<%= recaptcha_tags %>

To verify the captcha simple add verify_recaptcha() to your controller like the below

Old Code

if @user.save
redirect_to :action => “login”
flash[:notice] = “Your account setup is almost complete please check your mail for activation link.”
end

New Code

if verify_recaptcha() and @user.save
redirect_to :action => “login”
flash[:notice] = “Your account setup is almost complete please check your mail for activation link.”
end

This will properly validate the captcha image but will not display any error to the user if the validation fails. You can add this extra params to the verify_recaptcha method

verify_recaptcha(:model => @user, :message => “Please re-enter the words from the image again!”)

HELPER CUSTOMIZATIONS

Ajax

<%= recaptcha_tags :ajax => true %>

For SSL

<%= recaptcha_tags :ssl => true %>

Themes

<%= recaptcha_tags :display => {:theme => “white”, :tabindex => 0} %>

You can get further directions on the Gem from http://github.com/ambethia/recaptcha

Thats all folks.

Extracting audio length using Rails

Was working on a module in one of my Rails projects and I had to find the length of the audio file clients uploaded to that they can be billed accordingly, since this was voice sms it was very crucial. I combed the whole internet for solutions and could not find any, came across a few gems that were great but lacked what I really needed, so I decided to sit my butt down and role out the code myself.

The first idea was using FFMPEG on the command line to extract the length, did some few searches and came across the exact line I needed. You need to install ffmpeg first before this will work. I code on Mac OS X, my personal production server at home is RedHat Enterprise 5 and the main production server is running Ubuntu Linux. so I have the luxury of learning all three install methods.

For mac you need macport installed then you run

sudo port install ffmpeg

On RHEL5 i had a lot of dependency problems but resolved them by adding the ATRPMS Repo (Really a saved me), how to add the repo to you Linux distribution is available on thier website. I wont cover it here, once you done, you can install ffmpeg with

sudo yum install ffmpeg

On Ubuntu Linux ( Debian Systems), you install ffmpeg using

sudo apt-get install ffmpeg

All said and done lets try a commandline demo and see it it really works.

ffmpeg -i “/Users/nukturnal/Desktop/try.mp3″ 2>&1 | grep ‘Duration’ | cut -d ‘ ‘ -f 4 | sed s/,//
we get something close to this result 00:03:45.44
Now the Ruby Code
Yep thats all folks nice and hardcore.

Rails 2.3.5 gem update (dependency errors fixed)

I recently updated my rails gem and realised i was getting errors trying to create new rails projects in 2.3.5, below is the error i was getting.

I went through a few debug modes checked but came up with no lasting solution. I finally checked for my installed gems “sudo gem list” and realised i had “Rack 1.0.0″ installed and I upgraded my rack version with the command “sudo gem update rack” I rechecked my gem list and realised i now i had Rack 1.0.0 and Rack 1.1.0 installed. Great! right? tried to launch my project again and I saw the same errors again, Uhh! Took another closer look again a realised somehow it depended more or less on Rack 1.0.1 which has been skipped by the update to the lastest version of Rack. So this meant we have to install that particular version of rack. I used the following command

sudo gem install -v=1.0.1 rack

Rechecked my rack verisons and now I had all three verisons, tried my project again and behold Rails was booting nicely :)