jgeoip 0.1.0-java → 0.1.5-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 CHANGED
@@ -11,12 +11,15 @@ define 'jgeoip' do
11
11
  project.version = JGeoIP::VERSION
12
12
 
13
13
  # add dependencies from jar
14
- compile.with('org.kohsuke:geoip:jar:1.2.5')
14
+ compile.using(:target => '1.6', :source => '1.6').with('org.kohsuke:geoip:jar:1.2.5')
15
+
16
+ # package our shiny little bidder jar
17
+ package :jar, :file => _("lib/java/jgeoip-#{JGeoIP::VERSION}.jar")
15
18
 
16
19
  desc 'copy all dependent jars to lib folder'
17
20
  task :copy_dependencies do
18
21
  cp project.compile.dependencies.collect(&:to_s), project.path_to('lib/java')
19
22
  end
20
23
 
21
- task :setup => [ :clean, :copy_dependencies ]
24
+ task :setup => [ :clean, :compile, :package, :copy_dependencies ]
22
25
  end
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
- ## Fast JRuby GeoIP Library
2
- This library will help you with all your GeoIP needs in JRuby.
1
+ ## Fast JRuby GeoIP Extension
2
+ This jRuby extension will help you with all your GeoIP needs in JRuby.
3
3
  It is pretty small and simple right now but will surely grow up soon.
4
4
  You can easily use it with the free GeoIP Library from maxmind.com aswell as the commercial one.
5
5
 
@@ -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 location object with all the result properties and a #to_hash method
10
+ Will return a Location object with all the result properties and a #to_hash method
11
11
 
12
12
 
13
13
  ### speed?
@@ -17,8 +17,10 @@ Results are promising:
17
17
 
18
18
  pure ruby GeoIP:
19
19
  35.614000 0.000000 35.614000 ( 35.614000)
20
- JGeoIP (Old Version, as extension, updating soon):
21
- 1.318000 0.000000 1.318000 ( 1.318000)
20
+ JGeoIP (>=0.1.5):
21
+ 0.285000 0.000000 0.285000 ( 0.286000)
22
+ JGeoIP (non extension version, <0.1.5):
23
+ 1.047000 0.000000 1.047000 ( 1.048000)
22
24
 
23
25
  Please note that we did a warmup to give the JIT a chance.
24
26
  The benchmark does 100k lookups.
Binary file
@@ -1,3 +1,3 @@
1
1
  class JGeoIP
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.5"
3
3
  end
data/lib/jgeoip.rb CHANGED
@@ -5,5 +5,8 @@ require File.expand_path('../java/geoip-1.2.5', __FILE__)
5
5
 
6
6
  # load the version file
7
7
  require File.expand_path('../jgeoip/version', __FILE__)
8
- require File.expand_path('../jgeoip/jgeoip', __FILE__)
9
- require File.expand_path('../jgeoip/location', __FILE__)
8
+
9
+ # import the jRuby Extension
10
+ require File.expand_path("../java/jgeoip-#{JGeoIP::VERSION}", __FILE__)
11
+ java_import 'org.github.tobsch.jgeoip.JGeoIPLibrary'
12
+ JGeoIPLibrary.new.load(JRuby.runtime, false)
data/script/benchmark.rb CHANGED
@@ -17,15 +17,20 @@ end
17
17
  require File.expand_path('../../lib/jgeoip.rb', __FILE__)
18
18
 
19
19
  # check the pure lib
20
- require 'geoip'
21
- db = GeoIP.new('/opt/MaxMind/GeoLiteCity.dat')
22
- benchmark 'pure ruby GeoIP:' do
23
- result = db.city('github.com')
24
- end
20
+ # require 'geoip'
21
+ # db = GeoIP.new('/opt/MaxMind/GeoLiteCity.dat')
22
+ # benchmark 'pure ruby GeoIP:' do
23
+ # result = db.city('github.com')
24
+ # end
25
25
 
26
26
  db = JGeoIP.new('/opt/MaxMind/GeoLiteCity.dat')
27
27
  benchmark 'JGeoIP:' do
28
28
  result = db.city('github.com')
29
29
  end
30
30
 
31
+ db = JGeoIPJava.new('/opt/MaxMind/GeoLiteCity.dat')
32
+ benchmark 'JGeoIPJava:' do
33
+ result = db.city('github.com')
34
+ end
35
+
31
36
 
@@ -0,0 +1,44 @@
1
+ package org.github.tobsch.jgeoip;
2
+
3
+ import com.maxmind.geoip.*;
4
+
5
+ import java.io.IOException;
6
+
7
+ import org.jruby.Ruby;
8
+
9
+ import org.jruby.RubyClass;
10
+ import org.jruby.RubyObject;
11
+
12
+ import org.jruby.anno.JRubyMethod;
13
+ import org.jruby.runtime.ThreadContext;
14
+ import org.jruby.runtime.builtin.IRubyObject;
15
+
16
+
17
+ public class JGeoIP extends RubyObject {
18
+ LookupService cl;
19
+ static RubyClass locationProxyClass;
20
+
21
+ public JGeoIP(final Ruby ruby, RubyClass rubyClass) {
22
+ super(ruby, rubyClass);
23
+ this.locationProxyClass = ruby.getClass("Location");
24
+ }
25
+
26
+ @JRubyMethod
27
+ public IRubyObject initialize(ThreadContext context, IRubyObject databaseLocation) throws IOException {
28
+ cl = new LookupService(databaseLocation.toString(), LookupService.GEOIP_MEMORY_CACHE);
29
+
30
+ return context.nil;
31
+ }
32
+
33
+ @JRubyMethod
34
+ public IRubyObject city(ThreadContext context, IRubyObject searchString) throws IOException {
35
+ Location location = cl.getLocation(searchString.toString());
36
+ if (location == null) {
37
+ return context.runtime.getNil();
38
+ }
39
+
40
+ LocationProxy loc = new LocationProxy(context.runtime, locationProxyClass, location);
41
+
42
+ return loc;
43
+ }
44
+ }
@@ -0,0 +1,43 @@
1
+
2
+ package org.github.tobsch.jgeoip;
3
+ import org.github.tobsch.jgeoip.*;
4
+ import com.maxmind.geoip.*;
5
+
6
+ import java.io.IOException;
7
+
8
+ import org.jruby.Ruby;
9
+ import org.jruby.RubyClass;
10
+ import org.jruby.runtime.ObjectAllocator;
11
+ import org.jruby.runtime.builtin.IRubyObject;
12
+ import org.jruby.runtime.load.Library;
13
+
14
+ /**
15
+ *
16
+ *
17
+ */
18
+ public class JGeoIPLibrary implements Library {
19
+ private Ruby ruby;
20
+
21
+ public void load(Ruby ruby, boolean bln) throws IOException {
22
+ this.ruby = ruby;
23
+
24
+ // define the jgeoip class
25
+ final RubyClass jgeoip = ruby.defineClass("JGeoIP", ruby.getObject(), new ObjectAllocator() {
26
+ public IRubyObject allocate(Ruby ruby, RubyClass rc) {
27
+ return new JGeoIP(ruby, rc);
28
+ }
29
+ });
30
+
31
+ jgeoip.defineAnnotatedMethods(JGeoIP.class);
32
+
33
+ // define the location class
34
+ final RubyClass location = ruby.defineClass("Location", ruby.getObject(), new ObjectAllocator() {
35
+ public IRubyObject allocate(Ruby ruby, RubyClass rc) {
36
+ return new LocationProxy(ruby, rc);
37
+ }
38
+ });
39
+
40
+ location.defineAnnotatedMethods(LocationProxy.class);
41
+
42
+ }
43
+ }
@@ -0,0 +1,136 @@
1
+ package org.github.tobsch.jgeoip;
2
+ import com.maxmind.geoip.*;
3
+
4
+ import java.io.IOException;
5
+
6
+ import org.jruby.Ruby;
7
+ import org.jruby.runtime.ThreadContext;
8
+ import org.jruby.runtime.ObjectAllocator;
9
+ import org.jruby.runtime.builtin.IRubyObject;
10
+ import org.jruby.runtime.load.Library;
11
+ import org.jruby.runtime.Block;
12
+
13
+ import org.jruby.anno.JRubyMethod;
14
+
15
+ import org.jruby.RubyClass;
16
+ import org.jruby.RubyArray;
17
+ import org.jruby.RubyClass;
18
+ import org.jruby.RubyObject;
19
+ import org.jruby.RubyHash;
20
+ import org.jruby.RubySymbol;
21
+
22
+ import java.util.ArrayList;
23
+
24
+
25
+ public class LocationProxy extends RubyObject {
26
+ Location location;
27
+
28
+ private static final ArrayList<String> attributes = new ArrayList<String>();
29
+
30
+ // a map with all properties
31
+ static {
32
+ attributes.add("city");
33
+ attributes.add("postal_code");
34
+ attributes.add("country_code");
35
+ attributes.add("country_name");
36
+ attributes.add("region");
37
+ attributes.add("latitude");
38
+ attributes.add("longitude");
39
+ attributes.add("dma_code");
40
+ attributes.add("area_code");
41
+ attributes.add("metro_code");
42
+ }
43
+
44
+ public LocationProxy(final Ruby ruby, RubyClass rubyClass) {
45
+ super(ruby, rubyClass);
46
+ }
47
+
48
+ public LocationProxy(final Ruby ruby, RubyClass rubyClass, Location location) {
49
+ super(ruby, rubyClass);
50
+ this.location = location;
51
+ }
52
+
53
+ @JRubyMethod
54
+ public RubyArray keys(ThreadContext context) {
55
+ RubyArray res = RubyArray.newArray(context.runtime);
56
+ for (String key : attributes) {
57
+ res.add(RubySymbol.newSymbol(context.runtime, key));
58
+ }
59
+
60
+ return res;
61
+ }
62
+
63
+ @JRubyMethod(name = "to_hash")
64
+ public IRubyObject toHash(ThreadContext context) {
65
+ RubyHash hash = RubyHash.newHash(context.runtime);
66
+ for (String key : attributes) {
67
+ hash.put(RubySymbol.newSymbol(context.runtime, key), get(context, context.runtime.newString(key)));
68
+ }
69
+
70
+ return hash;
71
+ }
72
+
73
+ @JRubyMethod(name = "[]")
74
+ public IRubyObject get(ThreadContext context, IRubyObject key) {
75
+ if (attributes.contains(key.toString())) {
76
+ return send(context, key, Block.NULL_BLOCK);
77
+ } else {
78
+ return context.runtime.getNil();
79
+ }
80
+ }
81
+
82
+ @JRubyMethod
83
+ public IRubyObject inspect(ThreadContext context) {
84
+ return toHash(context).inspect();
85
+ }
86
+
87
+ @JRubyMethod(name = "city")
88
+ public IRubyObject getCity(ThreadContext context) {
89
+ return location.city == null ? context.runtime.getNil() : context.runtime.newString(location.city);
90
+ }
91
+
92
+ @JRubyMethod(name = "postal_code")
93
+ public IRubyObject getPostalCode(ThreadContext context) {
94
+ return location.postalCode == null ? context.runtime.getNil() : context.runtime.newString(location.postalCode);
95
+ }
96
+
97
+ @JRubyMethod(name = "country_code")
98
+ public IRubyObject getCountryCode(ThreadContext context) {
99
+ return location.countryCode == null ? context.runtime.getNil() : context.runtime.newString(location.countryCode);
100
+ }
101
+
102
+ @JRubyMethod(name = "country_name")
103
+ public IRubyObject getCountryName(ThreadContext context) {
104
+ return location.countryName == null ? context.runtime.getNil() : context.runtime.newString(location.countryName);
105
+ }
106
+
107
+ @JRubyMethod(name = "region")
108
+ public IRubyObject getRegion(ThreadContext context) {
109
+ return location.region == null ? context.runtime.getNil() : context.runtime.newString(location.region);
110
+ }
111
+
112
+ @JRubyMethod(name = "latitude")
113
+ public IRubyObject getLatitude(ThreadContext context) {
114
+ return context.runtime.newFloat(location.latitude);
115
+ }
116
+
117
+ @JRubyMethod(name = "longitude")
118
+ public IRubyObject getLongitude(ThreadContext context) {
119
+ return context.runtime.newFloat(location.latitude);
120
+ }
121
+
122
+ @JRubyMethod(name = "dma_code")
123
+ public IRubyObject getDMACode(ThreadContext context) {
124
+ return context.runtime.newFixnum(location.dma_code);
125
+ }
126
+
127
+ @JRubyMethod(name = "area_code")
128
+ public IRubyObject getAreaCode(ThreadContext context) {
129
+ return context.runtime.newFixnum(location.area_code);
130
+ }
131
+
132
+ @JRubyMethod(name = "metro_code")
133
+ public IRubyObject getMetroCode(ThreadContext context) {
134
+ return context.runtime.newFixnum(location.metro_code);
135
+ }
136
+ }
@@ -14,17 +14,38 @@ class JGeoIPTest < Test::Unit::TestCase
14
14
  assert_equal 'San Francisco', result[:city]
15
15
  end
16
16
 
17
+ should 'be a proper location object' do
18
+ result = @geo.city('github.com')
19
+ assert_kind_of Location, result
20
+ end
21
+
17
22
  should 'find the city by ip too' do
18
23
  result = @geo.city('207.97.227.239')
19
24
  assert_equal 'United States', result[:country_name]
20
25
  end
21
-
22
- should 'be able to return the result as a hash' do
26
+
27
+ should 'return nil if an attribute does not exist' do
28
+ result = @geo.city('207.97.227.239')
29
+ assert_equal nil, result[:fooooooo]
30
+ end
31
+
32
+ should 'return a nil value if there is no value for an attribute' do
23
33
  result = @geo.city('85.183.18.35')
24
- assert_kind_of Hash, result.to_hash
34
+ assert_equal nil, result.postal_code
35
+ assert_equal 0, result.dma_code
36
+ end
37
+
38
+ should 'be able to return the result as a hash' do
39
+ result = @geo.city('85.183.18.35').to_hash
40
+ assert_kind_of Hash, result
25
41
  assert_equal 'Germany', result[:country_name]
26
42
  end
27
43
 
44
+ should 'be inspectable' do
45
+ result = @geo.city('85.183.18.35').inspect
46
+ assert_match '{:city=>"Othmarschen"', result
47
+ end
48
+
28
49
  should 'throw a clean exception if the ip was not found' do
29
50
  result = @geo.city('127.0.0.1')
30
51
  assert_equal nil, result
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: jgeoip
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.0
5
+ version: 0.1.5
6
6
  platform: java
7
7
  authors:
8
8
  - Tobias Schlottke
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-22 00:00:00.000000000 Z
12
+ date: 2012-02-26 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: shoulda-context
@@ -17,8 +17,7 @@ dependencies:
17
17
  requirements:
18
18
  - - ! '>='
19
19
  - !ruby/object:Gem::Version
20
- version: !binary |-
21
- MA==
20
+ version: '0'
22
21
  none: false
23
22
  requirement: *2056
24
23
  prerelease: false
@@ -40,8 +39,7 @@ dependencies:
40
39
  requirements:
41
40
  - - ! '>='
42
41
  - !ruby/object:Gem::Version
43
- version: !binary |-
44
- MA==
42
+ version: '0'
45
43
  none: false
46
44
  requirement: *2090
47
45
  prerelease: false
@@ -52,8 +50,7 @@ dependencies:
52
50
  requirements:
53
51
  - - ! '>='
54
52
  - !ruby/object:Gem::Version
55
- version: !binary |-
56
- MA==
53
+ version: '0'
57
54
  none: false
58
55
  requirement: *2106
59
56
  prerelease: false
@@ -65,36 +62,23 @@ executables: []
65
62
  extensions: []
66
63
  extra_rdoc_files: []
67
64
  files:
68
- - !binary |-
69
- LmdpdGlnbm9yZQ==
70
- - !binary |-
71
- LnJ2bXJj
72
- - !binary |-
73
- QnVpbGRmaWxl
74
- - !binary |-
75
- R2VtZmlsZQ==
76
- - !binary |-
77
- UkVBRE1FLm1k
78
- - !binary |-
79
- UmFrZWZpbGU=
80
- - !binary |-
81
- amdlb2lwLmdlbXNwZWM=
82
- - !binary |-
83
- bGliL2phdmEvZ2VvaXAtMS4yLjUuamFy
84
- - !binary |-
85
- bGliL2pnZW9pcC5yYg==
86
- - !binary |-
87
- bGliL2pnZW9pcC9qZ2VvaXAucmI=
88
- - !binary |-
89
- bGliL2pnZW9pcC9sb2NhdGlvbi5yYg==
90
- - !binary |-
91
- bGliL2pnZW9pcC92ZXJzaW9uLnJi
92
- - !binary |-
93
- c2NyaXB0L2JlbmNobWFyay5yYg==
94
- - !binary |-
95
- dGVzdC90ZXN0X2hlbHBlci5yYg==
96
- - !binary |-
97
- dGVzdC91bml0L2pnZW9pcF90ZXN0LnJi
65
+ - .gitignore
66
+ - .rvmrc
67
+ - Buildfile
68
+ - Gemfile
69
+ - README.md
70
+ - Rakefile
71
+ - jgeoip.gemspec
72
+ - lib/java/geoip-1.2.5.jar
73
+ - lib/java/jgeoip-0.1.5.jar
74
+ - lib/jgeoip.rb
75
+ - lib/jgeoip/version.rb
76
+ - script/benchmark.rb
77
+ - src/main/java/org/github/tobsch/jgeoip/JGeoIP.java
78
+ - src/main/java/org/github/tobsch/jgeoip/JGeoIPLibrary.java
79
+ - src/main/java/org/github/tobsch/jgeoip/LocationProxy.java
80
+ - test/test_helper.rb
81
+ - test/unit/jgeoip_test.rb
98
82
  homepage: http://github.com/tobsch/jgeoip
99
83
  licenses: []
100
84
  post_install_message:
@@ -105,15 +89,13 @@ required_ruby_version: !ruby/object:Gem::Requirement
105
89
  requirements:
106
90
  - - ! '>='
107
91
  - !ruby/object:Gem::Version
108
- version: !binary |-
109
- MA==
92
+ version: '0'
110
93
  none: false
111
94
  required_rubygems_version: !ruby/object:Gem::Requirement
112
95
  requirements:
113
96
  - - ! '>='
114
97
  - !ruby/object:Gem::Version
115
- version: !binary |-
116
- MA==
98
+ version: '0'
117
99
  none: false
118
100
  requirements: []
119
101
  rubyforge_project:
@@ -122,8 +104,6 @@ signing_key:
122
104
  specification_version: 3
123
105
  summary: Fast jRuby library for Maxminds GeoIP Databases
124
106
  test_files:
125
- - !binary |-
126
- dGVzdC90ZXN0X2hlbHBlci5yYg==
127
- - !binary |-
128
- dGVzdC91bml0L2pnZW9pcF90ZXN0LnJi
107
+ - test/test_helper.rb
108
+ - test/unit/jgeoip_test.rb
129
109
  ...
data/lib/jgeoip/jgeoip.rb DELETED
@@ -1,12 +0,0 @@
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
@@ -1,35 +0,0 @@
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