A Fresh Cup is Mike Gunderloy's software development weblog, covering Ruby on Rails and whatever else I find interesting in the universe of software. I'm a full-time Rails developer and contributor, available for long- or short-term consulting, with solid experience in working as part of a distributed team. If you'd like to hire me, drop me a line. I'm also the author of Rails Rescue Handbook and Rails Freelancing Handbook.


A Fresh Cup

Notes on Rails and other development


Double Shot #473

Looking forward to the weekend, when I can actually be productive.


Double Shot #472

After a rough start with rubyforge, github, and lighthouse all being down, yesterday saw the release of a batch of software:

  • ConfigToolkit - Extensive and flexible code for reading file-based configuration information in ruby.

  • Programmers: Before you turn 40, get a plan B - As a programmer about to turn 50, I'm a bit leery of the notion that age discrimination in the field is as pervasive as some people say, but this blog post does a reasonable job of laying out the issues.

  • RubyGems 1.3.4 - Another point release.

  • Google Quick Search Box - Looks like Google wants to replace QuickSilver.

  • Wagn 1.0 - Milestone release for this slick-looking wiki. I need to find some time to dig in.

  • RedCloth 4.2.0 - New release with some formatting changes. Check the changelog before you update if you use bulleted or numbered lists.

  • Devver - Run tests in the cloud and get them back faster. Looks interesting.

  • Blocky - Crowdsourced spammer blocking for Twitter.

Finding Visitor Locations in Rails

I've got an application that I work on where the client wants to track which countries they're seeing click-throughs from. They want this in real time and in the application's UI, not in an external package such as Google Analytics. There are various ways to guess at country, but for the purposes of this application, basing it on the IP address and where its range is assigned proves to be good enough.

There are various services that will let you geolocate based on IP just by making an API call. But there's no particular reason to go outside your own application for this; it's pretty trivial to set up in Rails:

1) Download the "Complete (Country) One Table" SQL file from IPInfoDB. This is the raw data necessary to do the lookups. Decompress it and then run the resulting SQL file in your application's database. This will give you an ip_group_country table. There's no need to set up a matching model; we're just going to hit it with raw SQL.

2) In this particular application, we're maintaining an Event model that holds, among other things, the IP address of the request. So the easy answer is to geolocate instances of that model when they're created or updated:

[sourcecode language='ruby']
class Event < ActiveRecord::Base
before_save :get_country_info

def get_country_info
segments = remote_ip.split('.')
ip_atom = ((segments[0].to_i * 256 + segments[1].to_i) * 256 + segments[2].to_i)*256
result = connection.execute("SELECT * FROM `ip_group_country` where `ip_start` <= #{ip_atom} order by ip_start desc limit 1;")
row = result.fetch_row
self.country_code = row[2]
self.country_name = row[3]


3) There is no #3. That's it!

Double Shot #471

Had a fun evening of patching Rails sites. Hope you did too.


Double Shot #470

I'm sure something happened in Rails-land yesterday, but I didn't see it, so just one link today. More tomorrow, I expect.

  • Firefox 3.5 Preview - If you haven't been seduced away by Safari, here's the latest Firefox, now nearing release candidate status and plenty stable enough for daily browsing.