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 software developer: most of my time in recent years has been spent writing Rails, though I've dabbled in many other things and like most people who have been writing code for decades I can learn new stuff as needed.

As of July 2016, I'm looking for my next job. I'm not able to relocate, so unless you're in the Evansville area, I'd need a completely remote gig. I have lots of experience working remote. Prefer full-time but I wouldn't be averse to an interesting contact gig. Drop me a comment if you've got something or email MikeG1 [at] larkfarm.com.

Navigation

A Fresh Cup

Notes on Rails and other development

Monday
Jun152009

Double Shot #474

Well, I don't know about you, but clearly some people had busy weekends.

Friday
Jun122009

Double Shot #473

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

Thursday
Jun112009

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.
Wednesday
Jun102009

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]
end

end
[/sourcecode]

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

Double Shot #471

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