Scriptaculous - a Javascript library

March 14th, 2008 by divya

Scriptaculous is a Javascript library for providing Web 2.0 effects in web pages. Scriptaculous is built on top of the Prototype library(prototype.js).Effects provided by scriptaculous include Visual effects like Appear,Higlight, Shake etc of DOM elements. It also provides advanced functions like Slider, Drag & Drop and Autocomplete for text boxes.

A drop and drop shopping cart example can be see here

Scriptaculous can be downloaded from here

Download and link the scriptaculouos.js file to your html page. Using scriptaculous effects is very easy

Example:

Effect.Appear('element_id'); (element_id is the id of a DOM element)

Similar examples with demo can be seen here

Scriptaculous library by itself is simple, but Ruby on Rails makes it even simpler. Rails has wrappers around scriptacuolus methods.

Examples:
i :onclick => visual_effect(:highlight, 'msg', :duration => 1.0)

ii. <%= draggable_element 'idOne', :revert => true %>


JuggerNaut - Push Server for Rails

March 14th, 2008 by unni

When we build a web page that contains data which gets updated very frequently (eg: Share price of Google, or score of a live cricket match), it would be extremely useful if we could ‘push’ data into the webpage, without the user having to ‘pull’ the data by reloading the page or requesting explicitly.

Juggernaut is a Ruby on Rails plugin that helps to implement this ‘push’ mechanism. Comet is the widely accepted technology for doing the same. Juggernaut claims to have a few advantages over comet. Details can be found here.

Apart from the Rails application, juggernaut runs a separate push server, which maintains persistent socket connection with all connected clients. Ruby app sends data to this push server, which pushes the data to all connected clients.

Juggernaut initiates a flash xmlsocket between server and browser allowing real time communication between the two. The open socket connection allows the server to ‘push’ base64 encoded javascript to the browser which is subsequently decoded and evaluated.

juggernaut.jpg

Installation and configuration of Juggernaut are explained in detail in the Juggrenaut page.

I tried to build a simple Juggernaut-Rails application that would push the current playing song name in my system(rails app runs on the same machine). It was fairly simple, current playing song name was extracted using the Dbus interface of Rhythmbox music player (More details on this can be found in my article on IPC with Dbus. Once the songname was obtained, pushing it to the browsers required only the following code in the controller:

 render :juggernaut do |page|
      page.replace_html 'song_name', "<b> #{song_name} </b>"
 end

Ramaze Ruby is amazing !!!

March 1st, 2008 by divya

Ramaze-means Ruby Amaze , light and modular open-source web application framework written in Ruby.Which is more easy to work with and no complexities for a new user.Ramaze is claiming to have more advantages than ruby on rails in speed , safety , user friendliness etc.

Installation is very simple

$ gem install ramaze installing as rubygem

Now your ramaze is ready to use.You can build ur applications by saying

$ ramaze --create my_app

Which will give u the basic format and you can edit and build your application in the way you want.Ramaze follows MVC architecture , unlike Rails it can work on a single ruby program also.You can run your ramaze application like

$ramaze or
$ruby start.rb

Or if u want to run a single script just run
$ ruby yourscript.rb

Ramaze is well documented with basic examples and easy to use even for a newbie.Ramaze stays close to the ruby’s principles such as simplicity n elegance.Basically Ramaze contains a controller called MainController and which maps to ‘/’. Here data is getting saved in a ‘.yaml’ file instead of database.

class MainController < Ramaze::Controller
end

Also we can map to other URL s if we want.Its pretty simpler than Rails i guess.

class AnotherController < Ramaze::Controller
map '/another'
end

You will get the basic examples to run your application from here.

Ramaze supports a lot of templating engines and such as Haml, Erubis,Markaby an d Ezmar.Also includes support for several adapters, including Mongrel, Evented Mongrel and FastCGIetc.
In the homepage of Ramaze you can see the tutorials ,just give a try you will understand more.


TV remote control, Ruby and Turtle Graphics

February 22nd, 2008 by unni

In my previous post I had written about doing Turtle graphics with Ruby. Now i want to control the movement of the turtle on the screen, using a TV remote control, wirelessly from a distance. Let me breakdown the whole process into simple steps :

  1. Transmitting Infrared (IR) signals from a distance.
  2. Receiving these IR signals.
  3. Interface to the computer.
  4. Software Interface (API) for reading the data from the I/O port.
  5. Application level program for controlling the movement of Turtle based on this data.

Transmitting IR signals

An circuit comprising of IR LED, power supply and a few switches would have been required, but we can simply use a TV remote control, instead :)

Receiving IR signals

A simple IR receiver circuit and instructions for building it can be found here. Components costs less than Rs.20 and can be bought from any electronics shop.

Here is the one which i made :

ir_receiver.jpg

Interface to the computer

Our IR receiver circuit has a serial interface. So we can connect it directly to the serial port of the computer.

Software Interface for reading and interpreting signals on the serial port

The Linux Infrared Remote Control project (LIRC) provides all necessary support for us. LIRC is a package that allows us to decode infra-red signals of the most commonly used remote controls. Even if we have a remote control that is not directly supported by LIRC, we can train LIRC to use our remote comtrol.

Instructions for installing and configuring LIRC is available here.

Alas, the remote controller which I had with me - it was the one which came with my TV tuner card, and was faulty. So I couldn’t configure it properly with LIRC. I was able to receive signals from my remote, and see it using ‘xmode2′ program that comes with LIRC, but LIRC wasn’t able to configure the keys of my remote control. I dint have the time to go and get a new remote control, so i devised a temporary work around - Make the turtle move in any one direction by default at a predefined speed, and whenever a key pressed on the remote (irrespective of which key is pressed), turn the turtle by 45 degrees.

Program for controlling Turtle movements upon keypress on remote control

Even though LIRC couldn’t help me directly with my remote, i found a device file /dev/lirc0 (that appears once the circuit is connected, and ‘lircd’ daemon started). I tried reading from this file, and on each keypress on the remote i got a series of bytes. So i decided to use this file, for detecting keypress on the remote.

Now the only thing left was modifying my previous turtle program, to make the turtle change direction upon every keypress on the remote control. I used two threads :

  • first one for moving the turtle forward in a direction
  • second one for detecting keypress (by reading /dev/lirc0) and changing the direction whenever key is pressed

A shared global variable key_pressed is used for communication between these threads. Access to key_pressed is synchronized with a mutex.

Here is my complete program for controlling Turtle on screen with a TV remote.

turtle_remote.rb

Root previleges are required for accessing /dev/lirc0, so run it as

sudo ruby turtle_remote.rb

Turtles movement on screen can be seen here