Sunday, December 30, 2012

Creating a Spree application

Spree is an open source e-commerce platform that is available as a ruby gem for Ruby on Rails framework. It is intended as a foundation over which developers can build their own e-commerce sites.


To use Spree you will need to install following:
  1. ruby
  2. imagemagick
  3. nodejs
  4. libxml2
  5. libxml2-dev
  6. libssl-dev
  7. libxslt-dev
  8. postgresql
  9. postgresql-server-dev-9.1
If you are using Ubuntu, you can easily install all of these from repos using aptitude. Imagemagick is required for resizing uploaded images. PostgreSql is selected because it is common in the hosting platforms but if you are not concerned about it, you can simply replace it with sqlite or anything else that you like.


After installing the above listed softwares, install bundler, rails, rake and spree gem using following command:
gem install bundler rake rails spree
Now create a new rails application. The default database is Sqlite, so we need to specify that we want postgres:
rails new myspreeapp -d postgresql
Edit config/database.yml and add host: localhost to all configurations and also correct database name, username and password. Next, add spree to your app. To do this run following command from withing your applications root directory:
spree install
This will run a wizard which will ask your choices for running migrations, loading seed data and others and perform the required actions. When the wizard ends your spree application is ready and you can now run it on local host using:
rails s
Your basic spree applications is now available at You will most probably want to customize this application according to your needs. Head over to spree customization guides for a detailed look at customizing spree.

Friday, December 7, 2012

Git: Beware of detached head

In git, every commit is recognized by a 40-character hash known as commit hash. A git repository is essentially a tree of commits, where each commit points to its ancestor(s). In git, a branch head is the last commit in the branch. A Git branch is simply a pointer to this commit, using its commit hash, in the repository. These pointers are kept in .git/refs/heads directory as files. If you look at one of these files you will find that each branch corresponds to a single file which contains nothing but a 40-character commit hash of the commit representing branch's head. So when you have a master branch, there is a file .git/refs/heads/master pointing to head commit of this branch.

There is a difference between a branch head and HEAD. As described earlier, a branch head is the last commit in the branch, whereas HEAD represents your currently checked-out commit. It is also maintained as a pointer in .git/HEAD file. Normally this pointer will point to the head of currently checked-out branch, thus the name HEAD, but it is quite possible for this pointer to point to a commit which is not the branch head. So , for example, if you are working on branch master and you have checked out the head of this branch, the contents of the .git/HEAD file will look like following:
adnan@adnan-laptop: test/> cat .git/HEAD
ref: refs/heads/master
So, in this case, the HEAD points to head of branch master, which then points to a commit. This means that you have currently checked-out head of the branch master. Now if you move one step backward and checkout a commit just before the head commit, you will get a detached HEAD.
adnan@adnan-laptop: test/> git checkout HEAD^
Note: checking out 'HEAD^'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
  git checkout -b new_branch_name
HEAD is now at 123fa7b1... my 1st commit comment
A detached HEAD is a situation where the HEAD pointer is no longer pointing to a branch head. In this case the file .git/HEAD doesn't contain a reference to a branch head but instead directly points to the checked-out commit using its commit hash.

If you now check the contents of .git/HEAD file, you will see it contains the hash of the commit you just checked-out.
adnan@adnan-laptop: test/> cat .git/HEAD 
It is totally okay to work with detached heads but it is very important to understand the situation as any commits made in this situation will not be part of any branch, as you are not at the branch head, and the only way to refer to it later on will be using its commit hash. Also git garbage collection will run occasionally and remove any commits which are not referenced, directly or indirectly, by a branch or a tag. 

Tuesday, November 20, 2012

Its a free world

Disclaimer: I am not a web developer. Although I am a CS grad and understand all the concepts involved and have worked on a few websites in my unversity days and little afterwards, but I am not employed as a webdeveloper and am out of touch with this subject and the technologies and services involved.

Recently, while working on an eCommerce website for a friend, I came across some amazing free services which can really help a start-up reduce its costs. 
The first and most important one is Heroku. Heroku is a no-hassle hosting solution for Ruby, Node.js, Clojure, Java, Python, and Scala. It helps the developers to focus on the most important thing i.e creating the most amazing web app ever and forget about servers. Your application can be up and running in minutes. All you need to do is create an app on their servers and then push your code to it using git. They provide a handy utility to create and manage applications from command line and it can be installed from Once you have installed this, all you need to do to make your application live is to run from command line:
heroku apps:create my_amazing_app
git push heroku master
and twalah, your site is up and running at You can read more about Heroku here

One thing that Heroku lacks is file storage. File system for your app (slug in Heroku's terms) is readonly. So you cant create and write to files on the server. This means that uploading files e.g images wont work. So you will need some cloud storage to store your assets and this is where Amazon Web Services comes in. Amazon S3 is free for you for 1 year. Upon sign-up, new Amazon Web Services customers receive free 5 GB of Amazon S3 standard storage, 20,000 Get Requests, 2,000 Put Requests, and 15GB of data transfer out each month for one year. So you can host your images or other assets on Amazon's servers and stop worrying. More about AWS Free Tier here.

Once your app is ready and running as you want at, the next thing you need is a domain name. Now, there is no way around it. You will need to buy one but you will also need DNS service so that you can point your new domain to your app on Heroku. This is where comes in. They offer a starter package which is free and gives you 50,000 DNS queries each month, which is more than what I need for now. Its also realy easy to setup Heroku to use Zerigo. All you need is to use Zerigo DNS add on and configure it to redirect to This is easily done using the following commands:
heroku addons:add zerigo_dns:basic
heroku domains:add
More on Zerigo add-on here.

The last thing that I needed, and which you may or maynot need, depending upon the web app, is a mail service. For an ecommerce website you need to be able to send emails to your customers. The free mailing functionality is provided by They also offer a free/starter package which allows you to send 200 emails per day for free. Similar to Zerigo addon, Heroku provides an addon to enable this functionality. All you need to set it up is the following command:
heroku addons:add sendgrid:starter
Afther that you need to configure your mailer software to use and smtp port 587 to send emails. You will also need the login/password for which you can get from Heroku using command:
heroku config
And thats it. Your app is up and running without paying a dime.
Isnt it amazing? I think it definitely is. 

Thursday, July 19, 2012

Thank you MIT and Prof. Alan V. Oppenheim

Recently I needed a refresher in Signals and Systems and luckily I found lecture videos for Signals and Systems by Prof Alan V. Oppenheim from MIT OCW website. Although I have studied Signals and Systems TWICE at the university, I have never understood the concepts the way I do now. These lectures have given me the ability to analytically understand the concepts and have enabled me to create a visual of these concepts in my mind which is amazing.

The lecture videos are old i.e from 1987 but the concepts have not changed and these lectures are as relevant today as they were back in 1987.

Once again THANKYOU MIT and Prof Oppenheim. Thanks a lot for this.

Wednesday, May 16, 2012

How to unblock the Internet (anonymous browsing)

I am tired of organizations trying to stop their employees from accessing sites like Facebook, MySpace, linkedin and even blogspot and lots of other websites that contain useful work related information. If someone is going to waste time while on job, they will find other ways to do so. Blocking these websites wont change a thing.
If you happen to work in such an organization or if you just want to remain anonymous while browsing online, Tor is what you are looking for. From their home page
"Tor is free software and an open network that helps you defend against a form of network surveillance that threatens personal freedom and privacy, confidential business activities and relationships, and organization state security known as traffic analysis"
Tor accomplishes this by routing your traffic through intermediate nodes(called relays) so someone watching your internet connection wont know which sites you are visiting and the sites you visit wont know your physical location.
Its realy easy to setup Tor on your machine. Below are the steps to use Tor with Chrome on windows machine.
  1. Download Tor from their download page and install it. You will need the Vidalia Bundle
  1. Run Vidalia. This will bring up the Vidalia Control Panel. let it connect to the relay network. When its done minimize it.
  1.  Now open Chrome and install Proxy Switch from Chrome Webstore. This is a proxy manager and it will enable you to switch to using Tor by a single click.
  2. Open options for Proxy Switchy and add a proxy profile. Name it Tor for easy reference and point it to

and thats it. Now when you want to switch to anonymous browsing, just click on the Proxy Switchy icon on the address bar and select the Tor profile and you will be anonymous.

Friday, April 6, 2012

My Kindle Fire with Cyanogenmod 7

I just installed Cyanogenmod 7 on my Kindle Fire. It took some time to get things right but it looks awesome now. I was really getting tired of Amazon not allowing me to install even free apps. Waiting for Cyanogenmod 9 AKA Ice Cream Sandwich for Kindle Fire :D

Tuesday, February 21, 2012

C Language: Struct packing and member alignment.

I know this has been discussed and re-discussed at so many locations and so many times that me writing about it again wont make much of a difference but I still see so many people confused about this that I cant stop my self from writing about it.

By default C compilers properly align each member of struct. This means that a 2-byte member e.g. short is aligned on 2-byte boundary, a 4-byte member e.g. int is aligned on 4-byte boundary and so on. This is done so that the struct members can be accessed efficiently and to reduce cache misses. To ensure proper alignment compilers add padding bytes to structs. Consider for example the following struct:
struct SomeData
    char Data1;
    short Data2;
    int Data3;
    char Data4;

Now as you can can see we are only using 8 bytes for data, but the compiler will add padding bytes to this struct to ensure proper alignment of its member. So a variable of this struct type will actually be like this after compilation on a 32-Bit machine:
struct SomeData
    /* 1 byte */
    char Data1; 
    /* 1 byte so the following 'short' can be aligned on a 2 byte boundary*/
    char Padding1[1]; 
    /* 2 bytes */
    short Data2;
    /* 4 bytes - largest struct member */
    int Data3;
    /* 1 byte */
    char Data4;
    /* 3 bytes to make total size of the struct 12 bytes */
    char Padding2[3];

The compiled size of the structure is now 12 bytes. It is important to note that the last member is padded with the number of bytes required to make total size of the struct a multiple of the size of the largest member of a struct. In this case 3 bytes are added to the last member to pad the struct to the size of a 12 bytes (4 bytes of int × 3).

As you can see, we are wasting memory. We can off course stop compiler from doing this by asking compiler to pack structs tightly (using #pragma pack() with gcc) but then we lose performance benefits of proper alignment.

In order to avoid these padding bytes but still have proper alignment, we can rearrange this struct so that larger members are listed before the smaller ones. So the above struct can be rearranged as:
struct SomeData
    int Data3;
    short Data2;
    char Data1;
    char Data4;
Now this does not require any padding as every element is already properly aligned and the overall size of the struct is a multiple of the largest member i.e 4*2 = 8. This is off-course just an example and in reality, even if you arrange your struct members this way, there will still be some padding at the end of the struct to make total size of the struct a multiple of the size of the largest struct member
So please always arrange members of your structs in decreasing order of their size.

Thursday, February 16, 2012

Android: How to start vendor specific Alarm Clock on any Android device?

Alarm clock is a nice to have feature for many applications and because of Intents you dont need to code this functionality in every app where you want it. For Android, adding an Alarm Clock to your application is as simple as launching an Intent for the built-in Alarm Clock of the device. The only issue here is that different manufacturers i.e HTC, Google, Samsung, Sony Ericson have their own implementation of Alarm Clock and there is no single intent that will automatically launch which ever Alarm Clock is present on the device. So to start Alarm Clock, you will first need to check which Alarm Clock is present on the device that the app is running on and then launch that Alarm clock.

The code below is what I used in one of my recent apps:
private void startAlarmActivity()
 PackageManager packageManager = getPackageManager();
 Intent alarmClockIntent = new Intent(Intent.ACTION_MAIN)

 // Verify clock implementation
 String clockImpls[][] = {
    { "HTC", "",
    "" },
    { "Standard", "", 
    "" },
    { "Froyo", "", 
    "" },
    { "Motorola", "com.motorola.blur.alarmclock",
    "com.motorola.blur.alarmclock.AlarmClock" },
    { "Sony Ericsson", "com.sonyericsson.alarm", "com.sonyericsson.alarm.Alarm" },
    { "Samsung", "", 
    "" } };

 boolean foundClockImpl = false;

 for (int i = 0; i < clockImpls.length; i++)
  String packageName = clockImpls[i][1];
  String className = clockImpls[i][2];
   ComponentName cn = new ComponentName(packageName, className);
   packageManager.getActivityInfo(cn, PackageManager.GET_META_DATA);
   foundClockImpl = true;
  catch (NameNotFoundException e)
   Log.d("ADNAN", "Alarm clock "+clockImpls[i][0]+" not found");
 if (foundClockImpl)

Thursday, February 9, 2012

Android: How to transform/scale a bitmap and draw it on Canvas without losing quality

If you want to rotate a bitmap and paint it on a Canvas in an Android app, you will probably use Canvas.rotate(degrees) to rotate the canvas and then paint your bitmap using any of Canvas.drawBitmap methods. Nothing fancy but the painted image will be too pixelated - except when the rotation is in multiple of 90 degrees.

The last argument to all Canvas.drawBitmap* methods is a Paint object and most of the examples I found on internet were passing a null for it. To avoid, rather reduce, the pixelation we need to use this Paint object. Create a paint object as mentiond below and pass it as a last argument to your drawBitmap() call.
Paint paint= new Paint(Paint.FILTER_BITMAP_FLAG |
                       Paint.DITHER_FLAG |
This will significantly reduce pixelation and your app will look a lot better.