Thursday, July 17, 2008

Wednesday, July 16, 2008

Non-English Chars in query string

Non-English chars when passed through query string can not be rebuilt to their orginal format unless some sort of encoding / decoding is applied to the chars.

In my project I had to receive Danish chars sent through query string and store them in database, the problem while saving the data was the Danish chars were either substituted by special chars (like ? or %) or were totally ignored.

After bit of R&D it was found that the query strings can be rebuilt by proper char encoding conversions. I used Ruby's Iconv library to achieve this.

An inside look

Usually we set SQL server in proper language mode. Like for danish support you would require

Danish_Norwegian_CI_AS mode enabled.


But rails by default is not equipped to differentiate lang. support. Usually rails is in UTF-8 mode.

Scenario : We need to compare for uniqueness of a user name column in users table which can contain Danish chars also. ( This is just an example as this validation can be done at Model level itself )

My comparison for existing record failed by using Find method

User.find_by_name('Bålåji')

Problem : Rails converted the string (
Bålåji) to UTF-8 mode forming a new string ( substituting å with some special char) thereby causing my Find method to return blank value projecting as if the record does not exist in the database and we can proceed with creating a new user.

Solution : Inorder to avoid such issue, I deliberately converted the input string to ISO-8859 format so as to preserver its original mode.

This can be done by

Iconv.conv('ISO-8859-1//IGNORE', 'UTF-8', STRING)


This converts a string from UTF-8 encoding to ISO-8859-1 encoding.

Now, when I try User.find_by_name('Bålåji') it returns me the correct match.

This conversions can be used in cases like

> importing values for a txt file / CSV .

> Comparing values in ruby standalone script.


Sunday, May 4, 2008

IP based restriction in Apache

We can restrict the access to any particular URL and its sub urls by modifying apache's Config. file

For example : if we have to restrict admin section of our site to be browsable only by few specified IP then in apache's Config. file we just have to add

<\Location /admin>
Order Allow,Deny
Allow from 191.16.25.62
<\/Location>
( Please remove the '\' before open and close Location tag's )


This is only allow requests coming from 191.16.25.62 , requests from any other IP will get

Forbidden

You don't have permission to access /admin on this server.

This restriction also applies to the sub-urls like admin/user_list etc....

Suppose we have to provide access to more than one IP, then we just have to provide the different IP's separated by a space between them.

Like

Allow from 191.16.25.62 191.16.25.63 ..........

The same can be applied for rejecting a request from a specified IP


Order Allow,Deny
Deny from 191.16.25.62

Monday, April 14, 2008

Increment/decrement operator's in Ruby

Ruby has no pre/post increment/decrement operator. For instance, x++ or x-- will fail to parse. More importantly, ++x or --x will do nothing! In fact, they behave as multiple unary prefix operators: -x == ---x == -----x == ...... To increment a number, simply write x += 1.

Methods in a class

To find the pre defined methods defined for a class type

ri classname
in your command prompt

Example --- ri File ; ri String