jgeoip 0.0.6-java → 0.1.0-java
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Buildfile +1 -4
- data/README.md +3 -3
- data/jgeoip.gemspec +2 -2
- data/lib/jgeoip.rb +4 -7
- data/lib/jgeoip/jgeoip.rb +12 -0
- data/lib/jgeoip/location.rb +35 -0
- data/lib/jgeoip/version.rb +1 -1
- data/script/benchmark.rb +4 -4
- data/test/unit/jgeoip_test.rb +7 -1
- metadata +7 -8
- data/lib/java/jgeoip-0.0.6.jar +0 -0
- data/src/main/java/org/github/tobsch/jgeoip/JGeoIPLibrary.java +0 -88
data/Buildfile
CHANGED
@@ -13,13 +13,10 @@ define 'jgeoip' do
|
|
13
13
|
# add dependencies from jar
|
14
14
|
compile.with('org.kohsuke:geoip:jar:1.2.5')
|
15
15
|
|
16
|
-
# package our shiny little bidder jar
|
17
|
-
package :jar, :file => _("lib/java/jgeoip-#{JGeoIP::VERSION}.jar")
|
18
|
-
|
19
16
|
desc 'copy all dependent jars to lib folder'
|
20
17
|
task :copy_dependencies do
|
21
18
|
cp project.compile.dependencies.collect(&:to_s), project.path_to('lib/java')
|
22
19
|
end
|
23
20
|
|
24
|
-
task :setup => [ :clean, :
|
21
|
+
task :setup => [ :clean, :copy_dependencies ]
|
25
22
|
end
|
data/README.md
CHANGED
@@ -7,7 +7,7 @@ You can easily use it with the free GeoIP Library from maxmind.com aswell as the
|
|
7
7
|
|
8
8
|
p JGeoIP.new('/opt/MaxMind/GeoLiteCity.dat').city('github.com')
|
9
9
|
|
10
|
-
Will return a
|
10
|
+
Will return a location object with all the result properties and a #to_hash method
|
11
11
|
|
12
12
|
|
13
13
|
### speed?
|
@@ -17,7 +17,7 @@ Results are promising:
|
|
17
17
|
|
18
18
|
pure ruby GeoIP:
|
19
19
|
35.614000 0.000000 35.614000 ( 35.614000)
|
20
|
-
JGeoIP:
|
20
|
+
JGeoIP (Old Version, as extension, updating soon):
|
21
21
|
1.318000 0.000000 1.318000 ( 1.318000)
|
22
22
|
|
23
23
|
Please note that we did a warmup to give the JIT a chance.
|
@@ -25,6 +25,6 @@ The benchmark does 100k lookups.
|
|
25
25
|
Those results are Java 7, jruby-1.6.5 based. Looking forward to 1.7.0.
|
26
26
|
|
27
27
|
### TODO
|
28
|
-
*
|
28
|
+
* switch back to an extension and a proxy object: this is surely faster. but time is money at the moment
|
29
29
|
* integrate the other lookup methods: so far we're just able to do city lookups
|
30
30
|
* check all the license related stuff: the maxmind lib is LGPL, which would be okay for me
|
data/jgeoip.gemspec
CHANGED
@@ -7,8 +7,8 @@ Gem::Specification.new do |s|
|
|
7
7
|
s.authors = ["Tobias Schlottke"]
|
8
8
|
s.email = ["tobias.schlottke@gmail.com"]
|
9
9
|
s.homepage = "http://github.com/tobsch/jgeoip"
|
10
|
-
s.summary = 'Fast
|
11
|
-
s.description = 'This gem wraps maxminds original java library
|
10
|
+
s.summary = 'Fast jRuby library for Maxminds GeoIP Databases'
|
11
|
+
s.description = 'This gem wraps maxminds original java library, which should make things faster than using other pure ruby libraries.'
|
12
12
|
s.platform = 'java'
|
13
13
|
|
14
14
|
s.files = `git ls-files`.split("\n")
|
data/lib/jgeoip.rb
CHANGED
@@ -1,12 +1,9 @@
|
|
1
1
|
require 'java'
|
2
2
|
|
3
|
-
# load the version file
|
4
|
-
require File.expand_path('../jgeoip/version', __FILE__)
|
5
|
-
|
6
3
|
# Load the required jars
|
7
4
|
require File.expand_path('../java/geoip-1.2.5', __FILE__)
|
8
|
-
require File.expand_path("../java/jgeoip-#{JGeoIP::VERSION}", __FILE__)
|
9
5
|
|
10
|
-
#
|
11
|
-
|
12
|
-
|
6
|
+
# load the version file
|
7
|
+
require File.expand_path('../jgeoip/version', __FILE__)
|
8
|
+
require File.expand_path('../jgeoip/jgeoip', __FILE__)
|
9
|
+
require File.expand_path('../jgeoip/location', __FILE__)
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class JGeoIP
|
2
|
+
# Import the geoip jar
|
3
|
+
java_import 'com.maxmind.geoip.LookupService'
|
4
|
+
|
5
|
+
def initialize(database_path)
|
6
|
+
@cl = LookupService.new(database_path, LookupService::GEOIP_MEMORY_CACHE)
|
7
|
+
end
|
8
|
+
|
9
|
+
def city(search_string)
|
10
|
+
@cl.getLocation(search_string)
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
class Java::ComMaxmindGeoip::Location
|
2
|
+
# returns a list of all relevant keys
|
3
|
+
def keys
|
4
|
+
mapping.keys
|
5
|
+
end
|
6
|
+
|
7
|
+
# getter for all value
|
8
|
+
def [](key)
|
9
|
+
mapping.key?(key) ? self.send(mapping[key]) : nil
|
10
|
+
end
|
11
|
+
|
12
|
+
def inspect
|
13
|
+
to_hash.inspect
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_hash
|
17
|
+
mapping.inject({}) { |mem, map| mem[map.first] = self[map.last]; mem }
|
18
|
+
end
|
19
|
+
|
20
|
+
protected
|
21
|
+
def mapping
|
22
|
+
{
|
23
|
+
:city => :city,
|
24
|
+
:postal_code => :postalCode,
|
25
|
+
:country_code => :countryCode,
|
26
|
+
:country_name => :countryName,
|
27
|
+
:region => :region,
|
28
|
+
:latitude => :latitude,
|
29
|
+
:longitude => :longitude,
|
30
|
+
:dma_code => :dma_code,
|
31
|
+
:area_code => :area_code,
|
32
|
+
:metro_code => :metro_code
|
33
|
+
}
|
34
|
+
end
|
35
|
+
end
|
data/lib/jgeoip/version.rb
CHANGED
data/script/benchmark.rb
CHANGED
@@ -13,19 +13,19 @@ def benchmark(description, &block)
|
|
13
13
|
}
|
14
14
|
}
|
15
15
|
end
|
16
|
+
# check the JGeoIP
|
17
|
+
require File.expand_path('../../lib/jgeoip.rb', __FILE__)
|
16
18
|
|
17
19
|
# check the pure lib
|
18
20
|
require 'geoip'
|
19
21
|
db = GeoIP.new('/opt/MaxMind/GeoLiteCity.dat')
|
20
|
-
benchmark 'pure ruby GeoIP:'
|
22
|
+
benchmark 'pure ruby GeoIP:' do
|
21
23
|
result = db.city('github.com')
|
22
24
|
end
|
23
25
|
|
24
|
-
|
25
|
-
# check the JGeoIP
|
26
|
-
require File.expand_path('../../lib/jgeoip.rb', __FILE__)
|
27
26
|
db = JGeoIP.new('/opt/MaxMind/GeoLiteCity.dat')
|
28
27
|
benchmark 'JGeoIP:' do
|
29
28
|
result = db.city('github.com')
|
30
29
|
end
|
31
30
|
|
31
|
+
|
data/test/unit/jgeoip_test.rb
CHANGED
@@ -16,7 +16,13 @@ class JGeoIPTest < Test::Unit::TestCase
|
|
16
16
|
|
17
17
|
should 'find the city by ip too' do
|
18
18
|
result = @geo.city('207.97.227.239')
|
19
|
-
assert_equal '
|
19
|
+
assert_equal 'United States', result[:country_name]
|
20
|
+
end
|
21
|
+
|
22
|
+
should 'be able to return the result as a hash' do
|
23
|
+
result = @geo.city('85.183.18.35')
|
24
|
+
assert_kind_of Hash, result.to_hash
|
25
|
+
assert_equal 'Germany', result[:country_name]
|
20
26
|
end
|
21
27
|
|
22
28
|
should 'throw a clean exception if the ip was not found' do
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: jgeoip
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0
|
5
|
+
version: 0.1.0
|
6
6
|
platform: java
|
7
7
|
authors:
|
8
8
|
- Tobias Schlottke
|
@@ -58,7 +58,7 @@ dependencies:
|
|
58
58
|
requirement: *2106
|
59
59
|
prerelease: false
|
60
60
|
type: :development
|
61
|
-
description: This gem wraps maxminds original java library
|
61
|
+
description: This gem wraps maxminds original java library, which should make things faster than using other pure ruby libraries.
|
62
62
|
email:
|
63
63
|
- tobias.schlottke@gmail.com
|
64
64
|
executables: []
|
@@ -81,17 +81,16 @@ files:
|
|
81
81
|
amdlb2lwLmdlbXNwZWM=
|
82
82
|
- !binary |-
|
83
83
|
bGliL2phdmEvZ2VvaXAtMS4yLjUuamFy
|
84
|
-
- !binary |-
|
85
|
-
bGliL2phdmEvamdlb2lwLTAuMC42Lmphcg==
|
86
84
|
- !binary |-
|
87
85
|
bGliL2pnZW9pcC5yYg==
|
86
|
+
- !binary |-
|
87
|
+
bGliL2pnZW9pcC9qZ2VvaXAucmI=
|
88
|
+
- !binary |-
|
89
|
+
bGliL2pnZW9pcC9sb2NhdGlvbi5yYg==
|
88
90
|
- !binary |-
|
89
91
|
bGliL2pnZW9pcC92ZXJzaW9uLnJi
|
90
92
|
- !binary |-
|
91
93
|
c2NyaXB0L2JlbmNobWFyay5yYg==
|
92
|
-
- !binary |-
|
93
|
-
c3JjL21haW4vamF2YS9vcmcvZ2l0aHViL3RvYnNjaC9qZ2VvaXAvSkdlb0lQ
|
94
|
-
TGlicmFyeS5qYXZh
|
95
94
|
- !binary |-
|
96
95
|
dGVzdC90ZXN0X2hlbHBlci5yYg==
|
97
96
|
- !binary |-
|
@@ -121,7 +120,7 @@ rubyforge_project:
|
|
121
120
|
rubygems_version: 1.8.15
|
122
121
|
signing_key:
|
123
122
|
specification_version: 3
|
124
|
-
summary: Fast
|
123
|
+
summary: Fast jRuby library for Maxminds GeoIP Databases
|
125
124
|
test_files:
|
126
125
|
- !binary |-
|
127
126
|
dGVzdC90ZXN0X2hlbHBlci5yYg==
|
data/lib/java/jgeoip-0.0.6.jar
DELETED
Binary file
|
@@ -1,88 +0,0 @@
|
|
1
|
-
|
2
|
-
package org.github.tobsch.jgeoip;
|
3
|
-
import com.maxmind.geoip.*;
|
4
|
-
|
5
|
-
import java.io.IOException;
|
6
|
-
import java.util.ArrayList;
|
7
|
-
import java.util.Iterator;
|
8
|
-
import java.util.List;
|
9
|
-
import java.util.HashMap;
|
10
|
-
import org.jruby.runtime.Block;
|
11
|
-
import java.util.Map;
|
12
|
-
import java.util.HashSet;
|
13
|
-
import java.util.Set;
|
14
|
-
import java.lang.Object;
|
15
|
-
import org.jruby.Ruby;
|
16
|
-
import org.jruby.RubyNil;
|
17
|
-
import org.jruby.RubyArray;
|
18
|
-
import org.jruby.RubyClass;
|
19
|
-
import org.jruby.RubyFixnum;
|
20
|
-
import org.jruby.RubyObject;
|
21
|
-
import org.jruby.RubyStruct;
|
22
|
-
import org.jruby.RubyString;
|
23
|
-
import org.jruby.RubyFloat;
|
24
|
-
import org.jruby.RubyHash;
|
25
|
-
import org.jruby.anno.JRubyMethod;
|
26
|
-
import org.jruby.runtime.ObjectAllocator;
|
27
|
-
import org.jruby.runtime.ThreadContext;
|
28
|
-
import org.jruby.runtime.builtin.IRubyObject;
|
29
|
-
import org.jruby.runtime.load.Library;
|
30
|
-
import org.jruby.RubySymbol;
|
31
|
-
|
32
|
-
/**
|
33
|
-
*
|
34
|
-
*
|
35
|
-
*/
|
36
|
-
public class JGeoIPLibrary implements Library {
|
37
|
-
private Ruby ruby;
|
38
|
-
|
39
|
-
public void load(Ruby ruby, boolean bln) throws IOException {
|
40
|
-
this.ruby = ruby;
|
41
|
-
|
42
|
-
RubyClass jgeoip = ruby.defineClass("JGeoIP", ruby.getObject(), new ObjectAllocator() {
|
43
|
-
public IRubyObject allocate(Ruby ruby, RubyClass rc) {
|
44
|
-
return new JGeoIP(ruby, rc);
|
45
|
-
}
|
46
|
-
});
|
47
|
-
|
48
|
-
jgeoip.defineAnnotatedMethods(JGeoIP.class);
|
49
|
-
}
|
50
|
-
|
51
|
-
public class JGeoIP extends RubyObject {
|
52
|
-
LookupService cl;
|
53
|
-
|
54
|
-
public JGeoIP(final Ruby ruby, RubyClass rubyClass) {
|
55
|
-
super(ruby, rubyClass);
|
56
|
-
}
|
57
|
-
|
58
|
-
@JRubyMethod
|
59
|
-
public IRubyObject initialize(ThreadContext context, IRubyObject databaseLocation) throws IOException {
|
60
|
-
cl = new LookupService(databaseLocation.toString(), LookupService.GEOIP_MEMORY_CACHE);
|
61
|
-
|
62
|
-
return context.nil;
|
63
|
-
}
|
64
|
-
|
65
|
-
@JRubyMethod
|
66
|
-
public IRubyObject city(ThreadContext context, IRubyObject searchString) throws IOException {
|
67
|
-
Location location = cl.getLocation(searchString.toString());
|
68
|
-
if (location == null) {
|
69
|
-
RubyNil nil = new RubyNil(ruby);
|
70
|
-
return nil;
|
71
|
-
}
|
72
|
-
|
73
|
-
RubyHash loc = new RubyHash(ruby);
|
74
|
-
loc.put(RubySymbol.newSymbol(ruby, "city"), RubyString.newString(ruby, location.city));
|
75
|
-
loc.put(RubySymbol.newSymbol(ruby, "postal_code"), RubyString.newString(ruby, location.postalCode));
|
76
|
-
loc.put(RubySymbol.newSymbol(ruby, "country_code"), RubyString.newString(ruby, location.countryCode));
|
77
|
-
loc.put(RubySymbol.newSymbol(ruby, "country_name"), RubyString.newString(ruby, location.countryName));
|
78
|
-
loc.put(RubySymbol.newSymbol(ruby, "region"), RubyString.newString(ruby, location.region));
|
79
|
-
loc.put(RubySymbol.newSymbol(ruby, "latitude"), RubyFloat.newFloat(ruby, location.latitude));
|
80
|
-
loc.put(RubySymbol.newSymbol(ruby, "longitude"), RubyFloat.newFloat(ruby, location.longitude));
|
81
|
-
loc.put(RubySymbol.newSymbol(ruby, "dma_code"), RubyFixnum.newFixnum(ruby, location.dma_code));
|
82
|
-
loc.put(RubySymbol.newSymbol(ruby, "area_code"), RubyFixnum.newFixnum(ruby, location.area_code));
|
83
|
-
loc.put(RubySymbol.newSymbol(ruby, "metro_code"), RubyFixnum.newFixnum(ruby, location.metro_code));
|
84
|
-
|
85
|
-
return loc;
|
86
|
-
}
|
87
|
-
}
|
88
|
-
}
|