MySQL doesn’t have a built in type for an IP Address, PostgreSQL does though. You’ll find that ip addresses are often stored as an integer. You can translate between an integer and ip address and vice versa with a built in MySQL functions. In a recent piece of work we had to detect a user’s country code based on their incoming IP via against a range of IPs (stored as integers).
mysql> SELECT INET_ATON('192.168.0.1');
| INET_ATON('192.168.0.1') |
| 3232235521 |
1 row in set (0.00 sec)
mysql> SELECT INET_NTOA('3232235521');
| INET_NTOA('3232235521') |
| 192.168.0.1 |
1 row in set (0.00 sec)
Wouldn’t it be nice to get a Rails model to accept an ip address and store it as an integer. Well its basically serializing the ip address and using Rails 3.1’s new serialization api we can do the following.
# Converts IP to number
return unless n
# Converts number to IP
Basicially a class with two methods IpEncoder#load encodes its input, and IpEncoder#dump decodes it. Then you simply add the following to your model.
class Log < ActiveRecord::Base
serialize :ip_address, IpEncoder.new
And there you have it.
Rob-Laceys-MacBook-Pro:loathsome roblacey$ ./script/rails c
Loading development environment (Rails 3.1.2)
>> Log.create(:ip_address => '192.168.0.1')
SQL (0.5ms) INSERT INTO "logs" ("ip_address") VALUES (?) [["ip_address", 3232235521]]
=> #<Log id: 1, ip_address: "192.168.0.1">
I was mulling at the end of last year that while I’ve been programming for far too many years now its been for the most part web development and databases. But I don’t know how to build a GUI, and least of all one that would be cross platform. Time to learn.
I’ve just picked myself up a copy of O’Reilly Java Swing 2nd Edition which, while its old, should help me get to grips with building my first GUIs.
As an interesting learning process I’ve decided that while of course this is all about Java that I am going to read the book in Java, I shall translate it all into JRuby since I use Ruby as my programming language of choice. So I’ll learn three skills in one;
- GUI Development
I think the best way to pick it up is to go through every last example until it becomes second nature so all the Java AWT and Swing examples I come across in the book I shall be adding in a new jruby-swing github repository are here as JRuby.
In fact here’s the first example
Desktop app here I come.
Needed to get rid of PostegreSQL on start up. That’s freeing up 8Mb of memory….its better than nothing.
rails@cool-server-name-001:~$ sudo update-rc.d -f postgresql-8.3 remove
Removing any system startup links for /etc/init.d/postgresql-8.3 ...
Just signed up to a new New Relic account today for a client and got a free t-shirt to book. Now its not that I’m without clothes but I do like grey and I’m not afraid to admit I am a nerd. So why the hell not. All you need to do is sign up and deploy your first app which isn’t that much of a bother really and I was going to do it anyway.
Sign up for New Relic and get your free t-shirt and that also gives you 10% off your first bill which is nothing if you use the free version and $50 off my own bill that I am not paying anyway because I’m using the free version :)
Its all about the FREE.
Just a few resources for the almighty JRuby Desktop app I will one day build, maybe.
Installed latest Rubygems which broken my dev environment.
Rob-Laceys-MacBook-Pro:app roblacey$ ./script/rails s
/Library/Ruby/Site/1.8/rubygems/specification.rb:990:in `date=': invalid date format in specification: "2011-9-23" (Gem::InvalidSpecificationException)
Â Â from /Users/roblacey/repos/app/ruby/1.8/bundler/gems/compass-cb709350942f/compass.gemspec:7
Â Â from /Library/Ruby/Site/1.8/rubygems/specification.rb:1346:in `initialize'
Â Â from /Users/roblacey/repos/app/ruby/1.8/bundler/gems/compass-cb709350942f/compass.gemspec:4:in `new'
Â Â from /Users/roblacey/repos/app/ruby/1.8/bundler/gems/compass-cb709350942f/compass.gemspec:4
One to remember next time something screwy happens, and I am to lazy to use rvm
sudo gem uninstall rubygem-update -v 1.8.10
sudo gem install rubygems-update -v 1.5.2
sudo update_rubygems _1.5.2_
In order to start moving with building Warcraft AddOns you might want to extract the Blizzard UI Code and Art bundles. A little bit fiddly but…
/Applications/World\ of\ Warcraft/World\ of\ Warcraft.app/Contents/MacOS/World\ of\ Warcraft -console
On the login screen hit /~ to open the console and type
> exportInterfaceFiles code
> exportInterfaceFiles art
You should now have two new directories in your World Of Warcraft (/Applications/World of Warcraft/) BlizzardInterfaceArt and BlizzardInterfaceCode directory.
Rob-Laceys-MacBook-Pro:World of Warcraft roblacey$ ls -la
drwxrwxrwx 21 roblacey admin 714 24 May 08:59 .
drwxrwxr-x+ 71 root admin 2414 22 May 11:45 ..
-rw-r--r--@ 1 rl admin 12292 24 May 08:59 .DS_Store
drwxrwxrwx 3 rl admin 102 3 May 18:35 Background Downloader.app
drwxrwxrwx 3 rl admin 102 3 May 18:35 Blizzard Updater.app
drwxrwxrwx 3 rl admin 102 24 May 08:59 BlizzardInterfaceArt
drwxrwxrwx 3 rl admin 102 24 May 08:59 BlizzardInterfaceCode
drwxrwxrwx 4 rl admin 136 21 Apr 23:50 Cache
drwxrwxrwx 30 rl admin 1020 24 May 08:59 Data
drwxrwxrwx 2 rl admin 68 18 Apr 22:19 Errors
drwxrwxrwx 3 rl admin 102 22 Apr 00:03 Interface
drwxrwxrwx 11 rl admin 374 22 May 11:45 Logs
drwxrwxrwx 2 rl admin 68 3 May 21:02 Movies
-rwxrwxrwx 1 rl admin 67140 22 May 11:45 Patch.html
drwxrwxrwx 11 rl admin 374 22 May 11:50 Updates
drwxrwxrwx 5 rl admin 170 22 May 11:56 WTF
-rwxrwxrwx 1 rl admin 177 24 May 08:59 WoW.mfil
-rwxrwxrwx 1 rl admin 1927052 22 May 11:45 WoW.tfil
drwxrwxrwx 3 rl admin 102 3 May 18:35 World of Warcraft Launcher.app
drwxrwxrwx 3 rl admin 102 3 May 18:35 World of Warcraft Repair.app
drwxrwxrwx 3 rl admin 102 22 May 11:45 World of Warcraft.app
My first World Of Warcraft AddOn, ok so I found a nice tutorial on WowWiki – http://www.wowwiki.com/AddOn_programming_tutorial/Introduction .
## Interface: 40000
## Title: Hello World!
## Notes: My first AddOn
And there if you look in the chat console ‘Hello World!’. Kungla is so impressed he needs to have a proper long sit down.
redirects = 
redirects << params[:redirect_to] || request.referrer
…does not mean this…
redirects = 
redirects << (params[:redirect_to] || request.referrer)
…it means this.
redirects = 
(redirects << params[:redirect_to]) || request.referrer