Thursday, December 28, 2006

How to use Dynamic Object invocation in Rails for Models (String to Model conversion)

Requirement
-------------
You have String value of the model (model name) and you want to dynamically invoke methods on this model. This comes handy when you want to parse controller/action and do some neat nifty tricks.


Solution
-----------
model_name = "invoice"

model_name_capital = model_name.capitalize # => this creates "Invoice"

@object = Object.const_get(model_name).find(:all) # => This is equivalent to Invoice.find_all

Wednesday, December 27, 2006

Ruby 4 Lines of Web service/SOAP Code Magic

This is amazing to see how easy and simple it is to write a SOAP code in ruby, have a look at the following code. This does call Google search engine by passing some random search query.

require 'soap/wsdlDriver'
factory = SOAP::WSDLDriverFactory.new("http://api.google.com/GoogleSearch.wsdl")
service = factory.create_rpc_driver
@service_output = service.doGoogleSearch("xxxxxxxxxxxxxx", "Ajuby", 0, 10, "false", "", "false", "", "latin1", "latin1")

That's it.. @service_output returns what I call it as "Ruby's intelligent data structure" which you can use to get respective data output

e.g.
- Results: <%=@service_output.estimatedTotalResultsCount%>
- searchTime: <%=@service_output.searchTime%>


Note: Above "xxxxxxxxxxxxx" is the api key that you need to obtain from Google to use their web services, its less than a minute job to get that from google.

Now there is a better and more modular way of writing above code using ActionWebService
where you can manage API's to access outgoing SOAP calls more effectively.

Monday, November 27, 2006

XSD to WSDL Conversion

# Good site to refer contract-first web service and XSL that translates XSD to WSDL
# Also Christian Weyer's jWSCF for Eclipse
# ObjectWeb's Celtix ESB's xsd2wsdl tool

Surprisingly first 2 generates different type of WSDL for same XSD.

Tuesday, October 10, 2006

Legends/Great Programmers answer simple but important questions..

This one is a classic page, should go in a history of software/hardware. Author of this page has done good job in asking simple but interesting (in terms of these great guys and their outlook towards simple things in their day to day life).
I liked the question "If you had three months to learn one relatively new technology, which one would You choose?". Answers to this question can project one vital thing "What is the interesting technology/concept or thing which you would see helping technology future"


Monday, October 09, 2006

Java String replace: java.lang.IndexOutOfBoundsException: No group 2

Issue

If you see java.lang.IndexOutOfBoundsException: No group 2 , when you are doing <String>.replaceFirst(<source string>, <target string>) or <String>.replaceAll(<source string>, <target string>).

Reason

You possibly have "$" character in your target string.

Solution

Add java.util.regex.Matcher.quoteReplacement(<target string>), before you apply replace function.

Bug reported on Sun site by someone.

Thursday, October 05, 2006

OpenAppDotOrg Toolkit hits 100 downloads!!.


Our Ruby/Rails based open source project OpenAppDotOrg finally hits 100 downloads on sourceforge . Groovy!!... See the captured snapshot of that moment on the left. This is just a beginning, we have a long way to go and I will come back to toast 1,000.....of downloads.

Current release 0.2515 includes, things like
  • Common user services (sign up/email support/forgot password etc)
  • User access control
  • Company/User relationship
  • First open source Feed management module

  1. More about the project can be found on our wiki
  2. Download the latest version from sourceforge
  3. Our community on Google groups and openapp forum

Tuesday, October 03, 2006

Rails Association example: has_many relationship

After a long time, I finally get to write this up. Step by step instructions to build Rails:ActiveRecord: has_many relationship.

Once you start with this , you can sense the power of this meta modeling techique which turns your business application/model building into a snap.

I will write up more going deeper into complex techniques of Rails association, meanwhile few interesting sites explain this as follows

Friday, August 18, 2006

Ruby/Rails tricks and tips with ActiveRecord

I had a simple problem definition:
  • Write a installer application in rails which will take database username, password and database name
  • Creates brand new database
  • Populate the database with schema and reference data.
  • Updates database.yml file
  • Reinitialize the rails activerecord connection context, so that user does not have to restart the application
This is how I solved it. few steps , hopefully this will be usefull for other ppl who wants to do something similar.


# Create new database

ActiveRecord::Base.connection.create_database("rex_boy") #rex_boy new database name.

# Load new database with schema and data.
.........This you can do either by using writing Rake migrate tasks or manually write SQL's to load the schema and data, 1000's of ways to do this. I did using creating using migrations and invoking rake command from rails.


# Read and update the database.yml file with newly created database

## Read the Yml and update with new database name
@config = YAML::load(File.open("#{RAILS_ROOT}/config/database.yml")) @config['development']['database']= "rex_boy"

## Now update the yml file back. basically write the database.yml back from @config object
File.open( "#{RAILS_ROOT}/config/database.yml", 'w' ) do |f| f << @config.to_yaml

# Now the interesting part to reintialize ActiveRecord connection context without restarting the server (Apache or WEBrick or Lighttpd..)

ActiveRecord::Base.configurations = @config ActiveRecord::Base.establish_connection ActiveRecord::Base.connection



This should get you very fancy in terms of building rails based installer application.

Thursday, August 10, 2006

Framework comparison

Check this video (almost 340MB) out, Its fun and gives users a quick glance at all the frameworks that author of this video is trying to use. He is comparing J2EE vs Ruby on Rails vs Zope vs TurboGears vs Django.

Friday, August 04, 2006

Rails based framework for Java. Co-existence or contender?

I was sailing thru the net to see if there is anyone doing Rails framework on Java?. so some of the following guys who are doing few things similar to rails, not all.

# Trails: domain driven development framework in the spirit of Ruby on Rails or Naked Objects. Uses Spring, Tapestry, and Hibernate. Probably this one has a long way to go. Not too much happening in this area.
# Sails: Purely framework designed by keeping Rails in mind. Have a download section.It would be interesting to download and play with it.
# Grails: Sounds the closest one to rails, including controller/model generation, scaffolding etc, its written in Groovy
# JBoss Seam: Probably most active and adopted project as compared to above. Seam uses EJB3 and Hibernate (both, I wonder why?. Probably things around Transaction support and things like heavy weight vs light weight components usage).

I will keep watching this space and add if i run into any other puppy who wants to be or do things like rails in Java.

Friday, June 09, 2006

Quick view on Computer Software/Programming Languages History

I found this great collection of pictures of Programming language creators/authors, surely a Wall of Fame for all those people who wants to be inspired by probably the best men to walk over this planet earth. Men who made today's world of computer/software/programming/ possible

Apache would not start if Skype is running

I wasted 30 mins figuring this out without knowing what could be the real reason. If you are trying to start your apache (with or without mod_ssl/:443 port enabled) and if you have Skype already running on your machine, Apache won't start, it will not create any error log, so you go look at your event viewer you will see something like

  • System Log: "The Apache2 service terminated with service-specific error 1." ; Arghh!! so helpful ;)
  • Application Log: "The Apache service named reported the following error:
    >>> Unable to open logs" , "The Apache service named reported the following error:
    >>> no listening sockets available, shutting down . "

Now the last message is little useful and it says that there is something already listening/running on port 80/443 , But hunting down was tough!. Because I don't have any other web server running. Root cause is "Skype"

Go to Skype->Tools->Options->Connection-> Uncheck "use 80 and 443 for incoming connection"

Who would ever thing my IM will waste my time, where as IM's are supposed to be productivity improvement tools :) , Anyways a good find and I hope it will be useful for others too. BTW No complains for skype, its a great tool.

Tuesday, June 06, 2006

Funky Browser based ruby shell

Tryruby is Neat!!...but couple of things are hack, Like I type

require 'abracadabra'
=>true

Now I don't think anyone has written gem called abracadabra yet. but this shell is cool, very handy. I tried quick String manipulation test, it works!

Monday, June 05, 2006

Is MySpace becoming IPOD for social community business platform

As usual I landed on Techcrunch website and what I see first 2 posts (companies), doing their business around MySpace. Like ,
  • SingleStat.us: Its a funny concept, but works for lot of singles or people with old crush. Techcrunch says "Want to know when that cute friend of yours breaks up with her boyfriend....a service that let’s you type in anyone’s Myspace profile and be notified by email if their relationship status changes".
  • Nooz: Interactive news service aimed for MySpace users, to start with.
  • and lot many...

So the question of the day: Is MySpace becoming IPOD for social community business platform. Like IPOD did, people made lots of dough by spinning their business around IPOD, concepts around IPOD. Now MySapce universe is emerging in a way, it's helping lots of Web2.0 startups to grow.

Monday, May 29, 2006

Buttonator....

Pretty nifty tool/site create buttons. It's very handy. check it out, world of web 2.0 design is getting easier every step of the way.

Friday, May 19, 2006

Ruby Java Bridge is here

Ruby bridge to Java is here, Not sure how smooth it is but surely worth a try.

Thursday, May 11, 2006

How Ruby can be next generation business building block

  • As Martin fowler said "As a result it certainly seems to me that certain languages are more suited to in-language DSL than others. Seeing lisp and smalltalk I concluded that the more suitable languages were minimalist ones with a single basic idea that's deeper and simpler than traditional languages (function application for lisp, objects and messages for smalltalk). But Ruby is more conventional and a much bigger language than these two, yet is still suitable for in-language DSLs."
So Ruby certainly sounds like a language which can rapidize (Not sure that's legal word or not, but I felt like using it here) business application building.

What does Rails on Apache mean?

Assuming you have a running rails application on WEBrick (which comes with rails), its 5 mins job to have rails up and running on Apache. You will need following things.
Remember You do not need FastCGI at all to run rails on Apache. .

Every rails application created is Apache deployable ready (that sounds so good!!), you don't need anything extra, neither FastCGI or Mongrel. You will need these 2 puppies for some other reasons that I will point out soon.

# Apache HTTP server
# Rails application

2 step process

- Open Apache httpd.conf under /conf.
- Add following at the very end and restart Apache

Listen 8027
<VirtualHost 127.0.0.1:8027>

DocumentRoot [railsapppath]/public/
<Directory [railsapppath]/public/>
Options ExecCGI FollowSymLinks
AddHandler cgi-script .cgi
AllowOverride all
Allow from all
Order allow,deny
</Directory>
</VirtualHost>

Or another better way is to (Use Apache Alias technique) add following in your httpd.conf

Alias /railsappname [railsapppath]/public


<Directory [railsapppath]/public>

Options ExecCGI FollowSymLinks

AllowOverride all

Order allow,deny

Allow from all


</Directory>

- Open public/.htaccess file and add "RewriteBase /railsappname "

(With above technique you will not be creating virtual host on different port and this kind of setup makes more sense when you have more than one rails component sitting on Apache)


Here

[railsapppath]/public/: is your rails application public folder. You need to point to this directory, else it won't work.

Port 8027: some random port, on which you are asking Apache to run a virtual host for your rails application.

Now just point your browser to http://127.0.0.1:8007/ and you will see the rails running on Apache. How fast was that?. Oh did I ask How fast was that?. Yes now this takes us to our next topic

# Fast CGI: Fast CGI makes ruby run faster on Apache, otherwise by itself ruby is slow on Apache, because it runs native CGI. I will talk on this in detail, in my next blog topic

# Mongrel : This puppy is getting its traction. Right now working good in Linux and Windows, not so good for Mac OSX. But this one is supposed to be better than FastCGI. I still need to experiment that.

Thursday, April 13, 2006

Difference between Web 1.0 and Web 2.0

This is a good one and seriously true. Everything is BETA , even though product is launched for 100 years it will stay BETA forever. Google started it and the whole world is doing it now.

Thursday, March 30, 2006

Ruby LoadError, Quick fix

Ruby library load error, takes long time for a newbie but simple hack could get you up and running with your thing.
I just installed RubyFul_Soup[ Ruby port of the hit Python HTML/XML parser Beautiful Soup.] and wanted get started with RadRails , so when I write this line on top

require 'RubyFulSoup' as per the example given I get following error

LoadError: No such file to load -- rubyfulsoup

The thing is when you install any gem using "gem install" , it installs gem under /usr/lib/ruby/gems/1.8/gems , so somehow ruby does not really get where to pick up gems from , best thing to do a quick sanity check regarding your setup , and you can do that by "irb" (Interactive Ruby Shell), just type
bash> irb
irb(main):002:0> require 'RubyFulSoup'
LoadError: No such file to load -- RubyFulSoup
from (irb):2:in `require'
from (irb):2

basically gem installed is "rubyful_soup' and its under /usr/lib/ruby/gems/1.8/gems
and Ruby looks for files under /usr/lib/ruby/1.8/ , So all I did is copied rubyful_soup.rb file from /usr/lib/ruby/gems/1.8/gems to /usr/lib/ruby/1.8/ and also copy htmltools gem files to /usr/lib/ruby/1.8/, then run

irb(main):001:0> require 'rubyful_soup'
=> true

This is a quick fix till I figure out the real solution for this


Wednesday, March 22, 2006

RadRails puts RoR on Web2.0 development Autobahn

It took me less than 50% time to get a small database RWD (Read-Write-Delete) application in RoR (Ruby On Rails) as compared to traditional Java application on Eclipse. How I did that, using RadRails
What is RadRails, Its a bomb!, its amazing, seriously I was never this excited, when Eclipse happened to Java/J2EE world. Why because it took me more time get Eclipse up and running with my java project and hook tomcat to it , yadda yadda!. But I could count it on on my fingers how long it took me get one simple rails app up and running with built in WEBrick server. It was just less than 5 mins, I had simple customer application with DB read/write/delete running on my browser.
RadRails is written on Eclipse framework, but extremely sexy, in terms producing components. Like rails/ruby/database/subversion support etc.

Some of the cool features
  • mvc key bindings : To jump one context of development to another, moving between view (.rhtml ) to app controller faster.
  • Generate tabs: All rails generate command on your fingertips
  • Console/Tail : Very helpful in AJAX development where you can see data going back and forth between your client and server.

Oh yeah!, did I say how It was this quick , have a look at the screencast , you got to love this stuff!!..

There is a famous say "You need a reason to convince yourself, why you should do different deed", I guess RadRails is my reason to love Ruby/RoR.

So finally I would say "Got RoR!"

Thursday, February 23, 2006

''Home-Sourcing'' vs. Offshoring - Outsourcing - CFO.com

What is next when offsourcing/offshoring becomes hard to manage in terms of money and transaction monitoring

Monday, January 16, 2006

.htaccess usage on Apache doesn't work?, See what you are missing.

I wasted couple of hours on this, So sharing this one with everyone so that I can save someone else's time.

Simplest way to implement directory level authorization on Apache HTTP server is to use .htaccess file with password file combinations, You will find How to Implement Authorization on Apache everywhere. But there are couple of things which you will not find in these sites, because they assume that you would have done this before or you know about this already. So let me run thru basic steps and steps which are generally ignored (but very important get this out there)

Basic Steps
  1. Create .htaccess file with all the directives like AuthType,AuthName,AuthUserFile,Require attributes
  2. Create password file (as specified in AuthUserFile) using htpasswd command.
usually Ignored Steps (in your httpd.conf file make sure following lines are not commented out)
  1. LoadModule auth_module libexec/httpd/mod_auth.so
  2. AddModule mod_auth.c

  3. Order allow,deny
    Deny from all
    Satisfy All

  4. Most important step which makes or brakes this whole exercise
    1. "AllowOverride none" should be replaced by "AllowOverride AuthConfig"

With above you should be up and running with Apache Basic Auth.