geoip_city 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (6) hide show
  1. data/README +25 -2
  2. data/Rakefile +1 -1
  3. data/extconf.rb +2 -0
  4. data/geoip_city.c +46 -4
  5. data/test.rb +30 -2
  6. metadata +41 -34
data/README CHANGED
@@ -11,7 +11,31 @@ mapping information. It is kindly provided free of charge by MaxMind.com.
11
11
  require 'geoip_city'
12
12
  db = GeoIPCity::Database.new('/opt/GeoIP/share/GeoIP/GeoLiteCity.dat')
13
13
  result = db.look_up('24.24.24.24')
14
- puts result.inspect
14
+ p result
15
+ # => {:city=>"Ithaca",
16
+ # :latitude=>42.4277992248535,
17
+ # :longitude=>-76.4981994628906,
18
+ # :country_code3=>"USA",
19
+ # :country_code=>"US",
20
+ # :country_name=>"United States",
21
+ # :dma_code=>555,
22
+ # :area_code=>607,
23
+ # :region=>"NY" }
24
+
25
+ There are two optional arguments to database initializer.
26
+ The first argument is the filename of the GeoIPCity.dat file
27
+ The second argument (optional) is to specify how GeoIP should
28
+ keep the database in memory. There are three possibilities
29
+ * :filesystem -- read database from filesystem, uses least memory.
30
+ * :index -- the most frequently accessed index portion of the database, resulting in faster lookups than :filesystem, but less memory usage than :memory.
31
+ * :memory -- load database into memory, faster performance but uses more memory.
32
+ (default)
33
+
34
+ The third argument is boolean and decides whether the system should reload the database if changes are made to the dat file. (You probably don't need this. Default: false.)
35
+
36
+ For example
37
+
38
+ GeoIPCity::Database.new(dbfile, :filesystem, true)
15
39
 
16
40
  = Install
17
41
  Some variation of the following should work.
@@ -35,7 +59,6 @@ Some variation of the following should work.
35
59
  I put this file in
36
60
  /opt/GeoIP/share/GeoIP/GeoLiteCity.dat
37
61
 
38
-
39
62
  4. Use it!
40
63
 
41
64
  = License
data/Rakefile CHANGED
@@ -24,7 +24,7 @@ spec = Gem::Specification.new do |s|
24
24
  s.name = 'geoip_city'
25
25
  s.author = 'ry dahl'
26
26
  s.email = 'ry@tinyclouds.org'
27
- s.version = "0.1.2"
27
+ s.version = "0.2.0"
28
28
  s.summary = "A Binding to the GeoIP C library"
29
29
  s.homepage = "http://geoip_city.rubyforge.org"
30
30
  s.files = FileList['Rakefile', '*.rb', '*.c', 'README*']
data/extconf.rb CHANGED
@@ -4,4 +4,6 @@ dir_config("geoip")
4
4
 
5
5
  if have_library('GeoIP', 'GeoIP_record_by_ipnum') and have_header('GeoIPCity.h')
6
6
  create_makefile('geoip_city')
7
+ else
8
+ abort("you must have geoip c library installed!")
7
9
  end
@@ -9,13 +9,51 @@
9
9
  #include <GeoIPCity.h>
10
10
 
11
11
  static VALUE cDB;
12
+ static VALUE rb_geoip_memory;
13
+ static VALUE rb_geoip_filesystem;
14
+ static VALUE rb_geoip_index;
12
15
 
13
- /* The argument is the filename of the GeoIPCity.dat file */
14
- VALUE rb_geoip_new(VALUE self, VALUE filename) {
16
+ /* The first argument is the filename of the GeoIPCity.dat file
17
+ * load_option = :standard, :index, or :memory. default :memory
18
+ * check_cache = true or false. default false
19
+ *
20
+ * filesystem: read database from filesystem, uses least memory.
21
+ *
22
+ * index: the most frequently accessed index portion of the database,
23
+ * resulting in faster lookups than :filesystem, but less memory usage than
24
+ * :memory.
25
+ *
26
+ * memory: load database into memory, faster performance but uses more
27
+ * memory.
28
+ */
29
+ static VALUE rb_geoip_new(int argc, VALUE *argv, VALUE self)
30
+ {
15
31
  GeoIP *gi;
16
32
  VALUE database = Qnil;
33
+ VALUE filename, load_option = Qnil, check_cache = Qnil;
34
+ int flag;
35
+
36
+ rb_scan_args(argc, argv, "12", &filename, &load_option, &check_cache);
37
+ if(NIL_P(load_option))
38
+ load_option = rb_geoip_memory;
39
+ if(NIL_P(check_cache))
40
+ check_cache = Qfalse;
41
+ Check_Type(load_option, T_SYMBOL);
42
+
43
+ if(load_option == rb_geoip_memory) {
44
+ flag = GEOIP_MEMORY_CACHE;
45
+ } else if(load_option == rb_geoip_filesystem) {
46
+ flag = GEOIP_STANDARD;
47
+ } else if(load_option == rb_geoip_index) {
48
+ flag = GEOIP_INDEX_CACHE;
49
+ } else {
50
+ rb_raise(rb_eTypeError, "the second option must be :memory, :filesystem, or :index");
51
+ return Qnil;
52
+ }
17
53
 
18
- if(gi = GeoIP_open(STR2CSTR(filename), GEOIP_MEMORY_CACHE)) {
54
+ if(RTEST(check_cache)) flag |= GEOIP_CHECK_CACHE;
55
+
56
+ if(gi = GeoIP_open(STR2CSTR(filename), flag)) {
19
57
  database = Data_Wrap_Struct(cDB, 0, GeoIP_delete, gi);
20
58
  rb_obj_call_init(database, 0, 0);
21
59
  } else {
@@ -84,7 +122,11 @@ void Init_geoip_city ()
84
122
  {
85
123
  VALUE mGeoIP = rb_define_module("GeoIPCity");
86
124
 
125
+ rb_geoip_memory = ID2SYM(rb_intern("memory"));
126
+ rb_geoip_filesystem = ID2SYM(rb_intern("filesystem"));
127
+ rb_geoip_index = ID2SYM(rb_intern("index"));
128
+
87
129
  cDB = rb_define_class_under(mGeoIP, "Database", rb_cObject);
88
- rb_define_singleton_method(cDB, "new", rb_geoip_new, 1);
130
+ rb_define_singleton_method(cDB, "new", rb_geoip_new, -1);
89
131
  rb_define_method(cDB, "look_up", rb_geoip_look_up, 1);
90
132
  }
data/test.rb CHANGED
@@ -1,5 +1,8 @@
1
1
  require 'test/unit'
2
- require 'geoip_city'
2
+ require File.dirname(__FILE__) + '/geoip_city'
3
+ require 'rubygems'
4
+ require 'ruby-debug'
5
+ Debugger.start
3
6
 
4
7
  class GeoIPTest < Test::Unit::TestCase
5
8
 
@@ -9,7 +12,7 @@ class GeoIPTest < Test::Unit::TestCase
9
12
  end
10
13
 
11
14
 
12
- def test_construction
15
+ def test_construction_default
13
16
  db = GeoIPCity::Database.new(@dbfile)
14
17
 
15
18
  assert_raises TypeError do
@@ -17,11 +20,36 @@ class GeoIPTest < Test::Unit::TestCase
17
20
  end
18
21
 
19
22
  h = db.look_up('24.24.24.24')
23
+ #debugger
20
24
  assert_kind_of Hash, h
21
25
  assert_equal 'Ithaca', h[:city]
22
26
  assert_equal 'United States', h[:country_name]
23
27
  end
24
28
 
29
+ def test_construction_index
30
+ db = GeoIPCity::Database.new(@dbfile, :index)
31
+ h = db.look_up('24.24.24.24')
32
+ assert_equal 'Ithaca', h[:city]
33
+ end
34
+
35
+ def test_construction_filesystem
36
+ db = GeoIPCity::Database.new(@dbfile, :filesystem)
37
+ h = db.look_up('24.24.24.24')
38
+ assert_equal 'Ithaca', h[:city]
39
+ end
40
+
41
+ def test_construction_memory
42
+ db = GeoIPCity::Database.new(@dbfile, :memory)
43
+ h = db.look_up('24.24.24.24')
44
+ assert_equal 'Ithaca', h[:city]
45
+ end
46
+
47
+ def test_construction_filesystem_check
48
+ db = GeoIPCity::Database.new(@dbfile, :filesystem, true)
49
+ h = db.look_up('24.24.24.24')
50
+ assert_equal 'Ithaca', h[:city]
51
+ end
52
+
25
53
  def test_bad_db_file
26
54
  assert_raises Errno::ENOENT do
27
55
  GeoIPCity::Database.new('/blah')
metadata CHANGED
@@ -1,50 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.2
3
- specification_version: 1
4
2
  name: geoip_city
5
3
  version: !ruby/object:Gem::Version
6
- version: 0.1.2
7
- date: 2007-12-09 00:00:00 -08:00
8
- summary: A Binding to the GeoIP C library
9
- require_paths:
10
- - .
11
- email: ry@tinyclouds.org
12
- homepage: http://geoip_city.rubyforge.org
13
- rubyforge_project:
14
- description:
15
- autorequire:
16
- default_executable:
17
- bindir: bin
18
- has_rdoc: false
19
- required_ruby_version: !ruby/object:Gem::Version::Requirement
20
- requirements:
21
- - - ">"
22
- - !ruby/object:Gem::Version
23
- version: 0.0.0
24
- version:
4
+ version: 0.2.0
25
5
  platform: ruby
26
- signing_key:
27
- cert_chain:
28
- post_install_message:
29
6
  authors:
30
7
  - ry dahl
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-03-05 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: ry@tinyclouds.org
18
+ executables: []
19
+
20
+ extensions:
21
+ - extconf.rb
22
+ extra_rdoc_files: []
23
+
31
24
  files:
32
25
  - Rakefile
33
26
  - extconf.rb
34
27
  - test.rb
35
28
  - geoip_city.c
36
29
  - README
37
- test_files:
38
- - test.rb
30
+ has_rdoc: false
31
+ homepage: http://geoip_city.rubyforge.org
32
+ post_install_message:
39
33
  rdoc_options: []
40
34
 
41
- extra_rdoc_files: []
42
-
43
- executables: []
44
-
45
- extensions:
46
- - extconf.rb
35
+ require_paths:
36
+ - .
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ version:
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
47
49
  requirements: []
48
50
 
49
- dependencies: []
50
-
51
+ rubyforge_project:
52
+ rubygems_version: 1.0.1
53
+ signing_key:
54
+ specification_version: 2
55
+ summary: A Binding to the GeoIP C library
56
+ test_files:
57
+ - test.rb