cdamian-geoip_city 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,108 @@
1
+ Ruby GeoIPCity 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_city'
18
+ db = GeoIPCity::Database.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
+ GeoIPCity::Database.new(dbfile, :filesystem, true)
53
+
54
+
55
+ Install
56
+ -------
57
+
58
+ Some variation of the following should work.
59
+
60
+ 1. Install the GeoCity C library. You can get it from
61
+ [maxmind](http://www.maxmind.com/app/c).
62
+ For example, I like to install mine in `/opt/GeoIP`, so I do this:
63
+
64
+ tar -zxvf GeoIP-1.4.3.tar.gz
65
+
66
+ cd GeoIP-1.4.3
67
+
68
+ ./configure --prefix=/opt/GeoIP
69
+
70
+ make && sudo make install
71
+
72
+ 2. Now install the `geoip_city` gem
73
+
74
+ sudo gem install geoip_city -- --with-geoip-dir=/opt/GeoIP
75
+
76
+ 3. Download the GeoLite City database file in binary format at http://www.maxmind.com/app/geolitecity
77
+ Maybe this [direct link](http://www.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz) will work.
78
+ I put this file in
79
+
80
+ /opt/GeoIP/share/GeoIP/GeoLiteCity.dat
81
+
82
+ 4. Use it!
83
+
84
+ Hints
85
+ -----
86
+
87
+ 1. Might need to set
88
+
89
+ export ARCHFLAGS="-arch i386"
90
+
91
+ to be able to compile the gem.
92
+
93
+ 2. You might find [this shell
94
+ script](http://github.com/grimen/my_shell_scripts/blob/8cf04cb6829e68a47f2d6f9d9e057766ea72beb4/install_geoip-city.sh)
95
+ helpful to install the C library.
96
+
97
+ Links
98
+ -----
99
+
100
+ [rdocs](http://geoip-city.rubyforge.org/)
101
+
102
+ [git repo](https://github.com/ry/geoip-city/tree)
103
+
104
+ License
105
+ -------
106
+ Copyright (C) 2007--2009 Ryan Dahl (ry@tinyclouds.org)
107
+
108
+ I give permission for you to do whatever you'd like to do with this software.
@@ -0,0 +1,47 @@
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_city.{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_city.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_city'
25
+ s.author = 'ry dahl'
26
+ s.email = 'ry@tinyclouds.org'
27
+ s.version = "0.2.0"
28
+ s.summary = "A Binding to the GeoIP C library"
29
+ s.homepage = "http://geoip_city.rubyforge.org"
30
+ s.files = FileList['Rakefile', '*.rb', '*.c', 'README*']
31
+ s.test_files = 'test.rb'
32
+ s.extensions = 'extconf.rb'
33
+ s.require_path = '.'
34
+ end
35
+
36
+ Rake::GemPackageTask.new(spec) do |p|
37
+ p.need_tar = true
38
+ p.gem_spec = spec
39
+ end
40
+
41
+ desc 'compile the extension'
42
+ task(:compile => 'Makefile') { sh 'make' }
43
+ file('Makefile' => "geoip_city.c") { ruby 'extconf.rb' }
44
+
45
+ task(:webpage) do
46
+ sh 'scp -r doc/* rydahl@rubyforge.org:/var/www/gforge-projects/geoip-city/'
47
+ end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{cdamian-geoip_city}
5
+ s.version = "0.2.2"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["ry dahl"]
9
+ s.date = %q{2008-03-04}
10
+ s.email = %q{ry@tinyclouds.org}
11
+ s.extensions = ["extconf.rb"]
12
+ s.files = Dir.glob('**/*')
13
+ s.homepage = %q{http://github.com/ry/geoip-city}
14
+ s.require_paths = ["."]
15
+ s.rubygems_version = %q{1.6.2}
16
+ s.summary = %q{A Binding to the GeoIP C library}
17
+ s.test_files = ["test.rb"]
18
+
19
+ if s.respond_to? :specification_version then
20
+ s.specification_version = 2
21
+
22
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
23
+ else
24
+ end
25
+ else
26
+ end
27
+ end
@@ -0,0 +1,9 @@
1
+ require 'mkmf'
2
+
3
+ dir_config("geoip")
4
+
5
+ if have_library('GeoIP', 'GeoIP_record_by_ipnum') and have_header('GeoIPCity.h')
6
+ create_makefile('geoip_city')
7
+ else
8
+ abort("you must have geoip c library installed!")
9
+ end
@@ -0,0 +1,178 @@
1
+ /* ry dahl <ry@tinyclouds.org> May 21, 2007 */
2
+ /* This program is free software. It comes without any warranty, to
3
+ * the extent permitted by applicable law. You can redistribute it
4
+ * and/or modify it under the terms of the Do What The Fuck You Want
5
+ * To Public License, Version 2, as published by Sam Hocevar. See
6
+ * http://sam.zoy.org/wtfpl/COPYING for more details. */
7
+ #include <ruby.h>
8
+ #include <GeoIP.h>
9
+ #include <GeoIPCity.h>
10
+
11
+ #ifdef HAVE_RUBY_ENCODING_H
12
+ #include <ruby/encoding.h>
13
+ static rb_encoding *utf8Encoding;
14
+ #endif
15
+
16
+ static VALUE cDB;
17
+ static VALUE rb_geoip_memory;
18
+ static VALUE rb_geoip_filesystem;
19
+ static VALUE rb_geoip_index;
20
+
21
+ #ifdef HAVE_RUBY_ENCODING_H
22
+ #define ASSOCIATE_ENCODING(str) \
23
+ rb_enc_associate(str, utf8Encoding); \
24
+ if (default_internal_enc) { \
25
+ str = rb_str_export_to_enc(str, default_internal_enc); \
26
+ }
27
+ #else
28
+ #define ASSOCIATE_ENCODING(str)
29
+ #endif
30
+
31
+ /* The first argument is the filename of the GeoIPCity.dat file
32
+ * load_option = :standard, :index, or :memory. default :memory
33
+ * check_cache = true or false. default false
34
+ *
35
+ * filesystem: read database from filesystem, uses least memory.
36
+ *
37
+ * index: the most frequently accessed index portion of the database,
38
+ * resulting in faster lookups than :filesystem, but less memory usage than
39
+ * :memory.
40
+ *
41
+ * memory: load database into memory, faster performance but uses more
42
+ * memory.
43
+ */
44
+ static VALUE rb_geoip_new(int argc, VALUE *argv, VALUE self)
45
+ {
46
+ GeoIP *gi;
47
+ VALUE database = Qnil;
48
+ VALUE filename, load_option = Qnil, check_cache = Qnil;
49
+ int flag;
50
+
51
+ rb_scan_args(argc, argv, "12", &filename, &load_option, &check_cache);
52
+ if(NIL_P(load_option))
53
+ load_option = rb_geoip_memory;
54
+ if(NIL_P(check_cache))
55
+ check_cache = Qfalse;
56
+ Check_Type(load_option, T_SYMBOL);
57
+
58
+ if(load_option == rb_geoip_memory) {
59
+ flag = GEOIP_MEMORY_CACHE;
60
+ } else if(load_option == rb_geoip_filesystem) {
61
+ flag = GEOIP_STANDARD;
62
+ } else if(load_option == rb_geoip_index) {
63
+ flag = GEOIP_INDEX_CACHE;
64
+ } else {
65
+ rb_raise(rb_eTypeError, "the second option must be :memory, :filesystem, or :index");
66
+ return Qnil;
67
+ }
68
+
69
+ if(RTEST(check_cache)) flag |= GEOIP_CHECK_CACHE;
70
+
71
+ if(gi = GeoIP_open(StringValuePtr(filename), flag)) {
72
+ database = Data_Wrap_Struct(cDB, 0, GeoIP_delete, gi);
73
+ rb_obj_call_init(database, 0, 0);
74
+ } else {
75
+ rb_sys_fail("Problem opening database");
76
+ }
77
+ return database;
78
+ }
79
+
80
+ /* helper */
81
+ void rb_hash_sset(VALUE hash, const char *str, VALUE v) {
82
+ rb_hash_aset(hash, ID2SYM(rb_intern(str)), v);
83
+ }
84
+
85
+ VALUE rb_record_to_hash (GeoIPRecord *record)
86
+ {
87
+ VALUE hash = rb_hash_new();
88
+ #ifdef HAVE_RUBY_ENCODING_H
89
+ rb_encoding *default_internal_enc = rb_default_internal_encoding();
90
+ #endif
91
+
92
+ if(record->country_code) {
93
+ VALUE str = rb_str_new2(record->country_code);
94
+ ASSOCIATE_ENCODING(str);
95
+ rb_hash_sset(hash, "country_code", str);
96
+ }
97
+
98
+ if(record->country_code3) {
99
+ VALUE str = rb_str_new2(record->country_code3);
100
+ ASSOCIATE_ENCODING(str);
101
+ rb_hash_sset(hash, "country_code3", str);
102
+ }
103
+
104
+ if(record->country_name) {
105
+ VALUE str = rb_str_new2(record->country_name);
106
+ ASSOCIATE_ENCODING(str);
107
+ rb_hash_sset(hash, "country_name", str);
108
+ }
109
+
110
+ if(record->region) {
111
+ VALUE str = rb_str_new2(record->region);
112
+ ASSOCIATE_ENCODING(str);
113
+ rb_hash_sset(hash, "region", str);
114
+ }
115
+
116
+ if(record->city) {
117
+ VALUE str = rb_str_new2(record->city);
118
+ ASSOCIATE_ENCODING(str);
119
+ rb_hash_sset(hash, "city", str);
120
+ }
121
+
122
+ if(record->postal_code) {
123
+ VALUE str = rb_str_new2(record->city);
124
+ ASSOCIATE_ENCODING(str);
125
+ rb_hash_sset(hash, "postal_code", rb_str_new2(record->postal_code));
126
+ }
127
+
128
+ if(record->latitude)
129
+ rb_hash_sset(hash, "latitude", rb_float_new((double)record->latitude));
130
+ if(record->longitude)
131
+ rb_hash_sset(hash, "longitude", rb_float_new((double)record->longitude));
132
+ if(record->dma_code)
133
+ rb_hash_sset(hash, "dma_code", INT2NUM(record->dma_code));
134
+ if(record->area_code)
135
+ rb_hash_sset(hash, "area_code", INT2NUM(record->area_code));
136
+
137
+ return hash;
138
+ }
139
+
140
+ /* Pass this function an IP address as a string, it will return a hash
141
+ * containing all the information that the database knows about the IP
142
+ * db.look_up('24.24.24.24')
143
+ * => {:city=>"Ithaca", :latitude=>42.4277992248535,
144
+ * :country_code=>"US", :longitude=>-76.4981994628906,
145
+ * :country_code3=>"USA", :dma_code=>555,
146
+ * :country_name=>"United States", :area_code=>607,
147
+ * :region=>"NY"}
148
+ */
149
+ VALUE rb_geoip_look_up(VALUE self, VALUE addr) {
150
+ GeoIP *gi;
151
+ GeoIPRecord *record = NULL;
152
+ VALUE hash = Qnil;
153
+
154
+ Check_Type(addr, T_STRING);
155
+ Data_Get_Struct(self, GeoIP, gi);
156
+ if(record = GeoIP_record_by_addr(gi, StringValuePtr(addr))) {
157
+ hash = rb_record_to_hash(record);
158
+ GeoIPRecord_delete(record);
159
+ }
160
+ return hash;
161
+ }
162
+
163
+ void Init_geoip_city ()
164
+ {
165
+ VALUE mGeoIP = rb_define_module("GeoIPCity");
166
+
167
+ rb_geoip_memory = ID2SYM(rb_intern("memory"));
168
+ rb_geoip_filesystem = ID2SYM(rb_intern("filesystem"));
169
+ rb_geoip_index = ID2SYM(rb_intern("index"));
170
+
171
+ cDB = rb_define_class_under(mGeoIP, "Database", rb_cObject);
172
+ rb_define_singleton_method(cDB, "new", rb_geoip_new, -1);
173
+ rb_define_method(cDB, "look_up", rb_geoip_look_up, 1);
174
+
175
+ #ifdef HAVE_RUBY_ENCODING_H
176
+ utf8Encoding = rb_utf8_encoding();
177
+ #endif
178
+ }
data/test.rb ADDED
@@ -0,0 +1,70 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/geoip_city'
3
+ require 'rubygems'
4
+
5
+ class GeoIPTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ @dbfile = ENV['GEOIP_DB'] || 'GeoIPCity.dat'
9
+ end
10
+
11
+
12
+ def test_construction_default
13
+ db = GeoIPCity::Database.new(@dbfile)
14
+
15
+ assert_raises TypeError do
16
+ db.look_up(nil)
17
+ end
18
+
19
+ h = db.look_up('24.24.24.24')
20
+
21
+ assert_kind_of Hash, h
22
+ assert_equal 'Syracuse', h[:city]
23
+ assert_equal 'United States', h[:country_name]
24
+ end
25
+
26
+ def test_construction_index
27
+ db = GeoIPCity::Database.new(@dbfile, :index)
28
+ h = db.look_up('24.24.24.24')
29
+ assert_equal 'Syracuse', h[:city]
30
+ end
31
+
32
+ def test_construction_filesystem
33
+ db = GeoIPCity::Database.new(@dbfile, :filesystem)
34
+ h = db.look_up('24.24.24.24')
35
+ assert_equal 'Syracuse', h[:city]
36
+ end
37
+
38
+ def test_construction_memory
39
+ db = GeoIPCity::Database.new(@dbfile, :memory)
40
+ h = db.look_up('24.24.24.24')
41
+ assert_equal 'Syracuse', h[:city]
42
+ end
43
+
44
+ def test_construction_filesystem_check
45
+ db = GeoIPCity::Database.new(@dbfile, :filesystem, true)
46
+ h = db.look_up('24.24.24.24')
47
+ assert_equal 'Syracuse', h[:city]
48
+ end
49
+
50
+ def test_bad_db_file
51
+ assert_raises Errno::ENOENT do
52
+ GeoIPCity::Database.new('/blah')
53
+ end
54
+ end
55
+
56
+ if defined? Encoding
57
+ def test_string_encoding_support
58
+ Encoding.default_internal = nil
59
+ db = GeoIPCity::Database.new(@dbfile, :index)
60
+ h = db.look_up('24.24.24.24')
61
+ assert_equal Encoding.find('utf-8'), h[:city].encoding
62
+
63
+ Encoding.default_internal = Encoding.find('us-ascii')
64
+ db2 = GeoIPCity::Database.new(@dbfile, :index)
65
+ h2 = db2.look_up('24.24.24.24')
66
+ assert_equal Encoding.find('us-ascii'), h2[:city].encoding
67
+ end
68
+ end
69
+
70
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cdamian-geoip_city
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.2.2
6
+ platform: ruby
7
+ authors:
8
+ - ry dahl
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2008-03-04 00:00:00 -02:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description:
18
+ email: ry@tinyclouds.org
19
+ executables: []
20
+
21
+ extensions:
22
+ - extconf.rb
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - cdamian-geoip_city.gemspec
27
+ - Rakefile
28
+ - geoip_city.c
29
+ - README.md
30
+ - test.rb
31
+ - extconf.rb
32
+ has_rdoc: true
33
+ homepage: http://github.com/ry/geoip-city
34
+ licenses: []
35
+
36
+ post_install_message:
37
+ rdoc_options: []
38
+
39
+ require_paths:
40
+ - .
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ requirements: []
54
+
55
+ rubyforge_project:
56
+ rubygems_version: 1.6.2
57
+ signing_key:
58
+ specification_version: 2
59
+ summary: A Binding to the GeoIP C library
60
+ test_files:
61
+ - test.rb