rgeoip 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "yard", "~> 0.6.0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.2"
12
+ gem "rcov", ">= 0"
13
+ gem "ZenTest"
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,22 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ ZenTest (4.4.2)
5
+ git (1.2.5)
6
+ jeweler (1.5.2)
7
+ bundler (~> 1.0.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rake (0.8.7)
11
+ rcov (0.9.9)
12
+ yard (0.6.4)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ ZenTest
19
+ bundler (~> 1.0.0)
20
+ jeweler (~> 1.5.2)
21
+ rcov
22
+ yard (~> 0.6.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 miyucy
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = rgeoip
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to rgeoip
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 miyucy. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "rgeoip"
16
+ gem.homepage = "http://github.com/miyucy/rgeoip"
17
+ gem.license = "LGPL"
18
+ gem.summary = %Q{alternative libGeoIP binding for Ruby}
19
+ gem.description = %Q{alternative libGeoIP binding for Ruby}
20
+ gem.email = "miyucy@gmail.com"
21
+ gem.authors = ["miyucy"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'ext' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'yard'
46
+ YARD::Rake::YardocTask.new do |yard|
47
+ yard.files << FileList['lib/**/*.rb']
48
+ yard.files << FileList['ext/**/*.c']
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
data/ext/extconf.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "mkmf"
2
+
3
+ have_header "GeoIP.h"
4
+ have_header "GeoIPCity.h"
5
+
6
+ have_library "GeoIP"
7
+
8
+ create_makefile "rgeoip"
data/ext/rgeoip.c ADDED
@@ -0,0 +1,496 @@
1
+ #include "ruby.h"
2
+ #include <GeoIP.h>
3
+ #include <GeoIPCity.h>
4
+
5
+ /* -------- */
6
+
7
+ typedef int (*GeoIP_id_by) (GeoIP*, const char *);
8
+ typedef GeoIPRecord* (*GeoIP_record_by) (GeoIP*, const char *);
9
+
10
+ /* -------- */
11
+
12
+ enum {
13
+ RS_CODE,
14
+ RS_CODE3,
15
+ RS_NAME,
16
+ RS_REGION,
17
+ RS_CITY,
18
+ RS_POSTAL_CODE,
19
+ RS_LATITUDE,
20
+ RS_LONGITUDE,
21
+ RS_DMA_CODE,
22
+ RS_AREA_CODE,
23
+ RGEOIP_SYMBOLS_MAX,
24
+ } RGEOIP_SYMBOLS;
25
+
26
+ /* -------- */
27
+
28
+ static VALUE rb_cRgeoip, rb_cRgeoipDatabase;
29
+ static VALUE rgeoip_symbols[RGEOIP_SYMBOLS_MAX];
30
+
31
+ /* -------- */
32
+
33
+ typedef struct {
34
+ VALUE databases;
35
+ } Rgeoip;
36
+
37
+ typedef struct {
38
+ GeoIP *ptr;
39
+ } RgeoipDatabase;
40
+
41
+ /* -------- */
42
+
43
+ static VALUE
44
+ gi_alloc(VALUE klass);
45
+ static void
46
+ gi_mark(Rgeoip* gi);
47
+ static void
48
+ gi_free(Rgeoip* gi);
49
+ static VALUE
50
+ gi_new(int argc, VALUE *argv, VALUE self);
51
+ static VALUE
52
+ gi_open(VALUE self, VALUE filenames);
53
+ static VALUE
54
+ gi_country(VALUE self, VALUE addr_or_host);
55
+ static VALUE
56
+ gi_city(VALUE self, VALUE addr_or_host);
57
+ static VALUE
58
+ gid_alloc(VALUE klass);
59
+ static void
60
+ gid_mark(RgeoipDatabase* gid);
61
+ static void
62
+ gid_free(RgeoipDatabase* gid);
63
+ static VALUE
64
+ gid_country(VALUE self, VALUE addr_or_host);
65
+ static VALUE
66
+ gid_city(VALUE self, VALUE addr_or_host);
67
+ static GeoIP_id_by
68
+ GeoIP_id_dwim(VALUE addr);
69
+ static GeoIP_record_by
70
+ GeoIP_record_dwim(VALUE addr);
71
+ static int
72
+ is_addr_p(VALUE value);
73
+ static VALUE
74
+ rb_hash_sset(VALUE hash, VALUE key, VALUE value);
75
+
76
+ /* Rgeoip */
77
+ /*
78
+ * Document-class: Rgeoip
79
+ * * libGeoIP binding
80
+ */
81
+
82
+ static VALUE
83
+ gi_alloc(VALUE klass)
84
+ {
85
+ return Data_Wrap_Struct(klass, gi_mark, gi_free, 0);
86
+ }
87
+
88
+ static void
89
+ gi_mark(Rgeoip* gi)
90
+ {
91
+ rb_gc_mark(gi->databases);
92
+ }
93
+
94
+ static void
95
+ gi_free(Rgeoip* gi)
96
+ {
97
+ xfree(gi);
98
+ }
99
+
100
+ /*
101
+ * Document-method: new
102
+ * call-seq:
103
+ * Rgeoip.new
104
+ * Rgeoip.new("/path/to/database")
105
+ * Rgeoip.new("/path/to/database", "/path/to/other_database")
106
+ * Rgeoip.new(["/path/to/database", "/path/to/other_database"])
107
+ *
108
+ * @param path to GeoIP.dat and/or GeoLiteCity.dat
109
+ * @raise [RuntimeError] Problem opening database
110
+ * @return [Rgeoip] Rgeoip instance
111
+ */
112
+ static VALUE
113
+ gi_new(int argc, VALUE *argv, VALUE self)
114
+ {
115
+ Rgeoip *gi;
116
+ VALUE flags;
117
+ VALUE filenames;
118
+ int i;
119
+
120
+ Data_Get_Struct(self, Rgeoip, gi);
121
+ if (gi == NULL)
122
+ {
123
+ gi = ALLOC(Rgeoip);
124
+ MEMZERO(gi, Rgeoip, 1);
125
+ DATA_PTR(self) = gi;
126
+ }
127
+
128
+ gi->databases = rb_ary_new();
129
+
130
+ for (i=0; i<argc; i++)
131
+ {
132
+ gi_open(self, argv[i]);
133
+ }
134
+
135
+ return self;
136
+ }
137
+
138
+ static VALUE
139
+ _gi_open(VALUE filenames, VALUE self)
140
+ {
141
+ return gi_open(self, filenames);
142
+ }
143
+
144
+ /*
145
+ * Document-method: open
146
+ * call-seq:
147
+ * Rgeoip.open("/path/to/database")
148
+ * Rgeoip.open("/path/to/database", "/path/to/other_database")
149
+ * Rgeoip.open(["/path/to/database", "/path/to/other_database"])
150
+ *
151
+ * @param path to GeoIP.dat and/or GeoLiteCity.dat
152
+ * @raise [RuntimeError] Problem opening database
153
+ * @return [Rgeoip] Rgeoip instance
154
+ */
155
+ static VALUE
156
+ gi_open(VALUE self, VALUE filenames)
157
+ {
158
+ Rgeoip *gi;
159
+ VALUE args[1] = {0};
160
+ Data_Get_Struct(self, Rgeoip, gi);
161
+
162
+ if (TYPE(filenames) == T_ARRAY)
163
+ {
164
+ rb_iterate(rb_each, filenames, _gi_open, self);
165
+ }
166
+ else
167
+ {
168
+ args[0] = filenames;
169
+ rb_ary_push(gi->databases, rb_class_new_instance(1, args, rb_cRgeoipDatabase));
170
+ }
171
+
172
+ return self;
173
+ }
174
+
175
+ /*
176
+ * Document-method: country
177
+ * call-seq:
178
+ * rgeoip.country("example.com")
179
+ * rgeoip.country("127.0.0.1")
180
+ *
181
+ * @param [String] addr_or_host ipaddress or hostname
182
+ * @return Hash object or nil
183
+ */
184
+ static VALUE
185
+ gi_country(VALUE self, VALUE addr_or_host)
186
+ {
187
+ Rgeoip *gi;
188
+ RgeoipDatabase *gid;
189
+ int i;
190
+ VALUE result;
191
+
192
+ Data_Get_Struct(self, Rgeoip, gi);
193
+ for(i=0; i<RARRAY_LEN(gi->databases); i++)
194
+ {
195
+ Data_Get_Struct(RARRAY_PTR(gi->databases)[i], RgeoipDatabase, gid);
196
+ if (gid->ptr->databaseType != GEOIP_COUNTRY_EDITION)
197
+ {
198
+ continue;
199
+ }
200
+
201
+ result = gid_country(RARRAY_PTR(gi->databases)[i], addr_or_host);
202
+ if (!NIL_P(result))
203
+ {
204
+ return result;
205
+ }
206
+ }
207
+ }
208
+
209
+ /*
210
+ * Document-method: city
211
+ * call-seq:
212
+ * rgeoip.city("example.com")
213
+ * rgeoip.city("127.0.0.1")
214
+ *
215
+ * @param [String] addr_or_host ipaddress or hostname
216
+ * @return Hash object or nil
217
+ */
218
+ static VALUE
219
+ gi_city(VALUE self, VALUE addr_or_host)
220
+ {
221
+ Rgeoip *gi;
222
+ RgeoipDatabase *gid;
223
+ int i;
224
+ VALUE result;
225
+
226
+ Data_Get_Struct(self, Rgeoip, gi);
227
+ for(i=0; i<RARRAY_LEN(gi->databases); i++)
228
+ {
229
+ Data_Get_Struct(RARRAY_PTR(gi->databases)[i], RgeoipDatabase, gid);
230
+ if (gid->ptr->databaseType != GEOIP_CITY_EDITION_REV0 &&
231
+ gid->ptr->databaseType != GEOIP_CITY_EDITION_REV1)
232
+ {
233
+ continue;
234
+ }
235
+
236
+ result = gid_city(RARRAY_PTR(gi->databases)[i], addr_or_host);
237
+ if (!NIL_P(result))
238
+ {
239
+ return result;
240
+ }
241
+ }
242
+
243
+ return Qnil;
244
+ }
245
+
246
+ /* Rgeoip::Database */
247
+ /*
248
+ * Document-class: Rgeoip::Database
249
+ * * GeoIP Container
250
+ */
251
+
252
+ static VALUE
253
+ gid_alloc(VALUE klass)
254
+ {
255
+ return Data_Wrap_Struct(klass, gid_mark, gid_free, 0);
256
+ }
257
+
258
+ static void
259
+ gid_mark(RgeoipDatabase* gid)
260
+ {
261
+ }
262
+
263
+ static void
264
+ gid_free(RgeoipDatabase* gid)
265
+ {
266
+ GeoIP_delete(gid->ptr);
267
+ xfree(gid);
268
+ }
269
+
270
+ /*
271
+ * Document-method: new
272
+ * call-seq:
273
+ * Rgeoip::Database.new "/path/to/dbfile"
274
+ *
275
+ * @param [String] filename path to GeoIP.dat or GeoLiteCity.dat
276
+ * @raise [RuntimeError] Problem opening database
277
+ * @return [Rgeoip::Database] Rgeoip::Database instance
278
+ */
279
+ static VALUE
280
+ gid_new(int argc, VALUE *argv, VALUE self)
281
+ {
282
+ RgeoipDatabase *gid;
283
+ VALUE filename, flags;
284
+
285
+ Data_Get_Struct(self, RgeoipDatabase, gid);
286
+ if (gid == NULL)
287
+ {
288
+ gid = ALLOC(RgeoipDatabase);
289
+ MEMZERO(gid, RgeoipDatabase, 1);
290
+ DATA_PTR(self) = gid;
291
+ }
292
+
293
+ rb_scan_args(argc, argv, "11", &filename, &flags);
294
+
295
+ Check_Type(filename, T_STRING);
296
+
297
+ gid->ptr = GeoIP_open(RSTRING_PTR(filename),
298
+ GEOIP_MEMORY_CACHE | GEOIP_CHECK_CACHE | GEOIP_MMAP_CACHE);
299
+ if (gid->ptr == NULL)
300
+ {
301
+ rb_raise(rb_eRuntimeError, "Problem opening database");
302
+ }
303
+
304
+ return self;
305
+ }
306
+
307
+ /*
308
+ * Document-method: country
309
+ * call-seq:
310
+ * db.country("example.com")
311
+ * db.country("127.0.0.1")
312
+ *
313
+ * @param [String] addr_or_host ipaddress or hostname
314
+ * @return Hash object or nil
315
+ */
316
+ static VALUE
317
+ gid_country(VALUE self, VALUE addr_or_host)
318
+ {
319
+ RgeoipDatabase *gid;
320
+ GeoIP_id_by any = GeoIP_id_dwim(addr_or_host);
321
+ int id;
322
+ VALUE result;
323
+
324
+ if (NIL_P(addr_or_host))
325
+ {
326
+ return Qnil;
327
+ }
328
+
329
+ Data_Get_Struct(self, RgeoipDatabase, gid);
330
+
331
+ id = (*any)(gid->ptr, RSTRING_PTR(addr_or_host));
332
+ if (id == 0)
333
+ {
334
+ return Qnil;
335
+ }
336
+
337
+ result = rb_hash_new();
338
+ if (GeoIP_code_by_id(id))
339
+ {
340
+ rb_hash_sset(result, rgeoip_symbols[RS_CODE], rb_str_freeze(rb_str_new2(GeoIP_code_by_id(id))));
341
+ }
342
+ if (GeoIP_code3_by_id(id))
343
+ {
344
+ rb_hash_sset(result, rgeoip_symbols[RS_CODE3], rb_str_freeze(rb_str_new2(GeoIP_code3_by_id(id))));
345
+ }
346
+ if (GeoIP_name_by_id(id))
347
+ {
348
+ rb_hash_sset(result, rgeoip_symbols[RS_NAME], rb_str_freeze(rb_str_new2(GeoIP_name_by_id(id))));
349
+ }
350
+
351
+ return result;
352
+ }
353
+
354
+ /*
355
+ * Document-method: city
356
+ * call-seq:
357
+ * db.city("example.com")
358
+ * db.city("127.0.0.1")
359
+ *
360
+ * @param [String] addr_or_host ipaddress or hostname
361
+ * @return Hash object or nil
362
+ */
363
+ static VALUE
364
+ gid_city(VALUE self, VALUE addr_or_host)
365
+ {
366
+ RgeoipDatabase *gid;
367
+ GeoIP_record_by any = GeoIP_record_dwim(addr_or_host);
368
+ GeoIPRecord *record;
369
+ VALUE result;
370
+
371
+ if (NIL_P(addr_or_host))
372
+ {
373
+ return Qnil;
374
+ }
375
+
376
+ Data_Get_Struct(self, RgeoipDatabase, gid);
377
+
378
+ record = (*any)(gid->ptr, RSTRING_PTR(addr_or_host));
379
+ if (record == NULL)
380
+ {
381
+ return Qnil;
382
+ }
383
+
384
+ result = rb_hash_new();
385
+ if (record->country_code)
386
+ {
387
+ rb_hash_sset(result, rgeoip_symbols[RS_CODE], rb_str_freeze(rb_str_new2(record->country_code)));
388
+ }
389
+ if (record->country_code3)
390
+ {
391
+ rb_hash_sset(result, rgeoip_symbols[RS_CODE3], rb_str_freeze(rb_str_new2(record->country_code3)));
392
+ }
393
+ if (record->country_name)
394
+ {
395
+ rb_hash_sset(result, rgeoip_symbols[RS_NAME], rb_str_freeze(rb_str_new2(record->country_name)));
396
+ }
397
+ if (record->region)
398
+ {
399
+ rb_hash_sset(result, rgeoip_symbols[RS_REGION], rb_str_freeze(rb_str_new2(record->region)));
400
+ }
401
+ if (record->city)
402
+ {
403
+ rb_hash_sset(result, rgeoip_symbols[RS_CITY], rb_str_freeze(rb_str_new2(record->city)));
404
+ }
405
+ if (record->postal_code)
406
+ {
407
+ rb_hash_sset(result, rgeoip_symbols[RS_POSTAL_CODE], rb_str_freeze(rb_str_new2(record->postal_code)));
408
+ }
409
+ rb_hash_sset(result, rgeoip_symbols[RS_LATITUDE], rb_float_new((double)record->latitude));
410
+ rb_hash_sset(result, rgeoip_symbols[RS_LONGITUDE], rb_float_new((double)record->longitude));
411
+ rb_hash_sset(result, rgeoip_symbols[RS_DMA_CODE], INT2NUM(record->dma_code));
412
+ rb_hash_sset(result, rgeoip_symbols[RS_AREA_CODE], INT2NUM(record->area_code));
413
+ GeoIPRecord_delete(record);
414
+
415
+ return result;
416
+ }
417
+
418
+ /* -------- */
419
+
420
+ static GeoIP_id_by
421
+ GeoIP_id_dwim(VALUE addr)
422
+ {
423
+ if (NIL_P(addr))
424
+ {
425
+ return NULL;
426
+ }
427
+ if (is_addr_p(addr))
428
+ {
429
+ return GeoIP_id_by_addr;
430
+ }
431
+ return GeoIP_id_by_name;
432
+ }
433
+
434
+ static GeoIP_record_by
435
+ GeoIP_record_dwim(VALUE addr)
436
+ {
437
+ if (NIL_P(addr))
438
+ {
439
+ return NULL;
440
+ }
441
+ if (is_addr_p(addr))
442
+ {
443
+ return GeoIP_record_by_addr;
444
+ }
445
+ return GeoIP_record_by_name;
446
+ }
447
+
448
+ static int
449
+ is_addr_p(VALUE value)
450
+ {
451
+ int a,b,c,d;
452
+ char e;
453
+
454
+ Check_Type(value, T_STRING);
455
+ if (4 <= sscanf(RSTRING_PTR(value), "%d.%d.%d.%d%c", &a, &b, &c, &d, &e))
456
+ {
457
+ return 1;
458
+ }
459
+ return 0;
460
+ }
461
+
462
+ static VALUE
463
+ rb_hash_sset(VALUE hash, VALUE key, VALUE value)
464
+ {
465
+ return rb_hash_aset(hash, key, value);
466
+ }
467
+
468
+ /* -------- */
469
+
470
+ void
471
+ Init_rgeoip(void)
472
+ {
473
+ rb_cRgeoip = rb_define_class("Rgeoip", rb_cObject);
474
+ rb_define_alloc_func(rb_cRgeoip, gi_alloc);
475
+ rb_define_method(rb_cRgeoip, "initialize", gi_new, -1);
476
+ rb_define_method(rb_cRgeoip, "open", gi_open, 1);
477
+ rb_define_method(rb_cRgeoip, "country", gi_country, 1);
478
+ rb_define_method(rb_cRgeoip, "city", gi_city, 1);
479
+
480
+ rb_cRgeoipDatabase = rb_define_class_under(rb_cRgeoip, "Database", rb_cObject);
481
+ rb_define_alloc_func(rb_cRgeoipDatabase, gid_alloc);
482
+ rb_define_method(rb_cRgeoipDatabase, "initialize", gid_new, -1);
483
+ rb_define_method(rb_cRgeoipDatabase, "country", gid_country, 1);
484
+ rb_define_method(rb_cRgeoipDatabase, "city", gid_city, 1);
485
+
486
+ rgeoip_symbols[RS_CODE] = ID2SYM(rb_intern("code"));
487
+ rgeoip_symbols[RS_CODE3] = ID2SYM(rb_intern("code3"));
488
+ rgeoip_symbols[RS_NAME] = ID2SYM(rb_intern("name"));
489
+ rgeoip_symbols[RS_REGION] = ID2SYM(rb_intern("region"));
490
+ rgeoip_symbols[RS_CITY] = ID2SYM(rb_intern("city"));
491
+ rgeoip_symbols[RS_POSTAL_CODE] = ID2SYM(rb_intern("postal_code"));
492
+ rgeoip_symbols[RS_LATITUDE] = ID2SYM(rb_intern("latitude"));
493
+ rgeoip_symbols[RS_LONGITUDE] = ID2SYM(rb_intern("longitude"));
494
+ rgeoip_symbols[RS_DMA_CODE] = ID2SYM(rb_intern("dma_code"));
495
+ rgeoip_symbols[RS_AREA_CODE] = ID2SYM(rb_intern("area_code"));
496
+ }
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+
12
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'ext'))
15
+ require 'rgeoip'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,67 @@
1
+ require 'helper'
2
+ require 'tempfile'
3
+
4
+ class TestRgeoip < Test::Unit::TestCase
5
+ def test_constant
6
+ assert_not_nil(defined? Rgeoip)
7
+ end
8
+
9
+ def test_new
10
+ assert_instance_of(Rgeoip, Rgeoip.new)
11
+ end
12
+
13
+ def test_open
14
+ assert_nothing_raised {
15
+ Rgeoip.new.open paths[:country]
16
+ }
17
+ end
18
+
19
+ def test_open_invalid_dbfile
20
+ assert_raise(RuntimeError) {
21
+ Rgeoip.new.open Tempfile.new("rgeoip").path
22
+ }
23
+
24
+ assert_raise(TypeError) {
25
+ Rgeoip.new.open nil
26
+ }
27
+ end
28
+
29
+ def test_open_array
30
+ assert_nothing_raised {
31
+ Rgeoip.new.open [paths[:country], paths[:country]]
32
+ }
33
+ end
34
+
35
+ def test_open_array_invalid_dbfile
36
+ assert_raise(RuntimeError) {
37
+ Rgeoip.new.open [Tempfile.new("rgeoip").path, Tempfile.new("rgeoip").path]
38
+ }
39
+
40
+ assert_raise(TypeError) {
41
+ Rgeoip.new.open [nil]
42
+ }
43
+ end
44
+
45
+ def test_country
46
+ assert_equal("JP", rgeoip.country("m.root-servers.net")[:code])
47
+ assert_equal("JP", rgeoip.country("202.12.27.33")[:code])
48
+ end
49
+
50
+ def test_city
51
+ assert_equal("Tokyo", rgeoip.city("m.root-servers.net")[:city])
52
+ assert_equal("Tokyo", rgeoip.city("202.12.27.33")[:city])
53
+ end
54
+
55
+ private
56
+
57
+ def paths
58
+ {
59
+ :country => File.expand_path("../GeoIP.dat", File.dirname(__FILE__)),
60
+ :city => File.expand_path("../GeoLiteCity.dat", File.dirname(__FILE__)),
61
+ }
62
+ end
63
+
64
+ def rgeoip
65
+ Rgeoip.new(paths[:country], paths[:city])
66
+ end
67
+ end
@@ -0,0 +1,91 @@
1
+ require 'helper'
2
+ require 'benchmark'
3
+
4
+ class TestRgeoipDatabase < Test::Unit::TestCase
5
+ def test_constant
6
+ assert_not_nil(defined? Rgeoip::Database)
7
+ end
8
+
9
+ def test_new
10
+ assert_instance_of(Rgeoip::Database, country_db)
11
+ end
12
+
13
+ def test_country
14
+ result = country_db.country("198.41.0.4")
15
+ assert_equal("US", result[:code])
16
+ assert_equal("USA", result[:code3])
17
+ assert_equal("United States", result[:name])
18
+
19
+ result = country_db.country("202.12.27.33")
20
+ assert_equal("JP", result[:code])
21
+ assert_equal("JPN", result[:code3])
22
+ assert_equal("Japan", result[:name])
23
+ end
24
+
25
+ def test_country_by_hostname
26
+ result = country_db.country("a.root-servers.net")
27
+ assert_equal("US", result[:code])
28
+ assert_equal("USA", result[:code3])
29
+ assert_equal("United States", result[:name])
30
+
31
+ result = country_db.country("m.root-servers.net")
32
+ assert_equal("JP", result[:code])
33
+ assert_equal("JPN", result[:code3])
34
+ assert_equal("Japan", result[:name])
35
+ end
36
+
37
+ def test_country_by_invalid_argument
38
+ assert_equal(nil, country_db.country(nil))
39
+ assert_equal(nil, country_db.country(""))
40
+ assert_equal(nil, country_db.country("a" * 100_000))
41
+ assert_equal(nil, country_db.country("this.is.example"))
42
+ assert_equal(nil, country_db.country("1."))
43
+ assert_equal(nil, country_db.country(".1"))
44
+ assert_equal(nil, country_db.country("1.2.3.4.5"))
45
+ end
46
+
47
+ def test_city
48
+ {
49
+ "198.41.0.4" => "Sterling",
50
+ "202.12.27.33" => "Tokyo",
51
+ }.each do |addr, city|
52
+ assert_equal(city, city_db.city(addr)[:city])
53
+ end
54
+ end
55
+
56
+ def test_city_by_hostname
57
+ {
58
+ "a.root-servers.net" => "Sterling",
59
+ "m.root-servers.net" => "Tokyo",
60
+ }.each do |addr, city|
61
+ assert_equal(city, city_db.city(addr)[:city])
62
+ end
63
+ end
64
+
65
+ def test_city_by_invalid_argument
66
+ assert_equal(nil, city_db.city(nil))
67
+ assert_equal(nil, city_db.city(""))
68
+ assert_equal(nil, city_db.city("a" * 100_000))
69
+ assert_equal(nil, city_db.city("this.is.example"))
70
+ assert_equal(nil, city_db.city("1."))
71
+ assert_equal(nil, city_db.city(".1"))
72
+ assert_equal(nil, city_db.city("1.2.3.4.5"))
73
+ end
74
+
75
+ private
76
+
77
+ def paths
78
+ {
79
+ :country => File.expand_path("../GeoIP.dat", File.dirname(__FILE__)),
80
+ :city => File.expand_path("../GeoLiteCity.dat", File.dirname(__FILE__)),
81
+ }
82
+ end
83
+
84
+ def country_db
85
+ Rgeoip::Database.new(paths[:country], 0)
86
+ end
87
+
88
+ def city_db
89
+ Rgeoip::Database.new(paths[:city], 0)
90
+ end
91
+ end
metadata ADDED
@@ -0,0 +1,156 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rgeoip
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 0
10
+ version: 0.0.0
11
+ platform: ruby
12
+ authors:
13
+ - miyucy
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-26 00:00:00 +09:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ hash: 7
28
+ segments:
29
+ - 0
30
+ - 6
31
+ - 0
32
+ version: 0.6.0
33
+ prerelease: false
34
+ name: yard
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 23
44
+ segments:
45
+ - 1
46
+ - 0
47
+ - 0
48
+ version: 1.0.0
49
+ prerelease: false
50
+ name: bundler
51
+ type: :development
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ hash: 7
60
+ segments:
61
+ - 1
62
+ - 5
63
+ - 2
64
+ version: 1.5.2
65
+ prerelease: false
66
+ name: jeweler
67
+ type: :development
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ requirement: &id004 !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ prerelease: false
80
+ name: rcov
81
+ type: :development
82
+ version_requirements: *id004
83
+ - !ruby/object:Gem::Dependency
84
+ requirement: &id005 !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ prerelease: false
94
+ name: ZenTest
95
+ type: :development
96
+ version_requirements: *id005
97
+ description: alternative libGeoIP binding for Ruby
98
+ email: miyucy@gmail.com
99
+ executables: []
100
+
101
+ extensions:
102
+ - ext/extconf.rb
103
+ extra_rdoc_files:
104
+ - LICENSE.txt
105
+ - README.rdoc
106
+ files:
107
+ - .document
108
+ - Gemfile
109
+ - Gemfile.lock
110
+ - LICENSE.txt
111
+ - README.rdoc
112
+ - Rakefile
113
+ - VERSION
114
+ - ext/extconf.rb
115
+ - ext/rgeoip.c
116
+ - test/helper.rb
117
+ - test/test_rgeoip.rb
118
+ - test/test_rgeoip_database.rb
119
+ has_rdoc: true
120
+ homepage: http://github.com/miyucy/rgeoip
121
+ licenses:
122
+ - LGPL
123
+ post_install_message:
124
+ rdoc_options: []
125
+
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ hash: 3
134
+ segments:
135
+ - 0
136
+ version: "0"
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ hash: 3
143
+ segments:
144
+ - 0
145
+ version: "0"
146
+ requirements: []
147
+
148
+ rubyforge_project:
149
+ rubygems_version: 1.3.7
150
+ signing_key:
151
+ specification_version: 3
152
+ summary: alternative libGeoIP binding for Ruby
153
+ test_files:
154
+ - test/helper.rb
155
+ - test/test_rgeoip.rb
156
+ - test/test_rgeoip_database.rb