Technology Agnostic Rubyist : Open Source Lights The Way
Friday, April 3, 2009
Tuesday, March 31, 2009
Ruby on Rails Inflector for Nationality
I've given myself until midnight to complete the task I wrote about on November 25th.
The idea is for a Ruby Gem that provides a nationalize method for the String Class.
Any country code would be nationalized by simply typing something akin to "France".nationalize => "French" likewise "US".nationalize => "American"
As soon as I found a list country codes... I also realized I wouldn't have enough time to write the code and Gemify it. So I started hacking away at an Environment.rb file.
That got me something like this:
I decided this was getting a little cumbersome. Especially when I realized that any country with a space (char(32)) or other odd character in the name would need to be typecast as a :symbol in order for my code to work.
Options:
1) Devise out a work around, or
2) Plug away at the entire list of 230+ ISO country names and code for the exceptions
Of course one would prefer a more elegant and flexible solution. Have only two choices seemed awkward. So I did a little bit more research and found an XML list of countries.
This enabled another option:
3) Modify an XML country list to include nationality adjectives.
Now that I've modified the XML country list the data can be parsed, utlized, and published however one sees fit, in any computer program.
This is a flexible solution and completely language agnostic. I like it that way.
While I was thinking about this, I registered the domain name http://originfy.us. I will upload the XML file that I created to that domain.
The idea is for a Ruby Gem that provides a nationalize method for the String Class.
Any country code would be nationalized by simply typing something akin to "France".nationalize => "French" likewise "US".nationalize => "American"
As soon as I found a list country codes... I also realized I wouldn't have enough time to write the code and Gemify it. So I started hacking away at an Environment.rb file.
That got me something like this:
class String
# list of countries derived from http://www.internetworldstats.com/list2.htm#af
def nationalize
{
:Afghanistan => "Afghani",
:Africa => "African",
:Albania => "Albanian",
:Algeria => "Algerian",
:Andorra => "Andorra",
:Angola => "Angolan",
:Anguilla => "Anguilla",
:Antarctica => "Antarctic",
...
}[self.gsub(/_id$/, "").to_sym] || super
endI decided this was getting a little cumbersome. Especially when I realized that any country with a space (char(32)) or other odd character in the name would need to be typecast as a :symbol in order for my code to work.
Options:
1) Devise out a work around, or
2) Plug away at the entire list of 230+ ISO country names and code for the exceptions
Of course one would prefer a more elegant and flexible solution. Have only two choices seemed awkward. So I did a little bit more research and found an XML list of countries.
This enabled another option:
3) Modify an XML country list to include nationality adjectives.
Now that I've modified the XML country list the data can be parsed, utlized, and published however one sees fit, in any computer program.
This is a flexible solution and completely language agnostic. I like it that way.
While I was thinking about this, I registered the domain name http://originfy.us. I will upload the XML file that I created to that domain.
Tuesday, January 13, 2009
Tuesday, November 25, 2008
Write a nationality gem or plugin
This is just a reminder to self to write something that ties in with Ruby inflectors and the country_select helper.
Friday, November 14, 2008
Dirty numbers
/$#@!
This is a pretty basic programming concept but I haven't touched this blog in a while here we go...
Say you're working with numbers in a database field. Those numbers were keyed in by people using a different system, and the other system somehow stored non-numeric values in a numeric field.
Since Ruby does not have strong typing, an easy way to check and see if a field is an integer is to compare its integer value with its actual value.
if (somefield.to_i == somefield)
For example:
n = 1234567
if (n.to_i==n) => true
If the value of n is not keyed properly the equality test returns false
n = "12345x67"
if (n.to_i=="12345x67") => false
Easy!
This is a pretty basic programming concept but I haven't touched this blog in a while here we go...
Say you're working with numbers in a database field. Those numbers were keyed in by people using a different system, and the other system somehow stored non-numeric values in a numeric field.
Since Ruby does not have strong typing, an easy way to check and see if a field is an integer is to compare its integer value with its actual value.
if (somefield.to_i == somefield)
For example:
n = 1234567
if (n.to_i==n) => true
If the value of n is not keyed properly the equality test returns false
n = "12345x67"
if (n.to_i=="12345x67") => false
Easy!
Labels:
check,
conversion,
isint,
isinteger,
number,
Ruby,
validation,
varchar
Monday, March 24, 2008
Web site maintenance - global message
About once a week we upload and update all of the products in the database.
This intensive process takes about 15 minutes, during which time any pages that requires database access may time out.
There has to be a simple way to establish a "web site maintenance" message on the site while these processes run. Additionally cached pages need to be expired if they contain updated products.
I'm still working on a way to achieve this result.
This intensive process takes about 15 minutes, during which time any pages that requires database access may time out.
There has to be a simple way to establish a "web site maintenance" message on the site while these processes run. Additionally cached pages need to be expired if they contain updated products.
I'm still working on a way to achieve this result.
Labels:
global user message,
maintenance,
Ruby on Rails,
site offline
Monday, February 25, 2008
SSL Plugin for Rails
I'm using the acts_as_authenticated plugin for ruby on rails.
Unforunately it falls short in a few key areas for an ecommerce site. An obvious one is that it does not ensure all login pages are SSL encrypted.
So I wrote the simple method (explained below) and put it in the application.rb file.
in application.rb...
def redirect_to_ssl
redirect_to :protocol => "https://" unless (request.ssl? or local_request? or request.server_port.to_i!=80)
end
The reason for the last check of the server port != 80 is twofold. First, if the server port is equal to 80 then I am probably running in production mode. However if the server port is not 80 then I am either running a development box, or I'm already on port 443. Ports 80 and 443 are the standard port used in HTTP and SSL.
Any time I want to ensure that a page is SSL encrypted, I put the following bit of code in my controller as a before_filter.
in my SSL protected controller...
class MySslController < ApplicationController
before_filter :redirect_to_ssl, :except => actions_that_do_not_require_ssl
Voila! (or "Wahla!" as we americans like to say) it works.
Unforunately it falls short in a few key areas for an ecommerce site. An obvious one is that it does not ensure all login pages are SSL encrypted.
So I wrote the simple method (explained below) and put it in the application.rb file.
in application.rb...
def redirect_to_ssl
redirect_to :protocol => "https://" unless (request.ssl? or local_request? or request.server_port.to_i!=80)
end
The reason for the last check of the server port != 80 is twofold. First, if the server port is equal to 80 then I am probably running in production mode. However if the server port is not 80 then I am either running a development box, or I'm already on port 443. Ports 80 and 443 are the standard port used in HTTP and SSL.
Any time I want to ensure that a page is SSL encrypted, I put the following bit of code in my controller as a before_filter.
in my SSL protected controller...
class MySslController < ApplicationController
before_filter :redirect_to_ssl, :except => actions_that_do_not_require_ssl
Voila! (or "Wahla!" as we americans like to say) it works.
Subscribe to:
Posts (Atom)