geoip_city19 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,67 @@
1
+ = What?
2
+ This is a library which provides a single function. The function takes as
3
+ input an IP address and it outputs a hash containing best-guess geographical
4
+ information (like city, country, latitude, and longitude).
5
+
6
+ Actually this is only a Ruby binding to a C library which provides this
7
+ function. Also, you must download a large binary database with all this
8
+ mapping information. It is kindly provided free of charge by MaxMind.com.
9
+
10
+ = Usage
11
+ require 'geoip_city'
12
+ db = GeoIPCity::Database.new('/opt/GeoIP/share/GeoIP/GeoLiteCity.dat')
13
+ result = db.look_up('24.24.24.24')
14
+ p result
15
+ # => {:city=>"Ithaca",
16
+ # :latitude=>42.4277992248535,
17
+ # :longitude=>-76.4981994628906,
18
+ # :country_code3=>"USA",
19
+ # :country_code=>"US",
20
+ # :country_name=>"United States",
21
+ # :dma_code=>555,
22
+ # :area_code=>607,
23
+ # :region=>"NY" }
24
+
25
+ There are two optional arguments to database initializer.
26
+ The first argument is the filename of the GeoIPCity.dat file
27
+ The second argument (optional) is to specify how GeoIP should
28
+ keep the database in memory. There are three possibilities
29
+ * :filesystem -- read database from filesystem, uses least memory.
30
+ * :index -- the most frequently accessed index portion of the database, resulting in faster lookups than :filesystem, but less memory usage than :memory.
31
+ * :memory -- load database into memory, faster performance but uses more memory.
32
+ (default)
33
+
34
+ The third argument is boolean and decides whether the system should reload the database if changes are made to the dat file. (You probably don't need this. Default: false.)
35
+
36
+ For example
37
+
38
+ GeoIPCity::Database.new(dbfile, :filesystem, true)
39
+
40
+ = Install
41
+ Some variation of the following should work.
42
+
43
+ 1. Install the GeoCity C library. You can get it from here
44
+ http://www.maxmind.com/app/c
45
+ For example, I like to install mine in /opt/GeoIP
46
+ so I do this:
47
+ tar -zxvf GeoIP-1.4.3.tar.gz
48
+ cd GeoIP-1.4.3
49
+ ./configure --prefix=/opt/GeoIP
50
+ make && sudo make install
51
+
52
+ 2. Now install the geoip_city gem
53
+ sudo gem install geoip_city -- --with-geoip-dir=/opt/GeoIP
54
+
55
+ 3. Download the GeoLite City database file in binary format from:
56
+ http://www.maxmind.com/app/geolitecity
57
+ Maybe this direct link will work:
58
+ http://www.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
59
+ I put this file in
60
+ /opt/GeoIP/share/GeoIP/GeoLiteCity.dat
61
+
62
+ 4. Use it!
63
+
64
+ = License
65
+ Copyright (C) 2007 Ryan Dahl (ry@tinyclouds.org)
66
+
67
+ I give permission for you to do whatever you'd like to do with this software.
@@ -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,132 @@
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(StringValuePtr(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->city)
83
+ rb_hash_sset(hash, "city", rb_str_new2(record->city));
84
+ if(record->postal_code)
85
+ rb_hash_sset(hash, "postal_code", rb_str_new2(record->postal_code));
86
+ if(record->latitude)
87
+ rb_hash_sset(hash, "latitude", rb_float_new((double)record->latitude));
88
+ if(record->longitude)
89
+ rb_hash_sset(hash, "longitude", rb_float_new((double)record->longitude));
90
+ if(record->dma_code)
91
+ rb_hash_sset(hash, "dma_code", INT2NUM(record->dma_code));
92
+ if(record->area_code)
93
+ rb_hash_sset(hash, "area_code", INT2NUM(record->area_code));
94
+
95
+ return hash;
96
+ }
97
+
98
+ /* Pass this function an IP address as a string, it will return a hash
99
+ * containing all the information that the database knows about the IP
100
+ * db.look_up('24.24.24.24')
101
+ * => {:city=>"Ithaca", :latitude=>42.4277992248535,
102
+ * :country_code=>"US", :longitude=>-76.4981994628906,
103
+ * :country_code3=>"USA", :dma_code=>555,
104
+ * :country_name=>"United States", :area_code=>607,
105
+ * :region=>"NY"}
106
+ */
107
+ VALUE rb_geoip_look_up(VALUE self, VALUE addr) {
108
+ GeoIP *gi;
109
+ GeoIPRecord *record = NULL;
110
+ VALUE hash = Qnil;
111
+
112
+ Check_Type(addr, T_STRING);
113
+ Data_Get_Struct(self, GeoIP, gi);
114
+ if(record = GeoIP_record_by_addr(gi, StringValuePtr(addr))) {
115
+ hash = rb_record_to_hash(record);
116
+ GeoIPRecord_delete(record);
117
+ }
118
+ return hash;
119
+ }
120
+
121
+ void Init_geoip_city ()
122
+ {
123
+ VALUE mGeoIP = rb_define_module("GeoIPCity");
124
+
125
+ rb_geoip_memory = ID2SYM(rb_intern("memory"));
126
+ rb_geoip_filesystem = ID2SYM(rb_intern("filesystem"));
127
+ rb_geoip_index = ID2SYM(rb_intern("index"));
128
+
129
+ cDB = rb_define_class_under(mGeoIP, "Database", rb_cObject);
130
+ rb_define_singleton_method(cDB, "new", rb_geoip_new, -1);
131
+ rb_define_method(cDB, "look_up", rb_geoip_look_up, 1);
132
+ }
data/lib/geoip_city.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "geoip_city.bundle"
2
+ require "geoip_city/version"
3
+
4
+ module GeoipCity
5
+ # Your code goes here...
6
+ end
@@ -0,0 +1,3 @@
1
+ module GeoipCity
2
+ VERSION = "2.0.1"
3
+ end
data/test/test.rb ADDED
@@ -0,0 +1,57 @@
1
+ require "bundler/setup"
2
+
3
+ require "test/unit"
4
+ require "geoip_city"
5
+
6
+ class GeoIPTest < Test::Unit::TestCase
7
+
8
+ def setup
9
+ ## Change me!
10
+ @dbfile = 'data/GeoLiteCity.dat'
11
+ end
12
+
13
+
14
+ def test_construction_default
15
+ db = GeoIPCity::Database.new(@dbfile)
16
+
17
+ assert_raises TypeError do
18
+ db.look_up(nil)
19
+ end
20
+
21
+ h = db.look_up('24.24.24.24')
22
+ assert_kind_of Hash, h
23
+ assert_equal 'East Syracuse', h[:city]
24
+ assert_equal 'United States', h[:country_name]
25
+ end
26
+
27
+ def test_construction_index
28
+ db = GeoIPCity::Database.new(@dbfile, :index)
29
+ h = db.look_up('24.24.24.24')
30
+ assert_equal 'East Syracuse', h[:city]
31
+ end
32
+
33
+ def test_construction_filesystem
34
+ db = GeoIPCity::Database.new(@dbfile, :filesystem)
35
+ h = db.look_up('24.24.24.24')
36
+ assert_equal 'East Syracuse', h[:city]
37
+ end
38
+
39
+ def test_construction_memory
40
+ db = GeoIPCity::Database.new(@dbfile, :memory)
41
+ h = db.look_up('24.24.24.24')
42
+ assert_equal 'East Syracuse', h[:city]
43
+ end
44
+
45
+ def test_construction_filesystem_check
46
+ db = GeoIPCity::Database.new(@dbfile, :filesystem, true)
47
+ h = db.look_up('24.24.24.24')
48
+ assert_equal 'East Syracuse', h[:city]
49
+ end
50
+
51
+ def test_bad_db_file
52
+ assert_raises Errno::ENOENT do
53
+ GeoIPCity::Database.new('/blah')
54
+ end
55
+ end
56
+
57
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: geoip_city19
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - ry dahl
9
+ - Chris Kraybill
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2013-02-16 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rake-compiler
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ description: Ruby 1.9 library provides a binding to the C library for, given an IP
32
+ address, return a JSON hash of best-guess geographical data
33
+ email:
34
+ - ry@tinyclouds.org
35
+ - ckraybill@gmail.com
36
+ executables: []
37
+ extensions:
38
+ - ext/geoip_city/extconf.rb
39
+ extra_rdoc_files:
40
+ - README
41
+ - ext/geoip_city/geoip_city.c
42
+ files:
43
+ - lib/geoip_city/version.rb
44
+ - lib/geoip_city.rb
45
+ - ext/geoip_city/geoip_city.c
46
+ - ext/geoip_city/extconf.rb
47
+ - README
48
+ - test/test.rb
49
+ homepage: https://github.com/ckraybill/geoip_city
50
+ licenses: []
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ segments:
62
+ - 0
63
+ hash: 3476083723586382020
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ segments:
71
+ - 0
72
+ hash: 3476083723586382020
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 1.8.23
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: A Binding to the GeoIP C library for Ruby 19
79
+ test_files:
80
+ - test/test.rb