diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..538c8c5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.DS_Store +*~ diff --git a/trunk/Rakefile b/trunk/Rakefile index 52caead..16bf2c6 100644 --- a/trunk/Rakefile +++ b/trunk/Rakefile @@ -1,6 +1,6 @@ require 'rake/testtask' require 'rake/rdoctask' -require 'meta_project' +#require 'meta_project' require 'rake/contrib/rubyforgepublisher' require 'fileutils' diff --git a/trunk/ext/geoip.c b/trunk/ext/geoip.c index fbf8867..c163050 100644 --- a/trunk/ext/geoip.c +++ b/trunk/ext/geoip.c @@ -16,7 +16,7 @@ geoip_alloc(VALUE klass) { GeoIPContainer *container; container = ALLOC(GeoIPContainer); - + container->geoip = NULL; return Data_Wrap_Struct(klass, 0, geoip_free, container); } @@ -31,6 +31,26 @@ geoip_free(GeoIPContainer *container) { return; } +/* Net::GeoIP.open_type + * + */ + +VALUE +geoip_open_type(VALUE self, VALUE type, VALUE flags) { + + GeoIPContainer *container; + + Data_Get_Struct(self, GeoIPContainer, container); + + container->geoip = GeoIP_open_type(type, flags); + + if (container->geoip == NULL) { + rb_raise(cGeoIPError, "Invalid Database"); + } + + return self; +} + /* call-seq: * Net::GeoIP.new(database) * @@ -41,12 +61,18 @@ VALUE geoip_initialize(VALUE self, VALUE database) { GeoIPContainer *container; - Check_Type(database, T_STRING); - Data_Get_Struct(self, GeoIPContainer, container); - container->geoip = GeoIP_open(RSTRING(database)->ptr, GEOIP_INDEX_CACHE); + if ( FIXNUM_P(database) ){ + + container->geoip = GeoIP_open_type(FIX2INT(database), GEOIP_STANDARD); + + }else{ + + Check_Type(database, T_STRING); + container->geoip = GeoIP_open(RSTRING(database)->ptr, GEOIP_INDEX_CACHE); + } if (container->geoip == NULL) { rb_raise(cGeoIPError, "Invalid Database"); } @@ -75,6 +101,184 @@ geoip_index(VALUE self, VALUE index) { return record; } +// +// country_code_by_addr +// + + +VALUE +geoip_country_code_by_addr( VALUE self, VALUE addr ){ + + const char * p; + + GeoIPContainer *container; + + Data_Get_Struct(self, GeoIPContainer, container); + + p = GeoIP_country_code_by_addr (container->geoip, + RSTRING(addr)->ptr); + + return p == NULL ? Qnil : rb_str_new2(p); +} + + +// +// country_code_by_name +// + + +VALUE +geoip_country_code_by_name( VALUE self, VALUE hostname ){ + + const char * p; + + GeoIPContainer *container; + + Data_Get_Struct(self, GeoIPContainer, container); + + p = GeoIP_country_code_by_name (container->geoip, + RSTRING(hostname)->ptr); + + return p == NULL ? Qnil : rb_str_new2(p); +} + +// +// country_code3_by_addr +// + + +VALUE +geoip_country_code3_by_addr( VALUE self, VALUE addr ){ + + const char * p; + + GeoIPContainer *container; + + Data_Get_Struct(self, GeoIPContainer, container); + + p = GeoIP_country_code3_by_addr (container->geoip, + RSTRING(addr)->ptr); + + return p == NULL ? Qnil : rb_str_new2(p); +} + + +// +// country_code3_by_name +// + + +VALUE +geoip_country_code3_by_name( VALUE self, VALUE hostname ){ + + const char * p; + + GeoIPContainer *container; + + Data_Get_Struct(self, GeoIPContainer, container); + + p = GeoIP_country_code3_by_name (container->geoip, + RSTRING(hostname)->ptr); + + return p == NULL ? Qnil : rb_str_new2(p); +} + +// +// country_name_by_addr +// + + +VALUE +geoip_country_name_by_addr( VALUE self, VALUE addr ){ + + const char * p; + + GeoIPContainer *container; + + Data_Get_Struct(self, GeoIPContainer, container); + + p = GeoIP_country_name_by_addr (container->geoip, + RSTRING(addr)->ptr); + return p == NULL ? Qnil : rb_str_new2(p); +} + + +// +// country_name_by_name +// + + +VALUE +geoip_country_name_by_name( VALUE self, VALUE hostname ){ + + const char * p; + + GeoIPContainer *container; + + Data_Get_Struct(self, GeoIPContainer, container); + + p = GeoIP_country_name_by_name (container->geoip, + RSTRING(hostname)->ptr); + + return p == NULL ? Qnil : rb_str_new2(p); +} + +// +// +// + +VALUE +geoip_name_by_name( VALUE self, VALUE host ){ + + char * p; + + GeoIPContainer *container; + + Data_Get_Struct(self, GeoIPContainer, container); + + + p = GeoIP_name_by_name (container->geoip, + RSTRING(host)->ptr); + + return p == NULL ? Qnil : rb_str_new2(p); +} + +// +// +// + + +VALUE +geoip_name_by_addr( VALUE self, VALUE addr ){ + + char * p; + + GeoIPContainer *container; + + Data_Get_Struct(self, GeoIPContainer, container); + + p = GeoIP_name_by_addr (container->geoip, RSTRING(addr)->ptr); + + return p == NULL ? Qnil : rb_str_new2(p); +} + + +// +// database_info +// +VALUE +geoip_database_info(VALUE self) { + + char * p; + + GeoIPContainer *container; + + Data_Get_Struct(self, GeoIPContainer, container); + + p = GeoIP_database_info(container->geoip); + + return p == NULL ? Qnil : rb_str_new2(p); +} // // Extension initialization. @@ -87,10 +291,21 @@ Init_geoip() { mNet = rb_define_module("Net"); cGeoIP = rb_define_class_under(mNet, "GeoIP", rb_cObject); + rb_define_alloc_func(cGeoIP, geoip_alloc); rb_define_method(cGeoIP, "initialize", geoip_initialize, 1); rb_define_method(cGeoIP, "[]", geoip_index, 1); - + + rb_define_method(cGeoIP, "database_info", geoip_database_info, 0); + rb_define_method(cGeoIP, "name_by_name", geoip_name_by_name, 1); + rb_define_method(cGeoIP, "name_by_addr", geoip_name_by_addr, 1); + rb_define_method(cGeoIP, "country_name_by_addr", geoip_country_name_by_addr, 1); + rb_define_method(cGeoIP, "country_name_by_name", geoip_country_name_by_name, 1); + rb_define_method(cGeoIP, "country_code_by_name", geoip_country_code_by_name, 1); + rb_define_method(cGeoIP, "country_code3_by_name", geoip_country_code3_by_name, 1); + rb_define_method(cGeoIP, "country_code_by_addr", geoip_country_code_by_addr, 1); + rb_define_method(cGeoIP, "country_code3_by_addr", geoip_country_code3_by_addr, 1); + cGeoIPRecord = rb_define_class_under(cGeoIP, "Record", rb_cObject); rb_define_alloc_func(cGeoIPRecord, geoip_record_alloc); rb_define_method(cGeoIPRecord, "initialize", geoip_record_initialize, 2); @@ -105,6 +320,20 @@ Init_geoip() { rb_define_method(cGeoIPRecord, "dma_code", geoip_record_dma_code, 0); rb_define_method(cGeoIPRecord, "area_code", geoip_record_area_code, 0); + + // define the open types + rb_define_const (cGeoIP, "GEOIP_COUNTRY_EDITION", INT2NUM(GEOIP_COUNTRY_EDITION) ); + rb_define_const (cGeoIP, "GEOIP_REGION_EDITION_REV0", INT2NUM(GEOIP_REGION_EDITION_REV0) ); + rb_define_const (cGeoIP, "GEOIP_CITY_EDITION_REV0", INT2NUM(GEOIP_CITY_EDITION_REV0) ); + rb_define_const (cGeoIP, "GEOIP_ORG_EDITION", INT2NUM(GEOIP_ORG_EDITION) ); + rb_define_const (cGeoIP, "GEOIP_ISP_EDITION", INT2NUM(GEOIP_ISP_EDITION) ); + rb_define_const (cGeoIP, "GEOIP_CITY_EDITION_REV1", INT2NUM(GEOIP_CITY_EDITION_REV1) ); + rb_define_const (cGeoIP, "GEOIP_REGION_EDITION_REV1", INT2NUM(GEOIP_REGION_EDITION_REV1) ); + rb_define_const (cGeoIP, "GEOIP_PROXY_EDITION", INT2NUM(GEOIP_PROXY_EDITION) ); + rb_define_const (cGeoIP, "GEOIP_ASNUM_EDITION", INT2NUM(GEOIP_ASNUM_EDITION) ); + rb_define_const (cGeoIP, "GEOIP_NETSPEED_EDITION", INT2NUM(GEOIP_NETSPEED_EDITION) ); + rb_define_const (cGeoIP, "GEOIP_DOMAIN_EDITION", INT2NUM(GEOIP_DOMAIN_EDITION) ); + cGeoIPError = rb_define_class_under(mNet, "GeoIPError", rb_eException); cGeoIPRecordNotFoundError = rb_define_class_under(cGeoIP, "RecordNotFoundError", rb_eException); diff --git a/trunk/ext/geoip.h b/trunk/ext/geoip.h index 12f4160..dd4301a 100644 --- a/trunk/ext/geoip.h +++ b/trunk/ext/geoip.h @@ -31,6 +31,17 @@ extern VALUE geoip_alloc(VALUE klass); extern void geoip_free(GeoIPContainer *p); extern VALUE geoip_initialize(VALUE self, VALUE database); extern VALUE geoip_index(VALUE self, VALUE index); +extern VALUE geoip_database_info(VALUE self); +extern VALUE geoip_open_type(VALUE self, VALUE type, VALUE flags); +extern VALUE geoip_country_code_by_addr( VALUE self, VALUE addr ); +extern VALUE geoip_country_code_by_name( VALUE self, VALUE hostname ); +extern VALUE geoip_country_code3_by_addr( VALUE self, VALUE addr ); +extern VALUE geoip_country_code3_by_name( VALUE self, VALUE hostname ); +extern VALUE geoip_country_name_by_addr( VALUE self, VALUE addr ); +extern VALUE geoip_country_name_by_name( VALUE self, VALUE hostname ); +extern VALUE geoip_name_by_name( VALUE self, VALUE host ); +extern VALUE geoip_name_by_addr( VALUE self, VALUE addr ); + /* Net::GeoIP::Record */ extern VALUE geoip_record_alloc(VALUE klass); diff --git a/trunk/ext/record.c b/trunk/ext/record.c index 05963ab..79bbcd1 100644 --- a/trunk/ext/record.c +++ b/trunk/ext/record.c @@ -10,6 +10,7 @@ geoip_record_alloc(VALUE klass) { GeoIPRecordContainer *container; container = ALLOC(GeoIPRecordContainer); + container->record = NULL; return Data_Wrap_Struct(klass, 0, geoip_record_free, container); } diff --git a/trunk/test/geoip_database_info.rb b/trunk/test/geoip_database_info.rb new file mode 100644 index 0000000..df9de19 --- /dev/null +++ b/trunk/test/geoip_database_info.rb @@ -0,0 +1,22 @@ +require 'test/unit' +require 'geoip_test_helpers' + +require 'net/geoip' + + +class GeoIPDatabaseInfoTest < Test::Unit::TestCase + include GeoIPTestHelpers + + def setup + @geoip = Net::GeoIP.new(city_database_filename) + end + + def test_should_bitch_when_indexed_with_non_string + assert_raises(TypeError) { @geoip[0] } + end + + def test_should_get_geoip_database_info + assert_equal 'US', @geoip.database_info + end + +end diff --git a/trunk/test/geoip_init_test.rb b/trunk/test/geoip_init_test.rb index 63b9ef9..9350af9 100644 --- a/trunk/test/geoip_init_test.rb +++ b/trunk/test/geoip_init_test.rb @@ -19,9 +19,9 @@ class GeoIPInitTest < Test::Unit::TestCase assert_not_nil Net::GeoIP.new(city_database_filename) end - def test_should_raise_exceptoin_if_database_filename_is_not_a_string - assert_raises(TypeError) { Net::GeoIP.new(0) } - end +# def test_should_raise_exception_if_database_filename_is_not_a_string +# assert_raises(TypeError) { Net::GeoIP.new(0) } +# end def test_should_raise_exception_if_invalid_database_specified assert_raises(Net::GeoIPError) { Net::GeoIP.new(File.dirname(__FILE__) + '/../Rakefile') } diff --git a/trunk/test/geoip_lookup_test.rb b/trunk/test/geoip_lookup_test.rb new file mode 100644 index 0000000..154303d --- /dev/null +++ b/trunk/test/geoip_lookup_test.rb @@ -0,0 +1,29 @@ +require 'test/unit' +require 'geoip_test_helpers' + +require 'net/geoip' + + +class GeoIPLookupTest < Test::Unit::TestCase + include GeoIPTestHelpers + + def setup + @geoip = Net::GeoIP.new(Net::GeoIP::GEOIP_COUNTRY_EDITION) + end + +# def test_should_bitch_when_indexed_with_non_string +# assert_raises(TypeError) { @geoip[0] } +# end + + def test_should_get_jp +# assert_equal 'JP', @geoip.country_code_by_addr('zxy.de'); + assert_equal 'JP', @geoip.country_code_by_addr('203.174.65.12'); + end + def test_should_get_fr + assert_equal 'FR', @geoip.country_code_by_addr('212.208.74.140'); + end + def test_should_get_de + assert_equal 'DE', @geoip.country_code_by_addr('134.102.101.18'); + end + +end diff --git a/trunk/test/geoip_open_type_test.rb b/trunk/test/geoip_open_type_test.rb new file mode 100644 index 0000000..ea8b65d --- /dev/null +++ b/trunk/test/geoip_open_type_test.rb @@ -0,0 +1,19 @@ +require 'test/unit' +require 'geoip_test_helpers' + +require 'net/geoip' + + +class GeoIPOpenTypeTest < Test::Unit::TestCase + include GeoIPTestHelpers + + def setup + @geoip = Net::GeoIP.new(5); + end + + + def test_should_get_geoip_record_when_indexing_geoip_with_string + assert_equal 'Hosteurope GmbH', @geoip.name_by_name('www.2bz.de'); + end + +end diff --git a/trunk/test/geoip_record_lookup_test.rb b/trunk/test/geoip_record_lookup_test.rb index a5d924e..6a94097 100644 --- a/trunk/test/geoip_record_lookup_test.rb +++ b/trunk/test/geoip_record_lookup_test.rb @@ -18,7 +18,7 @@ class GeoIPRecordLookupTest < Test::Unit::TestCase # country_code def test_should_find_US_for_yahoo_country_code - assert_equal 'US', @geoip['yahoo.com'].country_code + assert_equal 'US', @geoip['216.109.112.135'].country_code end def test_should_find_CA_for_bravenet_country_code @@ -32,7 +32,7 @@ class GeoIPRecordLookupTest < Test::Unit::TestCase # country_code3 def test_should_find_USA_for_yahoo_country_code3 - assert_equal 'USA', @geoip['yahoo.com'].country_code3 + assert_equal 'USA', @geoip['216.109.112.135'].country_code3 end def test_should_find_CAN_for_yahoo_country_code3 @@ -46,7 +46,7 @@ class GeoIPRecordLookupTest < Test::Unit::TestCase # country_name def test_should_find_United_States_for_yahoo_country_name - assert_equal 'United States', @geoip['yahoo.com'].country_name + assert_equal 'United States', @geoip['216.109.112.135'].country_name end def test_should_find_Canada_for_bravenet_country_name @@ -60,7 +60,7 @@ class GeoIPRecordLookupTest < Test::Unit::TestCase # region def test_should_find_CA_for_yahoo_region - assert_equal 'CA', @geoip['yahoo.com'].region + assert_equal 'CA', @geoip['216.109.112.135'].region end def test_should_find_BC_for_bravenet_region @@ -74,7 +74,7 @@ class GeoIPRecordLookupTest < Test::Unit::TestCase # city def test_should_find_Sunnyvale_for_yahoo_city - assert_equal 'Sunnyvale', @geoip['yahoo.com'].city + assert_equal 'Sunnyvale', @geoip['216.109.112.135'].city end def test_should_find_Parksville_for_bravenet_city @@ -88,7 +88,7 @@ class GeoIPRecordLookupTest < Test::Unit::TestCase # postal_code def test_should_find_94089_for_yahoo_postal_code - assert_equal '94089', @geoip['yahoo.com'].postal_code + assert_equal '94089', @geoip['216.109.112.135'].postal_code end def test_should_find_v9p2h5_for_bravenet_postal_code @@ -102,7 +102,7 @@ class GeoIPRecordLookupTest < Test::Unit::TestCase # latitude def test_should_find_correct_latitude_for_yahoo - assert_in_delta 37.1893997192383, @geoip['yahoo.com'].latitude, 0.0001 + assert_in_delta 37.424900, @geoip['216.109.112.135'].latitude, 0.0001 end def test_should_find_correct_latitude_for_bravenet @@ -116,7 +116,7 @@ class GeoIPRecordLookupTest < Test::Unit::TestCase # longitude def test_should_find_correct_longitude_for_yahoo - assert_in_delta -121.705299377441, @geoip['yahoo.com'].longitude, 0.0001 + assert_in_delta -122.007401, @geoip['216.109.112.135'].longitude, 0.0001 end def test_should_find_correct_longitude_for_bravenet @@ -130,7 +130,7 @@ class GeoIPRecordLookupTest < Test::Unit::TestCase # dma_code def test_should_find_correct_dma_code_for_yahoo - assert_equal 807, @geoip['yahoo.com'].dma_code + assert_equal 807, @geoip['216.109.112.135'].dma_code end def test_should_find_correct_dma_code_for_bravenet @@ -144,7 +144,7 @@ class GeoIPRecordLookupTest < Test::Unit::TestCase # area_code def test_should_find_correct_area_code_for_yahoo - assert_equal 408, @geoip['yahoo.com'].area_code + assert_equal 408, @geoip['216.109.112.135'].area_code end def test_should_find_correct_area_code_for_bravenet diff --git a/trunk/test/geoip_test_helpers.rb b/trunk/test/geoip_test_helpers.rb index dfbeb81..559a415 100644 --- a/trunk/test/geoip_test_helpers.rb +++ b/trunk/test/geoip_test_helpers.rb @@ -3,6 +3,7 @@ module GeoIPTestHelpers private def city_database_filename File.dirname(__FILE__) + '/../data/GeoIPCity-531.dat' +# '/usr/local/share/GeoIP//GeoIPCity.dat' end -end \ No newline at end of file +end