“...I've been working since 2008 with Ruby / Ruby on Rails, love a bit of Elixir / Phoenix and learning Rust. I also poke through other people's code and make PRs for OpenSource Ruby projects that sometimes make it. Currently working for InPay...”

Rob Lacey (contact@robl.me)
Senior Software Engineer, Brighton, UK

Resize multiple images in one bash script

Badoom……!!!

#!/bin/bash
for i in *.JPG; do echo $i; base=`basename "$i" .JPG`; convert "$i" -resize 50% "thumbs/$base.jpg"; done

Delete files created more than 14 days ago

When you’ve got lots of temporary files on a unix server and you never clean them out this might help. Remove files that were created more than 14 days ago like so…

find . -type f -mtime +14 -exec rm {} \;

doing away with www.

“www.” for the most part is a pointless idea. We all know what a web page is. The “www.” prefix is outdated although necessary evil. I guess the same could be said of http:// and https:// for web requests …we all know what it means.

Here’s a quick snippet of my apache config to push all traffic from www.loathso.me to loathso.me

<VirtualHost *>
  ServerName www.loathso.me
  ServerAlias loathso.me
  DocumentRoot /var/www/loathsome/current/public/
  RackEnv production
  RewriteEngine on
  RewriteCond %{HTTP_HOST} ^www\.loathso\.me
  RewriteRule ^/(.*)$ http://loathso.me/$1 [R=permanent,L]
  CustomLog /var/log/apache2/loathsome-access.log common
  ErrorLog  /var/log/apache2/loathsome-error.log
</VirtualHost>

Rails Serializers and INET_NTOA

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.

class IpEncoder
  #
  # Converts IP to number
  # inet_aton
  #
  def load(n)
    return unless n
    [n].pack("N").unpack("C*").join "."
  end

  #
  # Converts number to IP
  # inet_ntoa
  #
  def dump(n)
    n.split(/\./).map(&:to_i).pack("C*").unpack("N").first
  end

end

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.

require 'ip_encoder'
class Log < ActiveRecord::Base
  serialize :ip_address,  IpEncoder.new
end

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">

JRuby Swing

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
  • Java
  • JRuby

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.

https://github.com/braindeaf/jruby-swing

In fact here’s the first example

https://raw.github.com/braindeaf/jruby-swing/master/chapter2/ToolbarFrame1.rb

Desktop app here I come.

Disable startup items in Debian / Ubuntu

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 ...
   /etc/rc0.d/K21postgresql-8.3
   /etc/rc1.d/K21postgresql-8.3
   /etc/rc3.d/S19postgresql-8.3
   /etc/rc4.d/S19postgresql-8.3
   /etc/rc5.d/S19postgresql-8.3
   /etc/rc6.d/K21postgresql-8.3

New Relic - Get a FREE 'data nerd' t-shirt

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.

Facebook Open Graph (Complex Types)

Complex Types

Music API

JRuby Resources

Just a few resources for the almighty JRuby Desktop app I will one day build, maybe.

MIDI Keyboard

Screenshot

Netbeans Tutorial

Soft Token

Screencast

Darkness Clone

Downgrading Rubygems

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_