geoip-c 0.5.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +141 -0
- data/Rakefile +55 -0
- data/extconf.rb +9 -0
- data/geoip.c +212 -0
- data/test.rb +162 -0
- metadata +61 -0
data/README.md
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
Ruby GeoIP Bindings
|
2
|
+
=======================
|
3
|
+
|
4
|
+
What?
|
5
|
+
-----
|
6
|
+
|
7
|
+
This is a library which provides a single function. The function takes as
|
8
|
+
input an IP address and it outputs a hash containing best-guess geographical
|
9
|
+
information (like city, country, latitude, and longitude).
|
10
|
+
|
11
|
+
Actually this is only a Ruby binding to a C library which provides this
|
12
|
+
function. Also, you must download a large binary database with all this
|
13
|
+
mapping information. It is kindly provided free of charge by MaxMind.com.
|
14
|
+
|
15
|
+
Usage
|
16
|
+
|
17
|
+
require 'geoip'
|
18
|
+
db = GeoIP::City.new('/opt/GeoIP/share/GeoIP/GeoLiteCity.dat')
|
19
|
+
result = db.look_up('24.24.24.24')
|
20
|
+
p result
|
21
|
+
# => {:city=>"Ithaca",
|
22
|
+
# :latitude=>42.4277992248535,
|
23
|
+
# :longitude=>-76.4981994628906,
|
24
|
+
# :country_code3=>"USA",
|
25
|
+
# :country_code=>"US",
|
26
|
+
# :country_name=>"United States",
|
27
|
+
# :dma_code=>555,
|
28
|
+
# :area_code=>607,
|
29
|
+
# :region=>"NY" }
|
30
|
+
|
31
|
+
There are arguments to database initializer.
|
32
|
+
|
33
|
+
1. The first argument is the filename of the GeoIPCity.dat file
|
34
|
+
|
35
|
+
2. The second argument (optional) is to specify how GeoIP should
|
36
|
+
keep the database in memory. There are three possibilities
|
37
|
+
|
38
|
+
* `:filesystem` -- Read database from filesystem, uses least memory.
|
39
|
+
|
40
|
+
* `:index` -- The most frequently accessed index portion of the
|
41
|
+
database, resulting in faster lookups than :filesystem, but less
|
42
|
+
memory usage than :memory.
|
43
|
+
|
44
|
+
* `:memory` -- (Default.) Load database into memory, faster performance but uses more memory.
|
45
|
+
|
46
|
+
3. The third argument is boolean and decides whether the system should
|
47
|
+
reload the database if changes are made to the dat file. (You probably
|
48
|
+
don't need this. Default: false.)
|
49
|
+
|
50
|
+
For example
|
51
|
+
|
52
|
+
GeoIP::City.new(dbfile, :filesystem, true)
|
53
|
+
|
54
|
+
Usage for Organization Search
|
55
|
+
-----------------------------
|
56
|
+
|
57
|
+
require 'geoip'
|
58
|
+
db = GeoIP::Organization.new('/opt/GeoIP/share/GeoIP/GeoIPOrg.dat')
|
59
|
+
db.look_up('24.24.24.24')
|
60
|
+
# => {:name=>"Road Runner"}
|
61
|
+
|
62
|
+
The name is the only data available for Organizations.
|
63
|
+
|
64
|
+
Install
|
65
|
+
-------
|
66
|
+
|
67
|
+
Some variation of the following should work.
|
68
|
+
|
69
|
+
1. Install the GeoCity C library. You can get it from
|
70
|
+
[maxmind](http://www.maxmind.com/app/c).
|
71
|
+
For example, I like to install mine in `/opt/GeoIP`, so I do this:
|
72
|
+
|
73
|
+
tar -zxvf GeoIP-1.4.3.tar.gz
|
74
|
+
|
75
|
+
cd GeoIP-1.4.3
|
76
|
+
|
77
|
+
./configure --prefix=/opt/GeoIP
|
78
|
+
|
79
|
+
make && sudo make install
|
80
|
+
|
81
|
+
NOTE: for Intel Mac OS X platforms, try the following:
|
82
|
+
|
83
|
+
env ARCHFLAGS="-arch i386" ./configure --prefix=/opt/GeoIP
|
84
|
+
|
85
|
+
env ARCHFLAGS="-arch i386" make
|
86
|
+
|
87
|
+
sudo env ARCHFLAGS="-arch i386" make install
|
88
|
+
|
89
|
+
2. Now install the `geoip` gem
|
90
|
+
|
91
|
+
gem install mtodd-geoip -s http://gems.github.com/ -- --with-geoip-dir=/opt/GeoIP
|
92
|
+
|
93
|
+
3. Download the GeoLite City database file in binary format at http://www.maxmind.com/app/geolitecity
|
94
|
+
Maybe this [direct link](http://www.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz) will work.
|
95
|
+
I put this file in
|
96
|
+
|
97
|
+
/opt/GeoIP/share/GeoIP/GeoLiteCity.dat
|
98
|
+
|
99
|
+
If you are a paying customer, you will download the files required below:
|
100
|
+
|
101
|
+
[MaxMind Customer Downloads](http://www.maxmind.com/app/download_files)
|
102
|
+
|
103
|
+
You will want to get the City Rev1 data file and Organization files at minimum.
|
104
|
+
|
105
|
+
4. Use it!
|
106
|
+
|
107
|
+
See above for usage details.
|
108
|
+
|
109
|
+
Hints
|
110
|
+
-----
|
111
|
+
|
112
|
+
1. Might need to set
|
113
|
+
|
114
|
+
export ARCHFLAGS="-arch i386"
|
115
|
+
|
116
|
+
to be able to compile the gem.
|
117
|
+
|
118
|
+
Example:
|
119
|
+
|
120
|
+
env ARCHFLAGS="-arch i386" gem install mtodd-geoip -s http://gems.github.com/ -- --with-geoip-dir=/opt/GeoIP
|
121
|
+
|
122
|
+
2. You might find [this shell script](http://github.com/grimen/my_shell_scripts/blob/8cf04cb6829e68a47f2d6f9d9e057766ea72beb4/install_geoip-city.sh)
|
123
|
+
helpful to install the C library.
|
124
|
+
|
125
|
+
Links
|
126
|
+
-----
|
127
|
+
|
128
|
+
This iteration of the library is based on the hard work of Ryah Dahl (ry@tinyclouds.org). You can find the original RDocs and Git Repo below:
|
129
|
+
|
130
|
+
[rdocs](http://geoip-city.rubyforge.org/)
|
131
|
+
[git repo](https://github.com/ry/geoip-city/tree)
|
132
|
+
|
133
|
+
License
|
134
|
+
-------
|
135
|
+
Copyright (C) 2007--2009 Ryah Dahl (ry@tinyclouds.org), Matt Todd (mtodd@highgroove.com)
|
136
|
+
|
137
|
+
This program is free software. It comes without any warranty, to
|
138
|
+
the extent permitted by applicable law. You can redistribute it
|
139
|
+
and/or modify it under the terms of the Do What The Fuck You Want
|
140
|
+
To Public License, Version 2, as published by Sam Hocevar. See
|
141
|
+
http://sam.zoy.org/wtfpl/COPYING for more details.
|
data/Rakefile
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/clean'
|
3
|
+
require 'rake/testtask'
|
4
|
+
require 'rake/rdoctask'
|
5
|
+
require 'rake/gempackagetask'
|
6
|
+
|
7
|
+
task :default => [:compile, :test]
|
8
|
+
|
9
|
+
CLEAN.add "geoip.{o,bundle,so,obj,pdb,lib,def,exp}"
|
10
|
+
CLOBBER.add ['Makefile', 'mkmf.log','doc']
|
11
|
+
|
12
|
+
Rake::RDocTask.new do |rdoc|
|
13
|
+
rdoc.rdoc_files.add ['README', 'geoip.c']
|
14
|
+
rdoc.main = "README" # page to start on
|
15
|
+
rdoc.rdoc_dir = 'doc/' # rdoc output folder
|
16
|
+
end
|
17
|
+
|
18
|
+
Rake::TestTask.new do |t|
|
19
|
+
t.test_files = 'test.rb'
|
20
|
+
t.verbose = true
|
21
|
+
end
|
22
|
+
|
23
|
+
spec = Gem::Specification.new do |s|
|
24
|
+
s.name = 'geoip-c'
|
25
|
+
s.version = "0.5.5"
|
26
|
+
|
27
|
+
s.authors = ['Ryah Dahl', 'Matt Todd', 'Charles Brian Quinn']
|
28
|
+
s.email = 'mtodd@highgroove.com'
|
29
|
+
|
30
|
+
s.summary = "A Binding to the GeoIP C library"
|
31
|
+
s.description = 'Generic GeoIP lookup tool. Based on the geoip_city RubyGem by Ryah Dahl'
|
32
|
+
s.homepage = "http://github.com/mtodd/geoip"
|
33
|
+
|
34
|
+
s.files = ["Rakefile", "extconf.rb", "test.rb", "geoip.c", "README.md"]
|
35
|
+
s.test_files = 'test.rb'
|
36
|
+
s.extensions = 'extconf.rb'
|
37
|
+
s.require_path = '.'
|
38
|
+
end
|
39
|
+
|
40
|
+
Rake::GemPackageTask.new(spec) do |p|
|
41
|
+
p.need_tar = true
|
42
|
+
p.gem_spec = spec
|
43
|
+
end
|
44
|
+
|
45
|
+
desc 'compile the extension'
|
46
|
+
task(:compile => 'Makefile') { sh 'make' }
|
47
|
+
file('Makefile' => "geoip.c") { ruby 'extconf.rb' }
|
48
|
+
|
49
|
+
task :install => [:gem] do
|
50
|
+
`env ARCHFLAGS="-arch i386" gem install pkg/geoip-c-0.5.0.gem -- --with-geoip-dir=/usr/local/GeoIP`
|
51
|
+
end
|
52
|
+
|
53
|
+
task(:webpage) do
|
54
|
+
sh 'scp -r doc/* rydahl@rubyforge.org:/var/www/gforge-projects/geoip-city/'
|
55
|
+
end
|
data/extconf.rb
ADDED
data/geoip.c
ADDED
@@ -0,0 +1,212 @@
|
|
1
|
+
/* ry dahl <ry@tinyclouds.org> May 21, 2007 */
|
2
|
+
/* Matt Todd <mtodd@highgroove.com> June 4, 2009 */
|
3
|
+
/* This program is free software. It comes without any warranty, to
|
4
|
+
* the extent permitted by applicable law. You can redistribute it
|
5
|
+
* and/or modify it under the terms of the Do What The Fuck You Want
|
6
|
+
* To Public License, Version 2, as published by Sam Hocevar. See
|
7
|
+
* http://sam.zoy.org/wtfpl/COPYING for more details. */
|
8
|
+
#include <ruby.h>
|
9
|
+
#include <GeoIP.h>
|
10
|
+
#include <GeoIPCity.h>
|
11
|
+
|
12
|
+
static VALUE mGeoIP;
|
13
|
+
static VALUE mGeoIP_City;
|
14
|
+
static VALUE mGeoIP_Organization;
|
15
|
+
static VALUE rb_geoip_memory;
|
16
|
+
static VALUE rb_geoip_filesystem;
|
17
|
+
static VALUE rb_geoip_index;
|
18
|
+
|
19
|
+
/* helpers */
|
20
|
+
void rb_hash_sset(VALUE hash, const char *str, VALUE v) {
|
21
|
+
rb_hash_aset(hash, ID2SYM(rb_intern(str)), v);
|
22
|
+
}
|
23
|
+
|
24
|
+
int check_load_option(VALUE load_option) {
|
25
|
+
if(load_option == rb_geoip_memory) {
|
26
|
+
return GEOIP_MEMORY_CACHE;
|
27
|
+
} else if(load_option == rb_geoip_filesystem) {
|
28
|
+
return GEOIP_STANDARD;
|
29
|
+
} else if(load_option == rb_geoip_index) {
|
30
|
+
return GEOIP_INDEX_CACHE;
|
31
|
+
} else {
|
32
|
+
rb_raise(rb_eTypeError, "the second option must be :memory, :filesystem, or :index");
|
33
|
+
return Qnil;
|
34
|
+
}
|
35
|
+
}
|
36
|
+
|
37
|
+
/* Generic initializer */
|
38
|
+
static VALUE rb_geoip_database_new(VALUE mGeoIP_Database_Class, int argc, VALUE *argv, VALUE self)
|
39
|
+
{
|
40
|
+
GeoIP *gi;
|
41
|
+
VALUE database = Qnil;
|
42
|
+
VALUE filename, load_option = Qnil, check_cache = Qnil;
|
43
|
+
int flag;
|
44
|
+
|
45
|
+
rb_scan_args(argc, argv, "12", &filename, &load_option, &check_cache);
|
46
|
+
if(NIL_P(load_option))
|
47
|
+
load_option = rb_geoip_memory;
|
48
|
+
if(NIL_P(check_cache))
|
49
|
+
check_cache = Qfalse;
|
50
|
+
Check_Type(load_option, T_SYMBOL);
|
51
|
+
|
52
|
+
if(flag = check_load_option(load_option)) flag;
|
53
|
+
|
54
|
+
if(RTEST(check_cache)) flag |= GEOIP_CHECK_CACHE;
|
55
|
+
|
56
|
+
if(gi = GeoIP_open(STR2CSTR(filename), flag)) {
|
57
|
+
database = Data_Wrap_Struct(mGeoIP_Database_Class, 0, GeoIP_delete, gi);
|
58
|
+
rb_obj_call_init(database, 0, 0);
|
59
|
+
} else {
|
60
|
+
rb_sys_fail("Problem opening database");
|
61
|
+
}
|
62
|
+
return database;
|
63
|
+
}
|
64
|
+
|
65
|
+
/* GeoIP::City ***************************************************************/
|
66
|
+
|
67
|
+
VALUE rb_city_record_to_hash(GeoIPRecord *record)
|
68
|
+
{
|
69
|
+
VALUE hash = rb_hash_new();
|
70
|
+
|
71
|
+
if(record->country_code)
|
72
|
+
rb_hash_sset(hash, "country_code", rb_str_new2(record->country_code));
|
73
|
+
if(record->country_code3)
|
74
|
+
rb_hash_sset(hash, "country_code3", rb_str_new2(record->country_code3));
|
75
|
+
if(record->country_name)
|
76
|
+
rb_hash_sset(hash, "country_name", rb_str_new2(record->country_name));
|
77
|
+
if(record->region)
|
78
|
+
rb_hash_sset(hash, "region", rb_str_new2(record->region));
|
79
|
+
if(record->city)
|
80
|
+
rb_hash_sset(hash, "city", rb_str_new2(record->city));
|
81
|
+
if(record->postal_code)
|
82
|
+
rb_hash_sset(hash, "postal_code", rb_str_new2(record->postal_code));
|
83
|
+
if(record->latitude)
|
84
|
+
rb_hash_sset(hash, "latitude", rb_float_new((double)record->latitude));
|
85
|
+
if(record->longitude)
|
86
|
+
rb_hash_sset(hash, "longitude", rb_float_new((double)record->longitude));
|
87
|
+
if(record->dma_code)
|
88
|
+
rb_hash_sset(hash, "dma_code", INT2NUM(record->dma_code));
|
89
|
+
if(record->area_code)
|
90
|
+
rb_hash_sset(hash, "area_code", INT2NUM(record->area_code));
|
91
|
+
|
92
|
+
return hash;
|
93
|
+
}
|
94
|
+
|
95
|
+
/* The first argument is the filename of the GeoIPCity.dat file
|
96
|
+
* load_option = :standard, :index, or :memory. default :memory
|
97
|
+
* check_cache = true or false. default false
|
98
|
+
*
|
99
|
+
* filesystem: read database from filesystem, uses least memory.
|
100
|
+
*
|
101
|
+
* index: the most frequently accessed index portion of the database,
|
102
|
+
* resulting in faster lookups than :filesystem, but less memory usage than
|
103
|
+
* :memory.
|
104
|
+
*
|
105
|
+
* memory: load database into memory, faster performance but uses more
|
106
|
+
* memory.
|
107
|
+
*/
|
108
|
+
static VALUE rb_geoip_city_new(int argc, VALUE *argv, VALUE self)
|
109
|
+
{
|
110
|
+
return rb_geoip_database_new(mGeoIP_City, argc, argv, self);
|
111
|
+
}
|
112
|
+
|
113
|
+
/* Pass this function an IP address as a string, it will return a hash
|
114
|
+
* containing all the information that the database knows about the IP
|
115
|
+
* db.look_up('24.24.24.24')
|
116
|
+
* => {:city=>"Ithaca", :latitude=>42.4277992248535,
|
117
|
+
* :country_code=>"US", :longitude=>-76.4981994628906,
|
118
|
+
* :country_code3=>"USA", :dma_code=>555,
|
119
|
+
* :country_name=>"United States", :area_code=>607,
|
120
|
+
* :region=>"NY"}
|
121
|
+
*/
|
122
|
+
VALUE rb_geoip_city_look_up(VALUE self, VALUE addr) {
|
123
|
+
GeoIP *gi;
|
124
|
+
GeoIPRecord *record = NULL;
|
125
|
+
VALUE hash = Qnil;
|
126
|
+
|
127
|
+
Check_Type(addr, T_STRING);
|
128
|
+
Data_Get_Struct(self, GeoIP, gi);
|
129
|
+
if(record = GeoIP_record_by_addr(gi, STR2CSTR(addr))) {
|
130
|
+
hash = rb_city_record_to_hash(record);
|
131
|
+
GeoIPRecord_delete(record);
|
132
|
+
}
|
133
|
+
return hash;
|
134
|
+
}
|
135
|
+
|
136
|
+
/* GeoIP::Organization *******************************************************/
|
137
|
+
|
138
|
+
/* GeoIP::Organization.new('/path/to/GeoIPOrg.dat', load_option)
|
139
|
+
* load_option can be:
|
140
|
+
* * :memory - load the data into memory, fastest (default)
|
141
|
+
* * :filesystem - look up from filesystem, least memory intensive
|
142
|
+
* * :index - stores in memory most recent queries
|
143
|
+
*
|
144
|
+
*/
|
145
|
+
static VALUE rb_geoip_org_new(int argc, VALUE *argv, VALUE self)
|
146
|
+
{
|
147
|
+
return rb_geoip_database_new(mGeoIP_Organization, argc, argv, self);
|
148
|
+
}
|
149
|
+
|
150
|
+
/* Pass this function an IP address as a string, it will return a hash
|
151
|
+
* containing all the information that the database knows about the IP:
|
152
|
+
* db.look_up('24.24.24.24')
|
153
|
+
* => {:name => "Road Runner"}
|
154
|
+
*/
|
155
|
+
VALUE rb_geoip_org_look_up(VALUE self, VALUE addr) {
|
156
|
+
GeoIP *gi;
|
157
|
+
VALUE hash = rb_hash_new();
|
158
|
+
char * name = NULL;
|
159
|
+
|
160
|
+
Check_Type(addr, T_STRING);
|
161
|
+
Data_Get_Struct(self, GeoIP, gi);
|
162
|
+
if(name = GeoIP_name_by_addr(gi, STR2CSTR(addr))) {
|
163
|
+
rb_hash_sset(hash, "name", rb_str_new2(name));
|
164
|
+
free(name);
|
165
|
+
}
|
166
|
+
return hash;
|
167
|
+
}
|
168
|
+
|
169
|
+
/* GeoIP *********************************************************************/
|
170
|
+
|
171
|
+
/* Returns the numeric form of an IP address.
|
172
|
+
*
|
173
|
+
* For example:
|
174
|
+
* 24.24.24.24 => 404232216
|
175
|
+
*
|
176
|
+
* This is used in order to be able to perform searches in CSV versions of the
|
177
|
+
* data files or in SQL records if the data has been put there.
|
178
|
+
*/
|
179
|
+
VALUE rb_geoip_addr_to_num(VALUE self, VALUE addr) {
|
180
|
+
Check_Type(addr, T_STRING);
|
181
|
+
return UINT2NUM((unsigned long)_GeoIP_addr_to_num(STR2CSTR(addr)));
|
182
|
+
}
|
183
|
+
|
184
|
+
VALUE rb_geoip_num_to_addr(VALUE self, VALUE num) {
|
185
|
+
VALUE num_type = TYPE(num);
|
186
|
+
switch(num_type) {
|
187
|
+
case T_FIXNUM: break;
|
188
|
+
case T_BIGNUM: break;
|
189
|
+
default: rb_raise(rb_eTypeError, "wrong argument type %s (expected Fixnum or Bignum)", rb_obj_classname(num));
|
190
|
+
}
|
191
|
+
return rb_str_new2((char*)_GeoIP_num_to_addr(NULL, (unsigned long)NUM2UINT(num)));
|
192
|
+
}
|
193
|
+
|
194
|
+
void Init_geoip()
|
195
|
+
{
|
196
|
+
mGeoIP = rb_define_module("GeoIP");
|
197
|
+
|
198
|
+
rb_geoip_memory = ID2SYM(rb_intern("memory"));
|
199
|
+
rb_geoip_filesystem = ID2SYM(rb_intern("filesystem"));
|
200
|
+
rb_geoip_index = ID2SYM(rb_intern("index"));
|
201
|
+
|
202
|
+
mGeoIP_City = rb_define_class_under(mGeoIP, "City", rb_cObject);
|
203
|
+
rb_define_singleton_method(mGeoIP_City, "new", rb_geoip_city_new, -1);
|
204
|
+
rb_define_method(mGeoIP_City, "look_up", rb_geoip_city_look_up, 1);
|
205
|
+
|
206
|
+
mGeoIP_Organization = rb_define_class_under(mGeoIP, "Organization", rb_cObject);
|
207
|
+
rb_define_singleton_method( mGeoIP_Organization, "new", rb_geoip_org_new, -1);
|
208
|
+
rb_define_method( mGeoIP_Organization, "look_up", rb_geoip_org_look_up, 1);
|
209
|
+
|
210
|
+
rb_define_singleton_method(mGeoIP, "addr_to_num", rb_geoip_addr_to_num, 1);
|
211
|
+
rb_define_singleton_method(mGeoIP, "num_to_addr", rb_geoip_num_to_addr, 1);
|
212
|
+
}
|
data/test.rb
ADDED
@@ -0,0 +1,162 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require File.dirname(__FILE__) + '/geoip'
|
3
|
+
require 'rubygems'
|
4
|
+
# require 'ruby-debug'
|
5
|
+
# Debugger.start
|
6
|
+
|
7
|
+
class Test::Unit::TestCase
|
8
|
+
|
9
|
+
def assert_look_up(db, addr, field, value)
|
10
|
+
h = db.look_up(addr)
|
11
|
+
assert_equal value, h[field]
|
12
|
+
h
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
class GeoIPTest < Test::Unit::TestCase
|
18
|
+
|
19
|
+
def setup
|
20
|
+
@ip = "24.24.24.24"
|
21
|
+
@ipnum = 16777216*24 + 65536*24 + 256*24 + 24
|
22
|
+
|
23
|
+
@large_ip = "245.245.245.245"
|
24
|
+
@large_ipnum = 16777216*245 + 65536*245 + 256*245 + 245
|
25
|
+
end
|
26
|
+
|
27
|
+
# addr_to_num
|
28
|
+
|
29
|
+
def test_addr_to_num_converts_an_ip_to_an_ipnum
|
30
|
+
assert_equal @ipnum, GeoIP.addr_to_num(@ip)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_addr_to_num_converts_large_ips_to_an_ipnum_correctly
|
34
|
+
assert_equal @large_ipnum, GeoIP.addr_to_num(@large_ip)
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_addr_to_num_expects_an_ip_string
|
38
|
+
assert_raises TypeError do
|
39
|
+
GeoIP.addr_to_num(nil)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_addr_to_num_returns_zero_for_an_illformed_ip_string
|
44
|
+
assert_equal 0, GeoIP.addr_to_num("foo.bar")
|
45
|
+
end
|
46
|
+
|
47
|
+
# num_to_addr
|
48
|
+
|
49
|
+
def test_num_to_addr_converts_an_ipnum_to_an_ip
|
50
|
+
assert_equal @ip, GeoIP.num_to_addr(@ipnum)
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_num_to_addr_converts_large_ipnums_to_an_ip_correctly
|
54
|
+
assert_equal @large_ip, GeoIP.num_to_addr(@large_ipnum)
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_num_to_addr_expects_a_numeric_ip
|
58
|
+
assert_raises TypeError do
|
59
|
+
GeoIP.num_to_addr(nil)
|
60
|
+
end
|
61
|
+
assert_raises TypeError do
|
62
|
+
GeoIP.num_to_addr("foo.bar")
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
class GeoIPCityTest < Test::Unit::TestCase
|
69
|
+
|
70
|
+
def setup
|
71
|
+
## Change me!
|
72
|
+
@dbfile = '/usr/local/GeoIP/share/GeoIP/GeoLiteCity.dat'
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_construction_default
|
76
|
+
db = GeoIP::City.new(@dbfile)
|
77
|
+
|
78
|
+
assert_raises TypeError do
|
79
|
+
db.look_up(nil)
|
80
|
+
end
|
81
|
+
|
82
|
+
h = db.look_up('24.24.24.24')
|
83
|
+
#debugger
|
84
|
+
assert_kind_of Hash, h
|
85
|
+
assert_equal 'Jamaica', h[:city]
|
86
|
+
assert_equal 'United States', h[:country_name]
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_construction_index
|
90
|
+
db = GeoIP::City.new(@dbfile, :index)
|
91
|
+
assert_look_up(db, '24.24.24.24', :city, 'Jamaica')
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_construction_filesystem
|
95
|
+
db = GeoIP::City.new(@dbfile, :filesystem)
|
96
|
+
assert_look_up(db, '24.24.24.24', :city, 'Jamaica')
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_construction_memory
|
100
|
+
db = GeoIP::City.new(@dbfile, :memory)
|
101
|
+
assert_look_up(db, '24.24.24.24', :city, 'Jamaica')
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_construction_filesystem_check
|
105
|
+
db = GeoIP::City.new(@dbfile, :filesystem, true)
|
106
|
+
assert_look_up(db, '24.24.24.24', :city, 'Jamaica')
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_bad_db_file
|
110
|
+
assert_raises Errno::ENOENT do
|
111
|
+
GeoIP::City.new('/blah')
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
class GeoIPOrgTest < Test::Unit::TestCase
|
118
|
+
|
119
|
+
def setup
|
120
|
+
## Change me!
|
121
|
+
@dbfile = '/usr/local/GeoIP/share/GeoIP/GeoIPOrg.dat'
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_construction_default
|
125
|
+
db = GeoIP::Organization.new(@dbfile)
|
126
|
+
|
127
|
+
assert_raises TypeError do
|
128
|
+
db.look_up(nil)
|
129
|
+
end
|
130
|
+
|
131
|
+
h = db.look_up('24.24.24.24')
|
132
|
+
assert_kind_of Hash, h
|
133
|
+
assert_equal 'Road Runner', h[:name]
|
134
|
+
end
|
135
|
+
|
136
|
+
def test_construction_index
|
137
|
+
db = GeoIP::Organization.new(@dbfile, :index)
|
138
|
+
assert_look_up(db, '24.24.24.24', :name, 'Road Runner')
|
139
|
+
end
|
140
|
+
|
141
|
+
def test_construction_filesystem
|
142
|
+
db = GeoIP::Organization.new(@dbfile, :filesystem)
|
143
|
+
assert_look_up(db, '24.24.24.24', :name, 'Road Runner')
|
144
|
+
end
|
145
|
+
|
146
|
+
def test_construction_memory
|
147
|
+
db = GeoIP::Organization.new(@dbfile, :memory)
|
148
|
+
assert_look_up(db, '24.24.24.24', :name, 'Road Runner')
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_construction_filesystem_check
|
152
|
+
db = GeoIP::Organization.new(@dbfile, :filesystem, true)
|
153
|
+
assert_look_up(db, '24.24.24.24', :name, 'Road Runner')
|
154
|
+
end
|
155
|
+
|
156
|
+
def test_bad_db_file
|
157
|
+
assert_raises Errno::ENOENT do
|
158
|
+
GeoIP::Organization.new('/blah')
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: geoip-c
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.5
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryah Dahl
|
8
|
+
- Matt Todd
|
9
|
+
- Charles Brian Quinn
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
|
14
|
+
date: 2009-10-27 00:00:00 -04:00
|
15
|
+
default_executable:
|
16
|
+
dependencies: []
|
17
|
+
|
18
|
+
description: Generic GeoIP lookup tool. Based on the geoip_city RubyGem by Ryah Dahl
|
19
|
+
email: mtodd@highgroove.com
|
20
|
+
executables: []
|
21
|
+
|
22
|
+
extensions:
|
23
|
+
- extconf.rb
|
24
|
+
extra_rdoc_files: []
|
25
|
+
|
26
|
+
files:
|
27
|
+
- Rakefile
|
28
|
+
- extconf.rb
|
29
|
+
- test.rb
|
30
|
+
- geoip.c
|
31
|
+
- README.md
|
32
|
+
has_rdoc: true
|
33
|
+
homepage: http://github.com/mtodd/geoip
|
34
|
+
licenses: []
|
35
|
+
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
|
39
|
+
require_paths:
|
40
|
+
- .
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
requirements: []
|
54
|
+
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.3.4
|
57
|
+
signing_key:
|
58
|
+
specification_version: 3
|
59
|
+
summary: A Binding to the GeoIP C library
|
60
|
+
test_files:
|
61
|
+
- test.rb
|