Quiet no more

I don’t do personal blog posts often. This post is an exception because the hemju blog was pretty silent for some time and I just wanted to let you know that the blog isn’t dead.

I worked till End of January for the Austrian startup Jumio. It was quite an experience (good and bad) and I learned a lot of how the startup scene works. Maybe I will write a follow up post about this experience because I believe many fellow start-uppers would benefit. However, time will help to give a more objective opinion.
So what I have done since then? I had the great luck to team up with a good friend of mine and start a company of our own, lingohub. lingohub will be what I envisioned for lingui.st. I won’t spoil much right now, but let’s say we want to change how software is translated for good. Actually our credo is, software that isn’t translated is broken. Hence, we set our standards high. You will hear a lot of it over the next few weeks and a beta is just around the corner.

So, the good news is you can expect this blog to be reactivated again. I will probably focus on development topics and startup topics like financing, contracts, HR, … as well. Should get very interesting.

Face Recognition with Ruby

———— DEPRECATED ————-

This post is no longer valid, since face.com was acquired by facebook and doesn’t provide an open API anymore.

We all know that we can search web pages for text, but there are services that go beyond a simple text search and use features like location and or image data. The hurdle relying on these features is that normally the end users have to enter manually the appropriate data. When was the last time you tagged people Facebook? Now think what if we have a system that helps us to automate the process of face recognition. Lets understand face recognition and how you can use it with Ruby.

What is Face Recognition?

Face Recognition uses feature extraction from facial points (like detecting where the eyes, nose, mouth etc are) within images or videos to recognize people. As you can image such a task isn’t trivial and requires a lot of know how. But the good news are that facial recognition just got a lot easier for developers to include in their applications. Face.com, a company that specializes in facial recognition, opened its platform APIs, allowing developers to integrate its facial recognition technology in third-party websites and applications.

Developers Get Facial Recognition API from Face.com

The face.com API uses REST-like interface. This means that all calls to the API are made over the Internet, using HTTP GET and POST requests to the face.com API server. Hence, any programming language that supports HTTP can be used to communicate with the service. Face.com has some common libraries to get you started.

How to use it with Ruby ?

Step 1: Get your API Key from http://developers.face.com/account. Don’t forget to add Facebook API and Secret Key as well as Twitter API Key And Secret Key. Keep them handy to use in your application.

Step 2: To use Face Recogonition API in Ruby we have to install the gem called face.
sudo gem install face

Step 3: Now using api_key and api_secret, you can access get_client method of API.
>> client = Face.get_client(:api_key => ‘your_api_key’, :api_secret => ‘your_api_secret’)

Face Detection using Face gem in Ruby

Face gem provides three different ways to detect faces in an image.

  1. Detect Faces with Urls
  2. Detect Faces with Raw image data
  3. Detect and Recognize faces with user auth

Lets understand with examples.

Detect Faces with Urls:

Suppose we want to detect face in the image located at this url. You will have to use faces_detect method as given below.

>>client.faces_detect(:urls => ['http://babybathreviews.com/wp-content/uploads/2011/01/How-to-choose-a-baby-bath.png'])

It will return photo detection output in the following way. Notice that you here you get face related information in a JSON format.

Detect Faces with Raw image data:

To detect faces from Raw image data, you will have to use following.

Detect and Recognize faces with user auth:

>> client.twitter_credentials = { :twitter_username => 'twitter_screen_name', :twitter_password => 'twitter_password' }
>> client.faces_recognize(:urls => ['http://test.com/1.jpg'], :uids => ['uiicor']) # will make request with twitter credentials

Hereby we are trying to recognize face with twitter credentials.

First we will have to initialize twitter credentials like this:

Step 2: To send the face recoginze request , write in the following way.

Note that uid is the id of the user on twitter.


The face gem provides facilty to detect faces from multi or single face images. Face gem is still under development. You can test it on face.com site. To get a feel for what is possible with the API you can browse some of the online examples, complete with source code, or play in the API sandbox. The face.com API has some decent documentation for those looking to dig deeper into the service, although the forums are fairly quiet at the moment.

Rails: Cron Job Scheduling using Redis, Resque and Rufus

What is scheduling?

Job SchedulerIn simple terms we can define scheduling as “the process of deciding how to commit resources between a variety of possible tasks“. The scheduling is basically a function which enables us to perform routine tasks at some predefined time or as part of a sequence. These tasks are normally executed in the background by so called “workers”.

Now the question is what’s the best way to run scheduled tasks in a Rails environment? Generally, Rails developers can use an application specific crontab to run application tasks. “Cron” is a time-based job scheduler in Unix-like operating systems (Linux, FreeBSD, Mac OS etc…). And these jobs or tasks are referred to as “Cron Jobs”. Downsides of Cron are:

  • Cron works well on a single server but what if you need to scale to multiple app servers? You’ll need to introduce some kind of lock to avoid concurrency problems. Also, you need to maintain these shared locks.
  • The other problem with Cron is that they are difficult to debug.
  • Cron is for scheduling things, not doing them.

This can almost always be better to place jobs in queue and place the worker system to perform or to execute those jobs. Luckily there is a very good gem named ‘resque‘ available in Ruby on Rails.

Introduction to Resque

Resque is a Redis-backed Ruby library for creating background jobs, placing those jobs on multiple queues and processing them later. The main benefit withResque is that it allows you to create jobs and place them on a queue, then, later, pull those jobs off the queue and process them.

For the backstory, philosophy, and history of Resque’s beginnings, please see the blog post. Besides that recently I came to know about other two plugins which are available in Ruby on Rails for job scheduling. One of them is resque-scheduler and the other is rufus-scheduler. Lets have a look at both both in detail.

Resque Scheduler – A light-weight job scheduling system

Resque Scheduler is an extension to Resque that provides both scheduled and delayed jobs. This service can replace cron and bring the ability to schedule task to execute at certain times. Resque Scheduler support job scheduling in two different ways: Recurring or Scheduled (a standard cron job) and Delayed.

Recurring or Scheduled job

A recurring/scheduled job is a task that is specified to be run at a certain time. For example you may want to update any indexes at midnight everyday; this is a scheduled job that runs based on a fixed schedule which is set at startup. The schedule is just a hash, but is most likely stored in a YAML.
Delayed job
A delayed job is a job that gets put on the queue with a specified time in the future to run at. For example you may want to schedule a followup email to go out a few days after a user has signed up.

How To use Resque-scheduler?

First of all Install Resque Scheduler. Create sample application or you can get the sample application from the github.

$ rails new rescue_jobs -d mysql
$ cd rescue_jobs/

Next step is to modify Gemfile to include

gem 'rails', '3.0.3'
gem 'mysql2'
gem 'resque'
gem 'resque-scheduler'

Run $ bundle install. Now its time to Configure Resque Scheduler. In our sample we use a Redis service, of course you can use your own Redis Server as well. Go to Redis To Go and sign up for the free plan. Once you have an instance, grab the URL given to you and modify the config/initializers/resque.rb as follows:

ENV["REDISTOGO_URL"] ||= "redis://username:password@host:1234/"
uri = URI.parse(ENV["REDISTOGO_URL"])

Resque.redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
require 'resque_scheduler'

Create a job named eat in app/jobs/eat.rb

module Eat
  @queue = :food
  def self.perform(food)
    puts "Ate #{food}!"

Inside config/initializers/resque.rb place the following code so that app/jobs/eat.rb is loaded.

Dir["#{Rails.root}/app/jobs/*.rb"].each { |file| require file }

Create controller file and add code

class EatController 
  "Put #{params[:food]} in fridge to eat later."

Add following line in routes.rb file

match 'eat/:food' => 'eat#food'

You also need to add file tasks/resque.rake

$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
require 'resque_scheduler/tasks'

Finally create the crontab for Resque Scheduler. Create file config/resque_schedule.yml and insert the following text:

  cron: 0 8 * * *
  class: Eat
  args: scrambled egg
  description: Breakfast
  cron: 0 12 * * *
  class: Eat
  args: penut-butter and jelly sandwich
  description: Lunch
  cron: 0 6 * * *
  class: Eat
  args: Steak
  description: Dinner
  cron: 0 0 * * *
  class: Eat
  args: Milk & Cookies
  description: Midnight Snack

To load the schedule when we initialize Resque add the following line to config/initializers/resque.rb

Resque.schedule = YAML.load_file("#{Rails.root}/config/resque_schedule.yml")

At the end Start the Scheduler Process

$ rake resque:scheduler

Rufus Scheduler – scheduler for Ruby (at, in, cron and every jobs)

Rufus-Scheduler is the latest version of a scheduler previously known as openwferu-scheduler. It is a Ruby gem for scheduling pieces of code (jobs). Rufus-Scheduler understands running a job AT a certain time, IN a certain time, EVERY x time or simply via a CRON statement. But we must note that rufus-scheduler is no replacement for cron since it runs inside of Ruby. The scheduler doesn’t do any server-side tasks to get jobs to run, but relies on its scheduler being run up and maintained persistently (or semi-persistently, as with Rails app processes that will tip the scheduler into action).

It doesn’t require any database table, queueing mechanism, or separate process to manage. Just a simple scheduler to call out to your existing ruby code. No need to register at anything. You have to include only one file in config/initializers and add the task you want to schedule. Whenever you start your server that tasks are done or performed at specific time. So ultimately it saves the time of queuing and you get the work done.

How To use Rufus-Scheduler?

First of all create a sample application.
rails new sample_app

Then add rufus_scheduler in Gemfile
gem ‘rufus-scheduler’

Install the gem with bundle
$ bundle install.

Add following lines in config/initializers/task_scheduler.rb

require ârubygemsâ
require ârufus/schedulerâ

## to start scheduler
scheduler = Rufus::Scheduler.start_new

## It will print message every i minute
scheduler.every("1m") do
  puts("HELLO #{Time.now}")

## Prints the message every day at noon
scheduler.cron("0,2 * * * *") do
  puts("HI #{Time.now}")

scheduler.in '20m' do
  puts "order butter"

scheduler.at 'Thu Mar 26 07:31:43 +0900 2009' do
  puts 'order pizza'

scheduler.cron '0 22 * * 1-5' do
  # every day of the week at 22:00 (10pm)
  puts 'activate security system'

scheduler.every '5m' do
  puts 'check status report'

You can also test above code on console.


Rescue Scheduler offers a simple and easy way to add scheduling abilities to any queueing system and is well worth the move from cron. But if you do’t want queuing and want only some scheduled jobs than rufus-scheduler is the best option.

Rails 3.1 release, rumors and news about features

What is the current status of  Ruby on Rails 3.1?

We all know that there isn’t a fixed roadmap/release date for Ruby on Rails 3.1, but there are definitely rumors about the new features and improvements. Yehuda Katz, a member of the Ruby on Rails core team, said “With 3.1 we’ll go back to basics and look at things that are less invasive internally, but could have a big impact on developers. If you want to check latest updates then click here and check milestones for Rails 3.1. It’s really a good news that just 26 tickets are remaining out of 68 total tickets.

Lets talk about new features which will be integrated with RoR 3.1.

Object Scopes in Rails 3.1

The change that is made to object scope is :

The above code describes that with Rails 3.1 it is possible to pass objects to scope methods that responds to the method ‘call’. Let’s understand what it means. Currently, with Rails 3 or Rails 3.0.x, scope works in the following way. For example, if we want to get all the products with name “Nokia” and with category “Mobile”, we used to create its scope method as given below.

Now to get required data, we use it like

But what if we have multiple classes that share the same logic for a named scope?? Suppose we want to get all the users with name = “John” and category = “Manager”. Think for a moment. In such cases it is obvious that we should be able to reuse same filter/scope code. Luckily, RoR 3.1 will provide exactly that kind of reusability. It will be possible to create classes that acts as scopes/filters.

In Rails 3.1, scope will be reused in other modules like this:

Its simply great !! Changing a simple is_a? to a respond_to? can unlock quite a power.

Automatic Flushing

The Rails 3.1 will have new feature named “Auto Flushing” which helps to boost the performance of Ruby on Rails application. Normally whenever we are downloading any page, there is a two step process: first, all the code gets compiled to generate html view and second, all necessary files like css, javascripts, images are being loaded one by one.

As you’ve probably seen, DHH announced that they try to add flushing in Rails 3.1 to improve the client-side performance of a typical Rails applications. The most obvious solution, and one that already exists in plugin form, is to allow a layout to have a new flush method, which would immediately flush the contents of the layout to the browser. By putting the flush method below the JavaScript and CSS includes, the browser could begin downloading and evaluating those static assets while the server continues building the page. For more information regarding this feature, you can check here .

Rails 3.1 > Integration of Spriting

One thing DHH mentioned in his keynote was that Rails 3.1 should improve the way Rails handles JavaScript, Style Sheets, icons and images. Rails 3.1 will support a technique called Sprite Generation which is basically a method for combining many graphics into a single image. That single image will be then displayed using CSS.

<% sprite_css(“icons”) %> this code does two things. First is to combine all the images within a single folder and second is to generating its CSS. The advantage of using such technique is to reduce dozens of HTTP requests into one cache-friendly image file.

Keep js/css close to the code in view folder

Guess what !! With Rails on Rails 3.1, we will be able to write the code for stylesheets and javascripts in the app/ * folder like this :

#This code will be compiled to the files like this:

The advantage of using the above feature is that after compilation of these files, your Ruby on Rails application will run on single CSS and javascript file which is neatly compressed without any kind of extra efforts.

What is going to be deprecated in Rails 3.1??

Passing options hash containing :conditions, :include, :joins, :limit, : offset, : order, :select, :readonly, :group, :having, :from, :lock etc inshort, finder methods which are provided by any ActiveRecord class will now be deprecated in Rails 3.1.

We used to write finder methods in this way

In RoR 3.1 it will be :

For more information you can check here.

From the above news about Rails 3.1 features, we can feel that Rails 3.1 will be another great release. Any guesses when we will see 3.1?

Quicktip: Finding deprecated constants/methods easily

In Rails 3 a couple of constants/methods were set deprecated. However, there are still plugins which use them. This ends normally in the annoying log message:

DEPRECATION WARNING: RAILS_ROOT is deprecated. Please use ::Rails.root.to_s. (called from <top (required)> at /Users/heli/hemju/programming/workspace/rubymine/linguist/config/application.rb:12)

So what to do when you want to get rid of them? Find them, (optional) create a patch, and report them to the plugin maintainer. One easy way to find deprecated elements is to override them and let them throw an exception or set them to nil. So in the above case, I would add RAILS_ROOT = nil to an initializer. At the next startup there will probably be an exception thrown and you know exactly where to find the deprecated method. Now start patching.

Helmut Michael Juskewycz