[Pharo-project] IPAddressCountry & Iso3166CountryLongitudeLatitude

Sven Van Caekenberghe sven at beta9.be
Mon Apr 4 15:21:09 CEST 2011


I want to share a couple of classes.

Did you ever want to add this trick to your website where you do something based on the country your visitors are from ? Now you can do so in Smalltalk !

T3EasyUtils getIpAddressCountry atAddress: '81.83.7.35'

	#BE

T3EasyUtils getIso3166CountryLongitudeLatitude at: #BE

	4.0 at 50.8333
	
#('www.beta9.be' 'www.inria.fr' 'nic.st' 'www.world.st' 'www.pharo-project.org')
	collect: [ :each | T3EasyUtils getIpAddressCountry atAddress: (NetNameResolver addressForName: each) ]

	#(#BE #FR #SE #US #CH)

(T3EasyUtils is holding unique, loaded instances of IPAddressCountry & Iso3166CountryLongitudeLatitude).

As this is part of one of our commercial applications, you can even access it over the internet as a JSON REST call:

$ curl http://easy.t3-platform.net/rest/geo-ip?address=81.83.7.35

	{"latitude":50.8333,"address":"81.83.7.35","country":"BE","longitude":4.0}

(Please note that this is not a public service as such, you can try it, but you are not supposed to call this from your own applications.)

BTW, T3 Easy is our new entry level track & trace product, an HTML5 web app (using client side HTML+Javascript+CSS) on top of a REST server written in Pharo Smalltalk. It consists of 4 stateless Pharo Cog VM's running behind an Apache load balancer. The server uses Zinc HTTP Components, both for its server part, as well as for clients talking to other systems behind the scenes. Even the Open Street Map tile serving, which is pushing out many megabytes, is currently going through this server.

Here is a live demo URL in case your are curious:

	http://easy.t3-platform.net?t=livedemo

(You need a fast web browser like Google Chrome, Safari, Opera 10 or Firefox 4.)

The code for IPAddressCountry & Iso3166CountryLongitudeLatitude is on SqueakSource:

	http://www.squeaksource.com/ADayAtTheBeach.html

These are the class comments:

---

IPAddressCountry maps IP addresses to their country code.
I hold a collection of IPAddressRangeCountry objects that I do a binary search on.

I am constructed based on a CSV data file using my class' #readFrom:
which expects lines like 3651886848,3651887103,BE or start,stop,code

You can download a version of this file from http://homepage.mac.com/svc/data/GeoIPCountry.csv.gz

((IPAddressCountry readFrom: 'GeoIPCountry.csv') atAddress: '81.83.7.35') = #BE

Addresses can also be specified as #(81 83 7 35) for #atAddress: or as 1364395811 for #at:

You should cache one instance of me.

Based on data from http://www.maxmind.com/app/geolitecountry

---

Iso3166CountryLongitudeLatitude maps country codes to their average or central longtiude/latitude.

My keys are two letter Iso3166 Symbols representing a country, such as #BE
My values are Points holding coordinates as longtiude at latitude

I am constructed based on a CSV data file using my class' #readFrom:
which expects lines like BE,50.8333,4.0000 or code,latitude,longitude

You can download a version of this file from http://homepage.mac.com/svc/data/CountryLatLong.csv.gz

((Iso3166CountryLongitudeLatitude readFrom: 'CountryLatLong.csv') at: #BE) = 4.0 at 50.8333

You should cache one instance of me.

---

Sven





More information about the Pharo-project mailing list