Quiz Stack is a SAAS based product. Which can conduct,manage,analysis quizzes.We are using rails and postgresql as our backend.It’s subdomain app and hence we decide to use postgresql schema based approach for storing/querying data.

While working on one of the feature It was needed to draw some random fixed questions for each user.So each user has different order of questions which is maintain for each specific user.

In Mysql it’s called field function to achieve this ,unfortunately there isn’t anything as of I know in PostgresSql or is it ?

Well we can obviously write a function which can help you do the same.Let’s the function name be sortbyid.

Simple Solution for creating Permutations

While working on a research project, i came across a simple task of creating sets of permutation for a given objects. What i thought simple task become tedious after goggling it. I didn’t find any simple solutions for creating a permutations. So i create his one for those who wants simple solution for creating permutations.

How to solve complicated problems of creating permutation.

Creating permutation of object such as set of colors, basically the task is to find combination of colors (i.e. color addition problem)

It’s 5 in the morning and i am tired and its a possibility that i may be writing a long post, Last night i asked my self what is it that i have learned in the las t couple of months and out of nothingness i rememberd a VIDEO CHAT APPLICATION which my friend Viren and I worked on. I thought it might be useful for some of you.

When we started, we didnt have enough knowledge where to begin with and had time constraints.We came across solutions like Red5,ErlyVideo.

Now out problem was that out client had to be in flash or java applet, we we tried lots of things and the mother solutions of all problems was yet released, HTML5.

When we started, we didnt have enough knowledge where to begin with and had time constraints to meet. We came across solutions like Red5,ErlyVideo. Now our problem was that our client had to be in flash or java applet. So we tried working around many solutions. HTML5 was months away from its release.

we are looking into other HTML5 Features and we get awesome HTML5 web socket support so we thought let’s get some hands on it.

So we think let’s build pubsub chat application, after Googling we got couple of solution.but we want to build own i have choice between nodejs and EventMachine.we have choose EM to quick start.

So we are ready,
We go through HTML5 Web Socket API,and tutorial so basically it’s a technique for two-way communication over one (TCP) socket, a type of PUSH technology. So it has mainly three events

onopen: When a socket has opened

onmessage: When a message has been received

onclose: When a socket has been closed

More info regarding HTML5 Web Socket you can visit Nettuts, HTML5Demo. lya Grigorik has written awesome library,and article on it so i don’t go in much detail.

Steps we are following

When User is connectedonopen event is fired and we create one default connection and add to pool of channels,it’s used for mainly notification purpose

Many of us knows what daemons are in linux. These are processes or programs that run in background with little or no user intervention.

Now we can write daemons in ruby too. And for doing so we can use Daemon-Kit(https://github.com/kennethkalmer/daemon-kit).

Now the daemon can do the task continuously or do it at particular interval of time or do it depending on a particular event.

Daemon-Kit provides a skeleton to write these different types of daemons with the help of differnt types of generators.(XMPP bot,AMQP client,Nanite agent,Cron-style,ruote remote participants). Of these XMPP and AMQP are event based and amqp is specifically based on queuing system, and Nanite agent gives a nanite structured daemon while Cron-style is used to write daemons that perform certain task at particular interval amount of time.

First install the gem daemon-kit

gem install daemon-kit

For viewing the help of daemonkit you can give the command,

daemon-kit -h

Now to build a simple daemon which would use a default generator you can use,

The pre-daemonize folder consists of all the files that are required before the daemon starts. The cron.rb can be used to require all the gems that are needed for the daemon.

The my_cron_daemon.rb in lib folder is automatically loaded when the daemon starts. Additonal ruby files can be created in the lib folder and can be required from the cron.rb in pre-daemonize for utilization.This can be done as,

This is done because my_cron_daemon.rb in lib is automatically loaded and requiring it multiple times might cause issues.

The post-daemonize folder is used to place those files that needs to be loaded after the daemon starts.

Similarly a yml file(e.g config.yml) which holds all the configuration setting can be created in the config and loaded into the daemon using,

CONFIG = DaemonKit::Config.load("config.yml")

Here CONFIG can be used anywhere in the daemon later on to access the required configuration.

Now you can also add ruby files that connect to a particular database and query data by putting those in lib. These files would have a structure similar to the ruby files in models of rails application.

As for the database connection you can put these pieces of code in cron.rb in pre-daemonize which establishes the databse connection,

Now we can also have the daemon as a request-response serving program similar to a rack-server, This can be done by using eventmachine.

Daemons are a difficult to debug. Because they can just cause your program to fail when a request comes in. Normally we write a code in a normal Ruby program and run it to test all the functionallity and then put it within the daemon. This helps in ensuring that your logic is proper. For further debugging I use a lot of puts statements when coding later switching to DaemonKit.logger.info or .debug to dump the statements to the log.

DaemonKit.logger.info "I am the DaemonKit logger info"

Here is a link to a basic daemon which is based on default,cron generator.

https://github.com/kelvink/My-Basic-Daemon

https://github.com/kelvink/My-Cron-Daemon

Also here is a link for the daemon to work as a rack server using evenmachine.

https://github.com/kelvink/My-EM-Daemon

Thus we can have a ruby daemon which perform specific task as required and when required.

Hey all,

Many of us knows what daemons are in linux. These are processes or programs that run in background with little or no user intervention.

Now we can write daemons in ruby too. And for doing so we can use Daemon-Kit.

Now the daemon can do the task continuously or do it at particular interval of time or do it depending on a particular event.

Daemon-Kit provides a skeleton to write these different types of daemons with the help of differnt types of generators.(XMPP bot,AMQP client,Nanite agent,Cron-style,ruote remote participants). Of these XMPP and AMQP are event based and amqp is specifically based on queuing system, and Nanite agent gives a nanite structured daemon while Cron-style is used to write daemons that perform certain task at particular interval amount of time.

Can’t get time because we are working almost more than 10 to 12 hr day with messy code.

So we are spending more time to refactoring and optimising code,and scaling application also.

Now i started making my time to writing.

In my current organization we are generating various Pdf report.

We are using prawn but now we are switching to acts_as_flying_saucer.

So there are couple of challenges i have faced

1) XHTML code is not proper

2) Special character in html

Solution

Cleanup Html code

I am using tidy library clean up html code.

By passing :clean=>true it will cleanup html befor generating pdf.

3) Send PDF as email

Solution

Generate PDF on server and do some processing like attached to mail.

:send_attachment => true it will generate pdf and return path of pdf file then do some processing.

So couple of new feature is added to acts_as_flying_saucer

1) Clean up html by passing :clean=>true

2) Generate pdf on server (locally) by passing :send_attachment=>true

3) acts_as_flying_saucer is also working with rails 3.1 (precomplie asset) and heroku.

It’s about more than 6 months i haven’t written any post Why ?

because recently join new organization, and can’t get much more time , we are working almost more than 10 to 12 hr day with messy code.So we are spending more time to refactoring and optimizing code,and scaling application.😥

In my current organization we are generating various PDF report.We are using prawn but now we are switching to acts_as_flying_saucer.

Well long break,Let me guess It over 3 month since I wrote my last post. I’ve been kind of busy lately not manage to dedicate much time to thing that I do.

Rails 3.1.0 is been in picture for a quite a while a now and major feature that been included in Rails 3.1.0 is asset pipeline and pre-processor.

For those is who aren’t aware of Rails 3.1.0 asset pipeline and pre-processor there is a very good article on rails guide and recently Ryan Bates introduce a screencast on Rails 3.1.0 asset pipeline.

I’m not here to talk about asset pipeline you can get it all if you just googled it.

So what I’m here upto then ?

Well ever since I was made aware of what asset pipeline and pre-processor more importantly pre-processor ,it always trilled me of using a custom pre-processor .

As you all know that Rails 3.1.0.rc1 is bundle with coffee-script and sass support. In your asset folder you may find js and css file that look something like this “*.js.coffee” , “*.css.scss“. Now you can add a chain of pre-processor that just look like this “*.js.coffee.erb” , “*.css.scss.erb”

The post is all about chaining your own pre-processor just as above something like “*.js.coffee.mustache”

Mustache (Mustache is taken just for example here you can use your own) because I been in love with it ever since I explored it . So how to accomplish this .