acamargo-geoip_city 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/README.md +110 -0
  2. data/Rakefile +47 -0
  3. data/extconf.rb +9 -0
  4. data/geoip_city.c +134 -0
  5. data/test.rb +61 -0
  6. metadata +59 -0
data/README.md ADDED
@@ -0,0 +1,110 @@
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
+ # => {:country_code3=>"USA",
22
+ # :latitude=>40.6763000488281,
23
+ # :country_name=>"United States",
24
+ # :longitude=>-73.7751998901367,
25
+ # :region=>"NY",
26
+ # :dma_code=>501,
27
+ # :region_name=>"New York",
28
+ # :area_code=>718,
29
+ # :city=>"Jamaica",
30
+ # :country_code=>"US",
31
+ # :postal_code=>"11434"}
32
+
33
+ There are arguments to database initializer.
34
+
35
+ 1. The first argument is the filename of the GeoIPCity.dat file
36
+
37
+ 2. The second argument (optional) is to specify how GeoIP should
38
+ keep the database in memory. There are three possibilities
39
+
40
+ * `:filesystem` -- Read database from filesystem, uses least memory.
41
+
42
+ * `:index` -- The most frequently accessed index portion of the
43
+ database, resulting in faster lookups than :filesystem, but less
44
+ memory usage than :memory.
45
+
46
+ * `:memory` -- (Default.) Load database into memory, faster performance but uses more memory.
47
+
48
+ 3. The third argument is boolean and decides whether the system should
49
+ reload the database if changes are made to the dat file. (You probably
50
+ don't need this. Default: false.)
51
+
52
+ For example
53
+
54
+ GeoIPCity::Database.new(dbfile, :filesystem, true)
55
+
56
+
57
+ Install
58
+ -------
59
+
60
+ Some variation of the following should work.
61
+
62
+ 1. Install the GeoCity C library. You can get it from
63
+ [maxmind](http://www.maxmind.com/app/c).
64
+ For example, I like to install mine in `/opt/GeoIP`, so I do this:
65
+
66
+ tar -zxvf GeoIP-1.4.3.tar.gz
67
+
68
+ cd GeoIP-1.4.3
69
+
70
+ ./configure --prefix=/opt/GeoIP
71
+
72
+ make && sudo make install
73
+
74
+ 2. Now install the `geoip_city` gem
75
+
76
+ sudo gem install acamargo-geoip_city -s http://gemcutter.org -- --with-geoip-dir=/opt/GeoIP
77
+
78
+ 3. Download the GeoLite City database file in binary format at http://www.maxmind.com/app/geolitecity
79
+ Maybe this [direct link](http://www.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz) will work.
80
+ I put this file in
81
+
82
+ /opt/GeoIP/share/GeoIP/GeoLiteCity.dat
83
+
84
+ 4. Use it!
85
+
86
+ Hints
87
+ -----
88
+
89
+ 1. Might need to set
90
+
91
+ export ARCHFLAGS="-arch i386"
92
+
93
+ to be able to compile the gem.
94
+
95
+ 2. You might find [this shell
96
+ script](http://github.com/grimen/my_shell_scripts/blob/8cf04cb6829e68a47f2d6f9d9e057766ea72beb4/install_geoip-city.sh)
97
+ helpful to install the C library.
98
+
99
+ Links
100
+ -----
101
+
102
+ [rdocs](http://geoip-city.rubyforge.org/)
103
+
104
+ [git repo](https://github.com/ry/geoip-city/tree)
105
+
106
+ License
107
+ -------
108
+ Copyright (C) 2007--2009 Ryan Dahl (ry@tinyclouds.org)
109
+
110
+ I give permission for you to do whatever you'd like to do with this software.
data/Rakefile ADDED
@@ -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
data/extconf.rb ADDED
@@ -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
data/geoip_city.c ADDED
@@ -0,0 +1,134 @@
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
+ static VALUE cDB;
12
+ static VALUE rb_geoip_memory;
13
+ static VALUE rb_geoip_filesystem;
14
+ static VALUE rb_geoip_index;
15
+
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
+ {
31
+ GeoIP *gi;
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
+ }
53
+
54
+ if(RTEST(check_cache)) flag |= GEOIP_CHECK_CACHE;
55
+
56
+ if(gi = GeoIP_open(STR2CSTR(filename), flag)) {
57
+ database = Data_Wrap_Struct(cDB, 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
+ /* helper */
66
+ void rb_hash_sset(VALUE hash, const char *str, VALUE v) {
67
+ rb_hash_aset(hash, ID2SYM(rb_intern(str)), v);
68
+ }
69
+
70
+ VALUE rb_record_to_hash (GeoIPRecord *record)
71
+ {
72
+ VALUE hash = rb_hash_new();
73
+
74
+ if(record->country_code)
75
+ rb_hash_sset(hash, "country_code", rb_str_new2(record->country_code));
76
+ if(record->country_code3)
77
+ rb_hash_sset(hash, "country_code3", rb_str_new2(record->country_code3));
78
+ if(record->country_name)
79
+ rb_hash_sset(hash, "country_name", rb_str_new2(record->country_name));
80
+ if(record->region)
81
+ rb_hash_sset(hash, "region", rb_str_new2(record->region));
82
+ if(record->country_code && record->region)
83
+ rb_hash_sset(hash, "region_name", rb_str_new2(GeoIP_region_name_by_code(record->country_code, record->region)));
84
+ if(record->city)
85
+ rb_hash_sset(hash, "city", rb_str_new2(record->city));
86
+ if(record->postal_code)
87
+ rb_hash_sset(hash, "postal_code", rb_str_new2(record->postal_code));
88
+ if(record->latitude)
89
+ rb_hash_sset(hash, "latitude", rb_float_new((double)record->latitude));
90
+ if(record->longitude)
91
+ rb_hash_sset(hash, "longitude", rb_float_new((double)record->longitude));
92
+ if(record->dma_code)
93
+ rb_hash_sset(hash, "dma_code", INT2NUM(record->dma_code));
94
+ if(record->area_code)
95
+ rb_hash_sset(hash, "area_code", INT2NUM(record->area_code));
96
+
97
+ return hash;
98
+ }
99
+
100
+ /* Pass this function an IP address as a string, it will return a hash
101
+ * containing all the information that the database knows about the IP
102
+ * db.look_up('24.24.24.24')
103
+ * => {:city=>"Ithaca", :latitude=>42.4277992248535,
104
+ * :country_code=>"US", :longitude=>-76.4981994628906,
105
+ * :country_code3=>"USA", :dma_code=>555,
106
+ * :country_name=>"United States", :area_code=>607,
107
+ * :region=>"NY", :region_name=>"New York"}
108
+ */
109
+ VALUE rb_geoip_look_up(VALUE self, VALUE addr) {
110
+ GeoIP *gi;
111
+ GeoIPRecord *record = NULL;
112
+ VALUE hash = Qnil;
113
+
114
+ Check_Type(addr, T_STRING);
115
+ Data_Get_Struct(self, GeoIP, gi);
116
+ if(record = GeoIP_record_by_addr(gi, STR2CSTR(addr))) {
117
+ hash = rb_record_to_hash(record);
118
+ GeoIPRecord_delete(record);
119
+ }
120
+ return hash;
121
+ }
122
+
123
+ void Init_geoip_city ()
124
+ {
125
+ VALUE mGeoIP = rb_define_module("GeoIPCity");
126
+
127
+ rb_geoip_memory = ID2SYM(rb_intern("memory"));
128
+ rb_geoip_filesystem = ID2SYM(rb_intern("filesystem"));
129
+ rb_geoip_index = ID2SYM(rb_intern("index"));
130
+
131
+ cDB = rb_define_class_under(mGeoIP, "Database", rb_cObject);
132
+ rb_define_singleton_method(cDB, "new", rb_geoip_new, -1);
133
+ rb_define_method(cDB, "look_up", rb_geoip_look_up, 1);
134
+ }
data/test.rb ADDED
@@ -0,0 +1,61 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/geoip_city'
3
+ require 'rubygems'
4
+ require 'ruby-debug'
5
+ Debugger.start
6
+
7
+ class GeoIPTest < Test::Unit::TestCase
8
+
9
+ def setup
10
+ ## Change me!
11
+ @dbfile = '/opt/GeoIP-1.4.2/share/GeoIP/GeoLiteCity.dat'
12
+ end
13
+
14
+
15
+ def test_construction_default
16
+ db = GeoIPCity::Database.new(@dbfile)
17
+
18
+ assert_raises TypeError do
19
+ db.look_up(nil)
20
+ end
21
+
22
+ h = db.look_up('24.24.24.24')
23
+ #debugger
24
+ assert_kind_of Hash, h
25
+ assert_equal 'Jamaica', h[:city]
26
+ assert_equal 'New York', h[:region_name]
27
+ assert_equal 'NY', h[:region]
28
+ assert_equal 'United States', h[:country_name]
29
+ end
30
+
31
+ def test_construction_index
32
+ db = GeoIPCity::Database.new(@dbfile, :index)
33
+ h = db.look_up('24.24.24.24')
34
+ assert_equal 'Jamaica', h[:city]
35
+ end
36
+
37
+ def test_construction_filesystem
38
+ db = GeoIPCity::Database.new(@dbfile, :filesystem)
39
+ h = db.look_up('24.24.24.24')
40
+ assert_equal 'Jamaica', h[:city]
41
+ end
42
+
43
+ def test_construction_memory
44
+ db = GeoIPCity::Database.new(@dbfile, :memory)
45
+ h = db.look_up('24.24.24.24')
46
+ assert_equal 'Jamaica', h[:city]
47
+ end
48
+
49
+ def test_construction_filesystem_check
50
+ db = GeoIPCity::Database.new(@dbfile, :filesystem, true)
51
+ h = db.look_up('24.24.24.24')
52
+ assert_equal 'Jamaica', h[:city]
53
+ end
54
+
55
+ def test_bad_db_file
56
+ assert_raises Errno::ENOENT do
57
+ GeoIPCity::Database.new('/blah')
58
+ end
59
+ end
60
+
61
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acamargo-geoip_city
3
+ version: !ruby/object:Gem::Version
4
+ version: "1.0"
5
+ platform: ruby
6
+ authors:
7
+ - "Andr\xC3\xA9 Camargo"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-03 00:00:00 -02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Generic GeoIP lookup tool. Based on the geoip_city RubyGem by Ryan Dahl
17
+ email: andre@boaideia.inf.br
18
+ executables: []
19
+
20
+ extensions:
21
+ - extconf.rb
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - extconf.rb
26
+ - geoip_city.c
27
+ - Rakefile
28
+ - README.md
29
+ - test.rb
30
+ has_rdoc: true
31
+ homepage: http://github.com/acamargo/geoip-city
32
+ licenses: []
33
+
34
+ post_install_message:
35
+ rdoc_options: []
36
+
37
+ require_paths:
38
+ - .
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ requirements: []
52
+
53
+ rubyforge_project:
54
+ rubygems_version: 1.3.5
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: A Ruby binding to the GeoIP C Library
58
+ test_files:
59
+ - test.rb