public
Description: ruby binding to maxmind geoip library
Homepage: http://geoip-city.rubyforge.org
Clone URL: git://github.com/ry/geoip-city.git
added options for database loading

please see the comment above rb_geoip_new for information on how to 
control it.

bumped version
Ryan Dahl (author)
Tue Mar 04 14:42:05 -0800 2008
commit  ea200d82a199528effb8c6d9575dd71b28d59d30
tree    3a2f68c4a7b3a90b189eeccea610e98d4bef44c6
parent  6baf7570aff7140b387b0ed2770b33fa308cc479
0
...
11
12
13
14
 
 
 
 
 
 
 
 
 
 
15
16
17
...
25
26
27
28
29
30
31
32
33
34
35
36
37
 
 
38
39
40
...
11
12
13
 
14
15
16
17
18
19
20
21
22
23
24
25
26
...
34
35
36
 
 
 
 
 
 
 
 
 
 
37
38
39
40
41
0
@@ -11,7 +11,16 @@ mapping information. It is kindly provided free of charge by MaxMind.com.
0
    require 'geoip_city'
0
    db = GeoIPCity::Database.new('/opt/GeoIP/share/GeoIP/GeoLiteCity.dat')
0
    result = db.look_up('24.24.24.24')
0
- puts result.inspect
0
+ p result
0
+ # => {:city=>"Ithaca",
0
+ # :latitude=>42.4277992248535,
0
+ # :longitude=>-76.4981994628906,
0
+ # :country_code3=>"USA",
0
+ # :country_code=>"US",
0
+ # :country_name=>"United States",
0
+ # :dma_code=>555,
0
+ # :area_code=>607,
0
+ # :region=>"NY" }
0
 
0
 = Install
0
 Some variation of the following should work.
0
@@ -25,16 +34,8 @@ Some variation of the following should work.
0
      ./configure --prefix=/opt/GeoIP
0
      make && sudo make install
0
 
0
-2. Now install the geoip Ruby gem (this package!)
0
-
0
- Hint: You need to make sure that gcc can find the GeoIP header and library
0
- files. One way to do this is to ensure that your CPATH and LIBRARY_PATH
0
- enviromental variables include the GeoIP directory. So, if you want, do
0
- this:
0
- export CPATH="/opt/GeoIP/include:$CPATH"
0
- export LIBRARY_PATH="/opt/GeoIP/lib:$LIBRARY_PATH"
0
- sudo gem install geoip_city
0
-
0
+2. Now install the geoip_city gem
0
+ sudo gem install geoip_city -- --with-geoip-dir=/opt/GeoIP
0
 
0
 3. Download the GeoLite City database file in binary format from:
0
    http://www.maxmind.com/app/geolitecity
...
24
25
26
27
 
28
29
30
...
24
25
26
 
27
28
29
30
0
@@ -24,7 +24,7 @@ spec = Gem::Specification.new do |s|
0
   s.name = 'geoip_city'
0
   s.author = 'ry dahl'
0
   s.email = 'ry@tinyclouds.org'
0
- s.version = "0.1.1"
0
+ s.version = "0.2.0"
0
   s.summary = "A Binding to the GeoIP C library"
0
   s.homepage = "http://geoip_city.rubyforge.org"
0
   s.files = FileList['Rakefile', '*.rb', '*.c', 'README*']
...
9
10
11
 
 
 
12
13
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
18
 
 
 
19
20
 
 
21
22
23
...
82
83
84
 
 
 
 
85
86
 
87
88
...
9
10
11
12
13
14
15
 
 
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
 
54
55
56
57
58
59
60
61
62
63
...
122
123
124
125
126
127
128
129
 
130
131
132
0
@@ -9,15 +9,55 @@
0
 #include <GeoIPCity.h>
0
 
0
 static VALUE cDB;
0
+static VALUE rb_geoip_memory;
0
+static VALUE rb_geoip_filesystem;
0
+static VALUE rb_geoip_index;
0
  
0
-/* The argument is the filename of the GeoIPCity.dat file */
0
-VALUE rb_geoip_new(VALUE self, VALUE filename) {
0
+/* The first argument is the filename of the GeoIPCity.dat file
0
+ * load_option = :standard, :index, or :memory. default :memory
0
+ * check_cache = true or false. default false
0
+ *
0
+ * filesystem: read database from filesystem, uses least memory.
0
+ *
0
+ * index: the most frequently accessed index portion of the database,
0
+ * resulting in faster lookups than :filesystem, but less memory usage than
0
+ * :memory.
0
+ *
0
+ * memory: load database into memory, faster performance but uses more
0
+ * memory.
0
+ */
0
+static VALUE rb_geoip_new(int argc, VALUE *argv, VALUE self)
0
+{
0
   GeoIP *gi;
0
   VALUE database = Qnil;
0
+ VALUE filename, load_option = Qnil, check_cache = Qnil;
0
+ int flag;
0
+
0
+ rb_scan_args(argc, argv, "12", &filename, &load_option, &check_cache);
0
+ if(NIL_P(load_option))
0
+ load_option = rb_geoip_memory;
0
+ if(NIL_P(check_cache))
0
+ check_cache = Qfalse;
0
+ Check_Type(load_option, T_SYMBOL);
0
+
0
+ if(load_option == rb_geoip_memory) {
0
+ flag = GEOIP_MEMORY_CACHE;
0
+ } else if(load_option == rb_geoip_filesystem) {
0
+ flag = GEOIP_STANDARD;
0
+ } else if(load_option == rb_geoip_index) {
0
+ flag = GEOIP_INDEX_CACHE;
0
+ } else {
0
+ rb_raise(rb_eTypeError, "the second option must be :memory, :filesystem, or :index");
0
+ return Qnil;
0
+ }
0
 
0
- if(gi = GeoIP_open(STR2CSTR(filename), GEOIP_MEMORY_CACHE)) {
0
+ if(RTEST(check_cache)) flag |= GEOIP_CHECK_CACHE;
0
+
0
+ if(gi = GeoIP_open(STR2CSTR(filename), flag)) {
0
     database = Data_Wrap_Struct(cDB, 0, GeoIP_delete, gi);
0
     rb_obj_call_init(database, 0, 0);
0
+ } else {
0
+ rb_sys_fail("Problem opening database");
0
   }
0
   return database;
0
 }
0
@@ -82,7 +122,11 @@ void Init_geoip_city ()
0
 {
0
   VALUE mGeoIP = rb_define_module("GeoIPCity");
0
 
0
+ rb_geoip_memory = ID2SYM(rb_intern("memory"));
0
+ rb_geoip_filesystem = ID2SYM(rb_intern("filesystem"));
0
+ rb_geoip_index = ID2SYM(rb_intern("index"));
0
+
0
   cDB = rb_define_class_under(mGeoIP, "Database", rb_cObject);
0
- rb_define_singleton_method(cDB, "new", rb_geoip_new, 1);
0
+ rb_define_singleton_method(cDB, "new", rb_geoip_new, -1);
0
   rb_define_method(cDB, "look_up", rb_geoip_look_up, 1);
0
 }
...
1
 
2
 
 
3
4
5
...
9
10
11
12
 
13
14
15
...
17
18
19
 
20
21
22
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
25
...
1
2
3
4
5
6
7
8
...
12
13
14
 
15
16
17
18
...
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
0
@@ -1,5 +1,8 @@
0
 require 'test/unit'
0
+require 'rubygems'
0
 require 'geoip_city'
0
+require 'ruby-debug'
0
+Debugger.start
0
 
0
 class GeoIPTest < Test::Unit::TestCase
0
   
0
@@ -9,7 +12,7 @@ class GeoIPTest < Test::Unit::TestCase
0
   end
0
   
0
   
0
- def test_construction
0
+ def test_construction_default
0
     db = GeoIPCity::Database.new(@dbfile)
0
     
0
     assert_raises TypeError do
0
@@ -17,9 +20,40 @@ class GeoIPTest < Test::Unit::TestCase
0
     end
0
     
0
     h = db.look_up('24.24.24.24')
0
+ #debugger
0
     assert_kind_of Hash, h
0
     assert_equal 'Ithaca', h[:city]
0
     assert_equal 'United States', h[:country_name]
0
   end
0
+
0
+ def test_construction_index
0
+ db = GeoIPCity::Database.new(@dbfile, :index)
0
+ h = db.look_up('24.24.24.24')
0
+ assert_equal 'Ithaca', h[:city]
0
+ end
0
+
0
+ def test_construction_filesystem
0
+ db = GeoIPCity::Database.new(@dbfile, :filesystem)
0
+ h = db.look_up('24.24.24.24')
0
+ assert_equal 'Ithaca', h[:city]
0
+ end
0
+
0
+ def test_construction_memory
0
+ db = GeoIPCity::Database.new(@dbfile, :memory)
0
+ h = db.look_up('24.24.24.24')
0
+ assert_equal 'Ithaca', h[:city]
0
+ end
0
+
0
+ def test_construction_filesystem_check
0
+ db = GeoIPCity::Database.new(@dbfile, :filesystem, true)
0
+ h = db.look_up('24.24.24.24')
0
+ assert_equal 'Ithaca', h[:city]
0
+ end
0
+
0
+ def test_bad_db_file
0
+ assert_raises Errno::ENOENT do
0
+ GeoIPCity::Database.new('/blah')
0
+ end
0
+ end
0
   
0
 end

Comments

    No one has commented yet.