diff --git a/.gitignore b/.gitignore index 5bf7660..16b9e5f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ -pkg/ -doc/ +/.bundle +/pkg +/doc mkmf.log Makefile conftest.dSYM/ @@ -7,3 +8,4 @@ geoip.bundle geoip.so geoip.o Gemfile.lock +/tmp diff --git a/Rakefile b/Rakefile index a56d68e..207152c 100644 --- a/Rakefile +++ b/Rakefile @@ -3,23 +3,39 @@ require 'bundler/gem_tasks' require 'rake/clean' require 'rake/testtask' require 'rdoc/task' +require "rake/extensiontask" -task :default => [:compile, :test] +task :default => [:test] -CLEAN.add "geoip.{o,bundle,so,obj,pdb,lib,def,exp}" -CLOBBER.add ['Makefile', 'mkmf.log','doc'] +CLOBBER.add 'doc', 'data' -Rake::RDocTask.new do |rdoc| - rdoc.rdoc_files.add ['README.md', 'geoip.c'] +RDoc::Task.new do |rdoc| rdoc.main = "README.md" # page to start on - rdoc.rdoc_dir = 'doc/' # rdoc output folder + rdoc.rdoc_files.add ["README.md", "ext/geoip/geoip.c"] + rdoc.rdoc_dir = 'doc' # rdoc output folder end Rake::TestTask.new do |t| - t.test_files = ['test.rb'] t.verbose = true end -desc 'compile the extension' -task(:compile => 'Makefile') { sh 'make' } -file('Makefile' => "geoip.c") { ruby 'extconf.rb' } +spec = Gem::Specification.load "geoip-c.gemspec" + +Rake::ExtensionTask.new("geoip", spec) do |ext| +end + +directory "data" + +file "data/GeoLiteCity.dat" => ["data"] do |f| + url = "http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz" + + sh "curl #{url} -o data/GeoLiteCity.dat.gz" + sh "gunzip data/GeoLiteCity.dat.gz" + touch f.name +end + +task :database => ["data/GeoLiteCity.dat"] do + ENV['CITY'] = File.expand_path("data/GeoLiteCity.dat") +end + +task :test => [:compile, :database] diff --git a/extconf.rb b/ext/geoip/extconf.rb similarity index 100% rename from extconf.rb rename to ext/geoip/extconf.rb diff --git a/geoip.c b/ext/geoip/geoip.c similarity index 99% rename from geoip.c rename to ext/geoip/geoip.c index f218101..3ae8591 100644 --- a/geoip.c +++ b/ext/geoip/geoip.c @@ -137,6 +137,7 @@ static VALUE generic_single_value_lookup_response(char *key, char *value) VALUE rb_city_record_to_hash(GeoIPRecord *record) { VALUE hash = rb_hash_new(); + char *region_name; if(record->country_code) rb_hash_sset(hash, "country_code", encode_to_utf8_and_return_rb_str(record->country_code)); @@ -147,10 +148,9 @@ VALUE rb_city_record_to_hash(GeoIPRecord *record) if(record->region) { rb_hash_sset(hash, "region", encode_to_utf8_and_return_rb_str(record->region)); - char *region_name = GeoIP_region_name_by_code(record->country_code, record->region); - if (region_name) { + region_name = GeoIP_region_name_by_code(record->country_code, record->region); + if (region_name) rb_hash_sset(hash, "region_name", encode_to_utf8_and_return_rb_str(region_name)); - } } if(record->city) rb_hash_sset(hash, "city", encode_to_utf8_and_return_rb_str(record->city)); diff --git a/geoip-c.gemspec b/geoip-c.gemspec index f4aed73..85f1485 100644 --- a/geoip-c.gemspec +++ b/geoip-c.gemspec @@ -1,3 +1,5 @@ +# -*- encoding: utf-8 -*- + Gem::Specification.new do |s| s.name = 'geoip-c' s.version = "0.9.0" @@ -11,11 +13,11 @@ Gem::Specification.new do |s| s.homepage = "http://github.com/mtodd/geoip" s.files = `git ls-files`.split("\n") - s.test_files = ['test.rb'] - s.extensions = ['extconf.rb'] - s.require_path = '.' + s.test_files = ['test/test_geoip.rb'] + s.extensions = ["ext/geoip/extconf.rb"] - s.add_development_dependency 'minitest', '~>5.0' - s.add_development_dependency 'rake', '~>10.0' - s.add_development_dependency 'rdoc', '~>4.0' + s.add_development_dependency 'minitest', '~> 5.0' + s.add_development_dependency 'rake', '~> 10.0' + s.add_development_dependency 'rdoc', '~> 4.0' + s.add_development_dependency "rake-compiler", "~> 0.9.1" end diff --git a/test.rb b/test/test_geoip.rb similarity index 96% rename from test.rb rename to test/test_geoip.rb index 49cc946..259a0c1 100644 --- a/test.rb +++ b/test/test_geoip.rb @@ -1,8 +1,9 @@ # encoding: utf-8 + require 'rubygems' gem 'minitest' require 'minitest/autorun' -require File.dirname(__FILE__) + '/geoip' +require 'geoip' CITY_DB = ENV.fetch("CITY", '/usr/local/GeoIP/share/GeoIP/GeoLiteCity.dat') ORG_DB = ENV.fetch("ORG", '/usr/local/GeoIP/share/GeoIP/GeoIPOrg.dat') @@ -124,6 +125,10 @@ def test_empty_region_name_does_not_crash assert_look_up(db, '119.236.232.169', :region_name, nil) end + def test_hong_kong_segfault + db = GeoIP::City.new(@dbfile, :filesystem, true) + assert_look_up(db, "61.93.14.4", :country_name, "Hong Kong") + end end class GeoIPOrgTest < Minitest::Test @@ -170,5 +175,4 @@ def test_bad_db_file GeoIP::Organization.new('/supposed-to-fail') end end - end