Tuesday, May 31, 2011

Disable all contents of the page using jQuery

Here is the code to disable all contents of the page. By "disable" i mean that everything on the page will neither be clickable nor any text will be selectable nor right click will work. Here is a simple solution to solve the problem using jQuery

var newDiv = $("<div></div>")
    $(newDiv).css({width: '100%',
                   height: $(document).height()+'px',
                   position: 'absolute',
                   top: '0px',
                   left: '0px',
                   backgroundColor: '#000000',
                   opacity:'0.5'});
    $("body").first().append(newDiv);
    $(document).bind("contextmenu",function(e){
            return false;
    });

}

You can also make a css class for the css attributes mentioned above, the only check is for the "height" , because setting height = 100% won't work. Since, if the page has a scroll bar, then setting height = 100% will only disable the part of the page which is currently in the context and if you scroll down, the rest of the page won't be disabled. If you simply set opacity to 0.0, this will show the page as a normal page but everything on it will be disabled

Thursday, April 28, 2011

Monitor and restart services in linux

Working on a project lately, I wanted to achieve a simple goal that if some particular service is not running, then start it automatically. Normally, such things are done through shell/perl scripts which are executed as cron job. There are some other solutions for this thing as well i.e. www.pingdom.com though this is a paid service. Looking further, I found a very nice open source service for this thing called MONIT.. Following link explains all the steps for installation and some usage examples as well

http://www.cyberciti.biz/tips/howto-monitor-and-restart-linux-unix-service.html

Tuesday, March 22, 2011

Installing Ruby 1.9.2 and Rails 3 stable on Ubuntu

There are a zillion threads on internet regarding fresh installation of ruby 1.9.2 and rails 3 on ubuntu, however, most of them have some sort of dependency missing. Lately, i found a very helpful link on this topic, so i thought of sharing the post

http://torqueo.net/installing-ruby-192-and-rails-3-stable-on-ubuntu/

Wednesday, March 16, 2011

Fetch emails from Gmail using POP3

There are a couple of protocols for fetching emails from email services such as Gmail.. POP and IMAP. In this post, i'll be telling a simple way of fetching emails from Gmail using POP3. The code is in Rails (will try to post a php version later if get time). The method is extremely simple and trivial. But before we jump to the code, the first thing you need to do is to enable POP access from your Gmail account. You can found the POP settings under "Settings" tab. Look at the below snapshot


Make sure that you save the changes. Now, we are done with the settings, let's see the code now.

for this code you need to have "net/pop".

require 'net/pop'

pop_server = 'pop.gmail.com'
pop_port = 995
username = "example@gmail.com"
password = "example_password"

Net::POP3.start(pop_server, pop_port, username, password) do |pop|
       if pop.mails.empty?
             puts "No Mail Found"
       else
             pop.mails.each do |email|
                  MailReader.receive(email.pop)
             end
       end
end

now, the only piece missing from the above code is "MailRead.receive". Basically, we need to have a email handler which has to be extended from ActionMailer. We just need to pass the email details to this handler and it will do the parsing for us. So, now let's see the code for this:

class MailReader < ActionMailer::Base
    def receive(email)
        puts "Subject ==>" + email.subject
        puts "Body ==> " + email.body
    end
end

That's all about it :) .. if you want to fetch and save attachments as well, just get the paperclip. You can download it from https://github.com/thoughtbot/paperclip (for direct installation with rails use the command rails plugin install git://github.com/thoughtbot/paperclip.git ).

Thursday, February 10, 2011

Thrift installation on ubuntu (for usage with ruby)

(This tutorial is tested on thrift 0.5.0)

I was working on thrift (http://thrift.apache.org/), found a couple of issues during the installation and had to look at a couple of links to figure out the correct method.. so i thought to sum it up here.
  1. Download thrift (http://thrift.apache.org/)
  2. Extract the downloaded archive and go to the folder through terminal
  3. In order to install thrift, you'd need following dependencies: (as mentioned on the thrift wiki page)
    1.  g++ 3.3.5+
    2. boost 1.33.1+ (1.34.0 for building all tests)
    3. Runtime libraries for lex and yacc might be needed for the compiler
    4. The dependencies for each language are different which you can see at the link => http://wiki.apache.org/thrift/ThriftRequirements
    5. GNU build tools: autoconf 2.59+ (2.60+ recommended), automake 1.9+, libtool
    6. For C, you'll also might need to install "libglib2.0-dev"
  4. In order to install above dependencies, you can use the following command
    sudo apt-get install g++ libboost-dev libevent-dev automake pkg-config libtool flex bison
  5. You might need to bootstrap first, for that run ./bootstrap.sh (assuming you are in the directory of extracted thrift)
  6. Now, run the following commands (in sequence) to configure and install thrift
    1. ./configure
    2. make
    3. sudo make install
  7. Now, you are good to go with thrift. The final piece missing is the thrift gem. You can install that using command => sudo gem install thrift

Hope you find the post helpful. I will try to post example of thrift using ruby soon :)

Wednesday, February 9, 2011

mySql is scalable and NoSql is not an answer to everything

I was kinda getting sick of hearing that "SQL is not scalable", "you can't have schema-free engine in SQL" .. so i thought of looking at what the fuss is all about.. i won't add anything myself here since i haven't done any benchmarking but from my experience of mySql, i can say one thing for sure that it IS scalable, you CAN have schema-free engine in mySql and if you use it rightly i.e. using techniques like sharding and memcache, you can achieve pretty high performance with the security and data-safety. Here are few links that i saw on the topic;

  • a nice article on why you shouldn't use NoSQL for everything http://bit.ly/cVUm4B
  • another nice article on how you can create a schema-free engine in mySql http://bit.ly/9zqBcK
  • this is an open source library for using key/value structure with mySql http://bit.ly/gYvBWF
  • and finally a big proof that mySql is scalable and if you use it rightly you can do wonders.. friendfeed uses mySql and here is a description of their mechanism http://bit.ly/U2A8M

Extraction of summary of a url (facebook post link feature)

I initially thought to share the ruby on rails code i wrote to achieve this particular functionality, but then i ain't a big fan of rails so here is a simple step by step description of how to achieve this (between this ain't a rocket science)

  1. first of all you need to have a dom parser (there are alot of parsers available in various languages.. choose the one you like)
  2. parse the url and create it's DOM
  3. now use XPATH (or whatever DOM parsing mechanism the library provides)
  4. if you have ever paid a bit of attention to the summary facebook generates against a link, you'll notice that it consists of 3 things ... i) title .. ii) description... iii) image. now let me describe each of them individually
    • Title: This is the most easiest of all.. you can simply extract the contents (title) from the <title> tag under <head> tag
    • Description: this is also a very easy thing to achieve.. you can extract the description from the contents attribute of <meta name="description" content="..."> tag.
    • Images: Now, here is a tricky part.. you need to extract all images available on the page and then choose the largest image (height and width wise) and use that as the thumbnail.. if you want to provide a "choose thumbnail" feature like fb, then simply extract all images which have height and width greater than 50px and show all those in a simple slideshow. One might think that this is time consuming, and i agree.. there is a small hack but that is not applicable on all links. Since, fb introduced "open graph", the websites who have moved to it use tags like "<meta property='og:image' ...>" to tell fb the representative image of the page. you can simply use the image given in this tag (if the tag exists) and save all the trouble of going through the complete DOM.
  5. After this all you need is to display the extracted stuff according to your style.
Will try to post PHP code for this if i get time otherwise might even post code of rails

Sunday, January 2, 2011

Java stored procedure in Oracle

Recently, i got a very interesting project from a client. The guy wanted me to write a script which is fired at specific instances and sends SMS to the users. The interesting part was that neither the source code of the front-end application was available nor the interface. Only one thing was given and that was the database (now it's funny for me to see a person thinking that it's not safe to give the source code of the front-end and yet give database.. but clients are this way sometimes) .. So, i thought that the only approach left is to use Oracle triggers but obviously, one can't send SMS using SQL (atleast not using any method that i know of). So, I thought of using Java stored procedure and calling the procedure from a trigger. Not many people know about this functionality of Oracle that it allows you to write a stored procedure purely in Java language and embed that in the Oracle DB.
For the time being, I am sharing a couple of very nice and easy to learn tutorials on this particular topic. I'll try to write a tutorial on this myself if i get time.

http://www.developer.com/db/article.php/3337411/Oracle-and-Java-Stored-Procedures.htm
http://download.oracle.com/docs/html/A95261_01/jdgtut1.htm