geoip-c 0.7.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/Rakefile +1 -1
  2. data/extconf.rb +4 -0
  3. data/geoip.c +27 -7
  4. data/test.rb +17 -9
  5. metadata +17 -5
data/Rakefile CHANGED
@@ -22,7 +22,7 @@ end
22
22
 
23
23
  spec = Gem::Specification.new do |s|
24
24
  s.name = 'geoip-c'
25
- s.version = "0.7.0"
25
+ s.version = "0.7.1"
26
26
 
27
27
  s.authors = ['Ryah Dahl', 'Matt Todd', 'Charles Brian Quinn', 'Michael Sheakoski', 'Silvio Quadri']
28
28
  s.email = 'mtodd@highgroove.com'
data/extconf.rb CHANGED
@@ -2,6 +2,10 @@ require 'mkmf'
2
2
 
3
3
  dir_config("geoip")
4
4
 
5
+ unless have_func('iconv_open', 'iconv.h') or have_library('iconv', 'iconv_open', 'iconv.h')
6
+ abort "you must have iconv library installed!"
7
+ end
8
+
5
9
  if have_library('GeoIP', 'GeoIP_record_by_ipnum') and have_header('GeoIPCity.h')
6
10
  create_makefile('geoip')
7
11
  else
data/geoip.c CHANGED
@@ -8,6 +8,7 @@
8
8
  #include <ruby.h>
9
9
  #include <GeoIP.h>
10
10
  #include <GeoIPCity.h>
11
+ #include "iconv.h"
11
12
 
12
13
  static VALUE mGeoIP;
13
14
  static VALUE mGeoIP_City;
@@ -25,6 +26,25 @@ void rb_hash_sset(VALUE hash, const char *str, VALUE v) {
25
26
  rb_hash_aset(hash, ID2SYM(rb_intern(str)), v);
26
27
  }
27
28
 
29
+ /* pulled from http://blog.inventic.eu/?p=238 and
30
+ https://github.com/Vagabond/erlang-iconv/blob/master/c_src/iconv_drv.c */
31
+ static VALUE encode_to_utf8_and_return_rb_str(char *value) {
32
+ char dst[BUFSIZ];
33
+ size_t srclen = strlen(value);
34
+ size_t dstlen = srclen * 2;
35
+
36
+ char * pIn = value;
37
+ char * pOut = ( char*)dst;
38
+
39
+ iconv_t cd = iconv_open("UTF-8","ISO-8859-1");
40
+ iconv(cd, &pIn, &srclen, &pOut, &dstlen);
41
+ iconv_close(cd);
42
+
43
+ *(pOut++) = 0; /* ensure we null terminate */
44
+
45
+ return rb_str_new2(dst);
46
+ }
47
+
28
48
  int check_load_option(VALUE load_option) {
29
49
  if(load_option == rb_geoip_memory) {
30
50
  return GEOIP_MEMORY_CACHE;
@@ -71,7 +91,7 @@ static VALUE generic_single_value_lookup_response(char *key, char *value)
71
91
  {
72
92
  VALUE result = rb_hash_new();
73
93
  if(value) {
74
- rb_hash_sset(result, key, rb_str_new2(value));
94
+ rb_hash_sset(result, key, encode_to_utf8_and_return_rb_str(value));
75
95
  return result;
76
96
  } else {
77
97
  return Qnil;
@@ -85,17 +105,17 @@ VALUE rb_city_record_to_hash(GeoIPRecord *record)
85
105
  VALUE hash = rb_hash_new();
86
106
 
87
107
  if(record->country_code)
88
- rb_hash_sset(hash, "country_code", rb_str_new2(record->country_code));
108
+ rb_hash_sset(hash, "country_code", encode_to_utf8_and_return_rb_str(record->country_code));
89
109
  if(record->country_code3)
90
- rb_hash_sset(hash, "country_code3", rb_str_new2(record->country_code3));
110
+ rb_hash_sset(hash, "country_code3", encode_to_utf8_and_return_rb_str(record->country_code3));
91
111
  if(record->country_name)
92
- rb_hash_sset(hash, "country_name", rb_str_new2(record->country_name));
112
+ rb_hash_sset(hash, "country_name", encode_to_utf8_and_return_rb_str(record->country_name));
93
113
  if(record->region)
94
- rb_hash_sset(hash, "region", rb_str_new2(record->region));
114
+ rb_hash_sset(hash, "region", encode_to_utf8_and_return_rb_str(record->region));
95
115
  if(record->city)
96
- rb_hash_sset(hash, "city", rb_str_new2(record->city));
116
+ rb_hash_sset(hash, "city", encode_to_utf8_and_return_rb_str(record->city));
97
117
  if(record->postal_code)
98
- rb_hash_sset(hash, "postal_code", rb_str_new2(record->postal_code));
118
+ rb_hash_sset(hash, "postal_code", encode_to_utf8_and_return_rb_str(record->postal_code));
99
119
  if(record->latitude)
100
120
  rb_hash_sset(hash, "latitude", rb_float_new((double)record->latitude));
101
121
  if(record->longitude)
data/test.rb CHANGED
@@ -4,6 +4,9 @@ require 'rubygems'
4
4
  # require 'ruby-debug'
5
5
  # Debugger.start
6
6
 
7
+ CITY_DB = ENV.fetch("CITY", '/usr/local/GeoIP/share/GeoIP/GeoLiteCity.dat')
8
+ ORG_DB = ENV.fetch("ORG", '/usr/local/GeoIP/share/GeoIP/GeoIPOrg.dat')
9
+
7
10
  class Test::Unit::TestCase
8
11
 
9
12
  def assert_look_up(db, addr, field, value)
@@ -69,7 +72,7 @@ class GeoIPCityTest < Test::Unit::TestCase
69
72
 
70
73
  def setup
71
74
  ## Change me!
72
- @dbfile = '/usr/local/GeoIP/share/GeoIP/GeoLiteCity.dat'
75
+ @dbfile = CITY_DB
73
76
  end
74
77
 
75
78
  def test_construction_default
@@ -82,43 +85,48 @@ class GeoIPCityTest < Test::Unit::TestCase
82
85
  h = db.look_up('24.24.24.24')
83
86
  #debugger
84
87
  assert_kind_of Hash, h
85
- assert_equal 'Jamaica', h[:city]
88
+ assert_equal 'New York', h[:city]
86
89
  assert_equal 'United States', h[:country_name]
87
90
  end
88
91
 
89
92
  def test_construction_index
90
93
  db = GeoIP::City.new(@dbfile, :index)
91
- assert_look_up(db, '24.24.24.24', :city, 'Jamaica')
94
+ assert_look_up(db, '24.24.24.24', :city, 'New York')
92
95
  end
93
96
 
94
97
  def test_construction_filesystem
95
98
  db = GeoIP::City.new(@dbfile, :filesystem)
96
- assert_look_up(db, '24.24.24.24', :city, 'Jamaica')
99
+ assert_look_up(db, '24.24.24.24', :city, 'New York')
97
100
  end
98
101
 
99
102
  def test_construction_memory
100
103
  db = GeoIP::City.new(@dbfile, :memory)
101
- assert_look_up(db, '24.24.24.24', :city, 'Jamaica')
104
+ assert_look_up(db, '24.24.24.24', :city, 'New York')
102
105
  end
103
106
 
104
107
  def test_construction_filesystem_check
105
108
  db = GeoIP::City.new(@dbfile, :filesystem, true)
106
- assert_look_up(db, '24.24.24.24', :city, 'Jamaica')
109
+ assert_look_up(db, '24.24.24.24', :city, 'New York')
107
110
  end
108
111
 
109
112
  def test_bad_db_file
110
113
  assert_raises Errno::ENOENT do
111
- GeoIP::City.new('/blah')
114
+ GeoIP::City.new('/supposed-to-fail')
112
115
  end
113
116
  end
114
117
 
118
+ def test_character_encoding_converted_to_utf8_first
119
+ db = GeoIP::City.new(@dbfile, :filesystem, true)
120
+ assert_look_up(db, '201.85.50.148', :city, 'São Paulo')
121
+ end
122
+
115
123
  end
116
124
 
117
125
  class GeoIPOrgTest < Test::Unit::TestCase
118
126
 
119
127
  def setup
120
128
  ## Change me!
121
- @dbfile = '/usr/local/GeoIP/share/GeoIP/GeoIPOrg.dat'
129
+ @dbfile = ORG_DB
122
130
  end
123
131
 
124
132
  def test_construction_default
@@ -155,7 +163,7 @@ class GeoIPOrgTest < Test::Unit::TestCase
155
163
 
156
164
  def test_bad_db_file
157
165
  assert_raises Errno::ENOENT do
158
- GeoIP::Organization.new('/blah')
166
+ GeoIP::Organization.new('/supposed-to-fail')
159
167
  end
160
168
  end
161
169
 
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geoip-c
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ hash: 1
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 7
9
+ - 1
10
+ version: 0.7.1
5
11
  platform: ruby
6
12
  authors:
7
13
  - Ryah Dahl
@@ -13,7 +19,7 @@ autorequire:
13
19
  bindir: bin
14
20
  cert_chain: []
15
21
 
16
- date: 2009-12-29 00:00:00 -05:00
22
+ date: 2011-03-08 00:00:00 -05:00
17
23
  default_executable:
18
24
  dependencies: []
19
25
 
@@ -41,21 +47,27 @@ rdoc_options: []
41
47
  require_paths:
42
48
  - .
43
49
  required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
44
51
  requirements:
45
52
  - - ">="
46
53
  - !ruby/object:Gem::Version
54
+ hash: 3
55
+ segments:
56
+ - 0
47
57
  version: "0"
48
- version:
49
58
  required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
50
60
  requirements:
51
61
  - - ">="
52
62
  - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
53
66
  version: "0"
54
- version:
55
67
  requirements: []
56
68
 
57
69
  rubyforge_project:
58
- rubygems_version: 1.3.5
70
+ rubygems_version: 1.4.2
59
71
  signing_key:
60
72
  specification_version: 3
61
73
  summary: A Binding to the GeoIP C library