Archive for the ‘Ruby’ Category

How to stip tags, script and style off the HTML

September 21st, 2008

Havn’t you just wished sometimes that all the html, script and style tags would just vanish from the html pages and all you get is pure text (for fun and profit). Well, here’s how I am managing it :)

require "open-uri"
require "hpricot"
require "sanitize"

html = open("http://www.google.com")
hp = Hpricot(html.read)
hp.search("script").remove
hp.search("style").remove
sanitize(hp.innerHTML, okTags="")

And output?

“GoogleWeb Images News Orkut Groups Gmail more ▼ Books Scholar Blogs YouTube Calendar Photos Documents Reader even more » iGoogle | Sign inIndia   Advanced Search  Preferences  Language ToolsSearch: the web pages from India Google.co.in offered in: Hindi Bengali Telugu Marathi Tamil Gujarati Kannada Malayalam Punjabi Advertising Programs - About Google - Go to Google.com©2008 - Privacy”

Now you can use this text to any imaginable use - as I mentioned earlier - maybe fun & profit :)

Libraries - hpricot, sanitize, open-uri

Have fun!

Posted in Ruby | Comments (1)

Getting back to the drawing board…

August 3rd, 2008


Drawing boards are so much more fun than paper’n pencil :)

Posted in ApnaBill, OpenSocial, Rails, Ruby, Startups, Web 2.0 | Comments (0)

Zarro boogs found!

July 31st, 2008

0 Bugs

This is the sweetest possible message Bugzilla can ever show a developer :D
Tonight, I’ll be syncing live ApnaBill.com with the most stable version we ever had - the operations are still getting formalized but the code-base looks awesome!

If you come across any bug @ ApnaBill.com, feel free to ping us at our support channel - who knows, you might just save the day for thousands of users :)

Posted in ApnaBill, Ruby, Startups, Web 2.0 | Comments (0)

A teaser from ApnaBill.com

May 30th, 2008

The current week is just halfway through, but it has already been one of the most eventful week for ApnaBill.com - with some definite advances towards the release. We’ll be launching the live pilot mode release in just days time.

More good news - we’ve expanded our network coverage from a handful of operators/cities to a pan India coverage.


We’ll be supporting 16 different prepaid vendors across 14 different geographical locations - I must say, that is a mighty list!

And to give you a small hint with what’s in the store, I present to you, bits and pieces from ApnaBill.com UI - all this and much more is just waiting to go live!


All the hard work that has gone into cross browser support…


A self-explanatory teaser on the front page


Another teaser :)


The prepaid coupon types we’ll be selling…

  
Secure transactions - with subtle hints on the UI


And of course, the ubiquitous help!

Please do signup for our Beta Release if you have not done so. The Beta Signup is still open.

Posted in ApnaBill, Ruby, Startups, Web 2.0 | Comments (2)

Giving wings to RAKA - graphing ability!

May 28th, 2008

Brightest ideas hit you when you least expect them. I was trying so hard all evening to put RAKA’s output into a format Graphviz could understand - but had no luck. I tried arrays, hashes, stack and what not - but nothing seemed right (or I wasn’t doing it right :) ) - and at 4am - right at the moment when I was packing up for the night - it hit me! - Just like a big bright Bulb!

Why not XML? Graphviz can very well render an XML (Some examples from ruby-graphviz gem) - and I was already able to output my tree in much similar format - whoa! 5 more minutes and I had this…


Read the graph - left to right, top to bottom. If X is displayed before Y in the graph, X is coded before Y.

And this is the code it corresponds to…

  1.  
  2. require "pp"
  3.  
  4. module Junk
  5.  
  6.   class Maku
  7.  
  8.     def say_hello(str)
  9.       puts "hello #{str}"
  10.       a = 1+1
  11.       if a == 2
  12.         if a == 1
  13.           puts "Yahoo"
  14.           if a == 10
  15.             puts "Microsoft"
  16.           else
  17.             puts "Google"
  18.           end
  19.         end
  20.         pp a
  21.       else
  22.         puts "bye"
  23.       end
  24.       puts ""
  25.       puts ""
  26.     end
  27.   end
  28.  
  29. end

Here is a side-by-side comparison of the output

The graph seems more like in vertical direction - however a more horizontal direction would look much better - what say?

Oh yes, I have a new expansion for RAKA - “A Ruby Klass Analyzer” :)

Posted in RAKA, Ruby | Comments (2)

Static call graphs for Ruby code

May 27th, 2008

Ever since my bug list for ApnaBill.com started to go down, I was worried that what am I going to do in all the free time which will suddenly be created. So much so, I was even creating unnecessary tasks and them scratching them out upon realizing that they are just not needed - sure shot symptoms of working too lately!

While working on ApnaBill, I felt a huge urge to document my code - specially the libraries I wrote and some of the complex controllers. Surely enough the code is very well documented - infact, at times makes pun and even tells stories - but I was always missing a call graph generation tool, which when given my Rails project, can graph out all the calls each of the actions make. I am not talking about profile time graphs - but something pretty static, which just looks at your ruby code and draws a graph.

…And just why is it needed? Documentation - ofcourse! A colored and connected graph, which can take you directly to the source code is alot easier to digest than just the code - specially for those who have not written that piece of code. This can be real useful in many scenarios.

Ok, so I started my search two days ago, on Saturday night - and for all night, I was crawling web pages like a googlebot - trying to find the relevant pieces of information. I did came across some fantastic projects like RailRoad, RCov and ruby-prof - but there was nothing that could satisfy my need. I even tried Doxygen (which is still to support Ruby), rdoc and almost all the other doc tools mentioned at “Other tools” seciton on Doxygen’s website.

Continuing my {re}search, I came across ParseTree. Its a C extension which churns out Sexp representation of Ruby code. You can parse ruby code in classes or methods or strings format. The library (apart from other stuff) - basically gives you a callback like access to various parts of the Ruby language. So “process_defn” would be invoked when any “def my_method()” is encountered.

There isn’t much on the internet about how to use ParseTree but the code samples and RDoc included in the gem are a good start. And then there’s the code installed by the gem - which you can always read at leisure. Another document worth checking out is Ryan’s Dependency Analyzer - where he does a bit of explaining as well.

Okay, back to the problem in hand - How can I draw a call graph of my code without actually executing it. I don’t want to execute it because it cannot guarantee 100% path coverage. Further, my objective is to document the code rather than profile it.

After my initial attempts, I was finally able to create a hierarchical structure of the code (which is utterly basic as of now).

  1.  
  2. # Target code which is to be graphed
  3. # Save as "mini.rb" in your current directory
  4. module Junk
  5.  
  6.   class Maku
  7.  
  8.     def say_hello(str)
  9.       puts "hello #{str}"
  10.       a = 1+1
  11.       if a == 2
  12.         if a == 1
  13.           puts "Yahoo"
  14.           if a == 10
  15.             puts "Microsoft"
  16.           else
  17.             puts "Google"
  18.           end
  19.         end
  20.         pp a
  21.       else
  22.         puts "bye"
  23.       end
  24.     end
  25.   end
  26.  
  27. end

Introducing RAKA

  1.  
  2. #!/usr/bin/env ruby
  3. # RAKA - A Ruby Kode Analyzer
  4. # Save in same directory as mini.rb
  5.  
  6. require "pp"
  7. require ‘rubygems’
  8. require ‘parse_tree’
  9. require ’sexp_processor’
  10.  
  11. class Raka < SexpProcessor
  12.  
  13.   attr_accessor :tabber
  14.  
  15.   def initialize
  16.     super
  17.     @tabber = 0
  18.     self.strict = false
  19.     self.auto_shift_type = true
  20.   end
  21.  
  22.   # Just a wrapper for s method.
  23.   # Usefull in decrementing tabbing structure once processing is done.
  24.   def s(*args)
  25.     result = super(*args)
  26.     @tabber -= 1
  27.     return result
  28.   end
  29.  
  30.   def process_fcall(exp)
  31.     @tabber += 1
  32.     name = exp.shift
  33.     display "CALL #{name}"
  34.     return s(:fcall, exp.shift)
  35.   end
  36.  
  37.   def process_if(exp)
  38.     @tabber += 1
  39.     display "IF"
  40.     test = exp.shift
  41.     process(test)
  42.     left = exp.shift
  43.     process(left)
  44.     display "ELSE"
  45.     right = exp.shift
  46.     process(right)
  47.     display "END"
  48.     return s(:if, test, left, right)
  49.   end
  50.  
  51.   def process_defn(exp)
  52.     @tabber += 1
  53.     name = exp.shift
  54.     display "DEF #{name}"
  55.     args = process exp.shift
  56.     body = process exp.shift
  57.     display "END"
  58.     return s(:defn, name, args, body)
  59.   end
  60.  
  61.   private
  62.  
  63.   def display(str)
  64.     @tabber.times{ print "\t" }
  65.     puts "#{str}"
  66.   end
  67.  
  68. end
  69.  
  70. require "mini"
  71. puts "============Junk::Maku============"
  72. Raka.new.process(*ParseTree.new.parse_tree(Junk::Maku))

The result

call graph for mini.rb

Woha! As you can see, I am able to relate the code structure with the calls and if statements. If I can output this into a format which Graphviz can understand, we’ll have exactly what we want!

Too bad, its almost 6am - and I have to catch up on some sleep :( I’ll have to stop here - more on the project as things with RAKA proceed.

Comments and suggestions are more than welcome :)

Posted in It calls for a blog..., Ruby | Comments (2)

LITBox helper for Rails

April 24th, 2008

Introducing - LITBox Helper for Rails!

Disclaimer: This is a work in progress and is available on “as is” basis.

For long, I was trying to integrate Criag Ambros’s RedBox plugin into ApnaBill.com project for implementing modal dialogs but somehow, the CSS always used to get upset, rendering the lightbox in such a way that the content which is to be shown inside it, shows below it.

Then I came across Ryan J Lowe’s LITBox(The site seems to down, try Google cache)- and boy, it rocks! Just 10 seconds setup, very light weight and looks brilliant! The code is nicely written too. But the only drawback was that it was plain JS stuff - so that means no Rails helpers available.

So, once the test implementation was done with, I decided to write a small helper myself. I’ve seen how Jorge’s Prototype Window Class’s rails helper works - so decided to base my code on the same lines. And since he’s already written the Ruby->JS part for converting between optional arguments, this didn’t seemed a daunting task even for a Wednesday night :)

  1.  
  2. # Based on Prototype Window Class Helper (http://pwc-helper.xurdeonrails.com)
  3.  
  4. module LitboxHelper
  5.  
  6.     def params_for_javascript(params) #options_for_javascript doesn’t works fine
  7.         ‘{’ + params.map {|k, v| "#{k}: #{
  8.        case v
  9.          when Hash then params_for_javascript( v )
  10.          when String then "‘#{v}’"          
  11.        else v   #Isn’t neither Hash or String
  12.        end }"}.sort.join(‘, ‘) + ‘}’
  13.     end
  14.    
  15.  
  16.     # Returns a default LITBox window.
  17.     def litbox_window(link_text, href, html_options={}, params={})
  18.       prefix = "<a "
  19.       # Add all html options as key="value" pairs
  20.       html_options.each do |k, v|
  21.         prefix += "#{k}=\"#{v}\" "
  22.       end
  23.       prefix += "href=\"javascript:void(null);\" onclick=\""
  24.      
  25.       # JS Payload
  26.       payload = "new LITBox(’#{href}’, #{params_for_javascript(params)});"
  27.      
  28.       # Rest of the stuff including link text
  29.       suffix = "\">#{link_text}</a>"
  30.      
  31.       return prefix + payload + suffix
  32.     end
  33.  
  34. end

I’m trying to make it feature complete so that I can make it available for download.

Till then… happy hacking!

Posted in ApnaBill, Rails, Ruby, Web 2.0 | Comments (5)

Ruby, Java, Python, C++ Job Trends

February 13th, 2008

Pretty interesting - Indeed (.com) :)

Ruby, Java, Python, C++ Job Trends | Indeed.com

Tags:

Posted in Ruby | Comments (2)

Ruby Meetup - 10th Oct, Atlanta

October 11th, 2007


Ruby Meetup, 10th Oct 07 - Atlanta
Leftmost - Zundre in white and Bob next to him :)
Where am I? (think green! wink wink :) )

Posted in Rails, Ruby, makuchaku in USofA | Comments (0)

From the Rails studio…

September 19th, 2007

Good morning :)

I am in Denver right now, the Rails Studio is about to start in 30 minutes… I was waiting for this training from past 2 months now - good to be here.

And gues what? Each one’s got a cool Rails T-shirt - which’s going to be one helluva source of evny for everyone back in Pune :D

Back to the studio… Pictures would be up soon!

[update]

Oh boy! I am surrounded by Mac’s - 90% attendees have a Mac here lol - this is one big difference between dev’s in India & US ;)

[/update]

Posted in Rails, Ruby, makuchaku in USofA | Comments (2)

Mechanizing Orkut!

September 15th, 2007

======My Orkut Scrapbook======
Album updated: ==> Hi!!!Happy vinayaka caviti :).
Tarun: ==> ok bye tc have a good day
Tarun: ==> thats coolso how u findind the place ?gone for long term or short?
….

Mechanize and Hpricot are some uber-cool libraries, available as gems on Rubyforge. Using them, I just managed to get into Orkut, logged in & opened my scrapbook - all programatically and fetched my scraps! Wheehaa!!!

Rokut - Orkut in Ruby!

This now opens up a whole new set of opportunities of what can be done with the available data :D

BTW, line 7 is to side-step a bug.

Posted in Ruby | Comments (0)

Fast forwarding Ruby…

July 8th, 2007

Saturday was a massively tiring day - Work started at 1AM, prepared atleast 100 questions for the oncoming day after finishing 3 books straight one after another! Slept at 4AM - plan was to get up at 7 & be in office by 9AM - but as always, dear sleep turned out to be so dear that I only managed to get up at 9AM, making it to office at only 10:50AM & got straight down to the business - after almost 6 hours of nonstop interviewing, the moment I reached back home I took the bed, slept from 7PM to 11:30 AM… & guess what? Got straight down to Ruby business as soon as I could - 12:30PM :)

So… just finished 11 chapters from Beginning Ruby & did a few sample programs - & I hope that should be enough to get me started hands on with Rails :D

Off to reading Agile Web Development with Rails

Posted in Ruby | Comments (5)

A crash course in Ruby!

July 6th, 2007

First program I saw on Ruby was the greeter code on ruby-lang.org & coming from Python world, it seemed very intuitive. Next came the 20 minutes Ruby Quickstart which actually took a lot less :D - tried some stuff in the browser console :)

I just managed to finish the Appendix A of Beginning Ruby & am already feeling much at home with Ruby. Lets see how soon can I get my hands dirty with Rails! But first… some sleep :D

[Update]

You need to check this out!!!
Envying Rails!

Part 1, 2, 3, 4

[/update]

Posted in Ruby | Comments (2)