jgeoip 0.2.3-java → 0.2.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.
- checksums.yaml +7 -0
- data/lib/java/jgeoip.jar +0 -0
- data/lib/jgeoip.rb +2 -8
- data/lib/jgeoip/version.rb +1 -1
- data/test/unit/jgeoip_test.rb +17 -7
- metadata +33 -48
- data/.gitignore +0 -8
- data/.ruby-version +0 -1
- data/Buildfile +0 -25
- data/Gemfile +0 -4
- data/README.md +0 -53
- data/Rakefile +0 -21
- data/jgeoip.gemspec +0 -28
- data/lib/java/geoip-api-1.2.10.jar +0 -0
- data/lib/java/jgeoip-0.2.2.jar +0 -0
- data/lib/java/jgeoip-0.2.3.jar +0 -0
- data/script/benchmark.rb +0 -45
- data/src/main/java/org/github/tobsch/jgeoip/JGeoIP.java +0 -64
- data/src/main/java/org/github/tobsch/jgeoip/JGeoIPLibrary.java +0 -43
- data/src/main/java/org/github/tobsch/jgeoip/LocationProxy.java +0 -142
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: dc1f71133149460ad25bf351bec24d4a9471beaa
|
4
|
+
data.tar.gz: 08a03294e91471c417056aae3d3fbe0009012fcf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 09c30ccf5ab9f03deb702724335d43678480448fee778a1d08937dc9ed06c750587ea2a52a0ee4a9b91a7a0436d30f849d256bd070ba99fc760ac574dce90837
|
7
|
+
data.tar.gz: 400ab61aa5345a0c52f5d96ff06cde1ba579346b990c0f6dcdb96508decaa3b00de3f5f8ff819b77743ad697196b1d2d5c72a1a0b7f1898e0ce707b2163dd26b
|
data/lib/java/jgeoip.jar
ADDED
Binary file
|
data/lib/jgeoip.rb
CHANGED
@@ -1,12 +1,6 @@
|
|
1
|
-
require '
|
1
|
+
require 'geoip-jars'
|
2
2
|
|
3
|
-
|
4
|
-
require File.expand_path('../java/geoip-api-1.2.10', __FILE__)
|
3
|
+
Dir[File.expand_path('../java/*.jar', __FILE__)].each { |jar| require(jar) }
|
5
4
|
|
6
|
-
# load the version file
|
7
|
-
require File.expand_path('../jgeoip/version', __FILE__)
|
8
|
-
|
9
|
-
# import the jRuby Extension
|
10
|
-
require File.expand_path("../java/jgeoip-#{JGeoIP::VERSION}", __FILE__)
|
11
5
|
java_import 'org.github.tobsch.jgeoip.JGeoIPLibrary'
|
12
6
|
JGeoIPLibrary.new.load(JRuby.runtime, false)
|
data/lib/jgeoip/version.rb
CHANGED
data/test/unit/jgeoip_test.rb
CHANGED
@@ -19,7 +19,8 @@ class JGeoIPTest < Test::Unit::TestCase
|
|
19
19
|
|
20
20
|
context 'The Geoip City API' do
|
21
21
|
setup do
|
22
|
-
@
|
22
|
+
@db_path = 'tmp/GeoLiteCity.dat'
|
23
|
+
@geo = JGeoIP.new(@db_path)
|
23
24
|
end
|
24
25
|
|
25
26
|
should 'find the correct city by hostname' do
|
@@ -37,8 +38,8 @@ class JGeoIPTest < Test::Unit::TestCase
|
|
37
38
|
should 'find the city by ip too' do
|
38
39
|
result = @geo.city('207.97.227.239')
|
39
40
|
assert_equal 'United States', result[:country_name]
|
40
|
-
assert_in_delta 37.74839782714844, result.latitude,
|
41
|
-
assert_in_delta -122.41560363769531, result.longitude,
|
41
|
+
assert_in_delta 37.74839782714844, result.latitude, 100
|
42
|
+
assert_in_delta -122.41560363769531, result.longitude, 100
|
42
43
|
end
|
43
44
|
|
44
45
|
should 'find out the distance between to points' do
|
@@ -46,10 +47,10 @@ class JGeoIPTest < Test::Unit::TestCase
|
|
46
47
|
p2 = @geo.city('alice.de')
|
47
48
|
p3 = @geo.city('facebook.com')
|
48
49
|
# from sa to hamburg
|
49
|
-
assert_in_delta 8537.271518247373, p1.distance(p2),
|
50
|
+
assert_in_delta 8537.271518247373, p1.distance(p2), 100
|
50
51
|
|
51
52
|
# from sa to sf
|
52
|
-
assert_in_delta 2363.280770976432, p1.distance(p3),
|
53
|
+
assert_in_delta 2363.280770976432, p1.distance(p3), 100
|
53
54
|
end
|
54
55
|
|
55
56
|
should 'return nil if an attribute does not exist' do
|
@@ -71,7 +72,7 @@ class JGeoIPTest < Test::Unit::TestCase
|
|
71
72
|
|
72
73
|
should 'be inspectable' do
|
73
74
|
result = @geo.city('85.183.18.35').inspect
|
74
|
-
assert_match /:city=>"
|
75
|
+
assert_match /:city=>"Hamburg"/, result
|
75
76
|
end
|
76
77
|
|
77
78
|
should 'throw a clean exception if the ip was not found' do
|
@@ -84,6 +85,15 @@ class JGeoIPTest < Test::Unit::TestCase
|
|
84
85
|
assert_equal 'Osnabrück', result
|
85
86
|
assert_equal 'UTF-8', result.encoding.to_s
|
86
87
|
end
|
87
|
-
|
88
|
+
|
89
|
+
should 'accept caching options' do
|
90
|
+
JGeoIP.new(@db_path, :caching => JGeoIP::GEOIP_STANDARD | JGeoIP::GEOIP_INDEX_CACHE)
|
91
|
+
end
|
92
|
+
|
93
|
+
should 'raises an error when the caching options are not a number' do
|
94
|
+
assert_raise ArgumentError do
|
95
|
+
JGeoIP.new(@db_path, :caching => 'yes please')
|
96
|
+
end
|
97
|
+
end
|
88
98
|
end
|
89
99
|
end
|
metadata
CHANGED
@@ -1,80 +1,85 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jgeoip
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
5
|
-
prerelease:
|
4
|
+
version: 0.2.5
|
6
5
|
platform: java
|
7
6
|
authors:
|
8
7
|
- Tobias Schlottke
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-03-17 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
|
-
|
14
|
+
name: geoip-jars
|
15
|
+
version_requirements: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
16
20
|
requirement: !ruby/object:Gem::Requirement
|
17
21
|
requirements:
|
18
22
|
- - '>='
|
19
23
|
- !ruby/object:Gem::Version
|
20
24
|
version: '0'
|
21
|
-
|
25
|
+
prerelease: false
|
26
|
+
type: :runtime
|
27
|
+
- !ruby/object:Gem::Dependency
|
22
28
|
name: shoulda-context
|
23
29
|
version_requirements: !ruby/object:Gem::Requirement
|
24
30
|
requirements:
|
25
31
|
- - '>='
|
26
32
|
- !ruby/object:Gem::Version
|
27
33
|
version: '0'
|
28
|
-
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
29
39
|
prerelease: false
|
30
|
-
- !ruby/object:Gem::Dependency
|
31
40
|
type: :development
|
32
|
-
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: buildr
|
43
|
+
version_requirements: !ruby/object:Gem::Requirement
|
33
44
|
requirements:
|
34
45
|
- - '='
|
35
46
|
- !ruby/object:Gem::Version
|
36
47
|
version: 1.4.6
|
37
|
-
|
38
|
-
name: buildr
|
39
|
-
version_requirements: !ruby/object:Gem::Requirement
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
40
49
|
requirements:
|
41
50
|
- - '='
|
42
51
|
- !ruby/object:Gem::Version
|
43
52
|
version: 1.4.6
|
44
|
-
none: false
|
45
53
|
prerelease: false
|
46
|
-
- !ruby/object:Gem::Dependency
|
47
54
|
type: :development
|
48
|
-
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: ci_reporter
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
49
58
|
requirements:
|
50
59
|
- - '>='
|
51
60
|
- !ruby/object:Gem::Version
|
52
61
|
version: '0'
|
53
|
-
|
54
|
-
name: ci_reporter
|
55
|
-
version_requirements: !ruby/object:Gem::Requirement
|
62
|
+
requirement: !ruby/object:Gem::Requirement
|
56
63
|
requirements:
|
57
64
|
- - '>='
|
58
65
|
- !ruby/object:Gem::Version
|
59
66
|
version: '0'
|
60
|
-
none: false
|
61
67
|
prerelease: false
|
62
|
-
- !ruby/object:Gem::Dependency
|
63
68
|
type: :development
|
64
|
-
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: test-unit
|
71
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
72
|
requirements:
|
66
73
|
- - '>='
|
67
74
|
- !ruby/object:Gem::Version
|
68
75
|
version: '0'
|
69
|
-
|
70
|
-
name: test-unit
|
71
|
-
version_requirements: !ruby/object:Gem::Requirement
|
76
|
+
requirement: !ruby/object:Gem::Requirement
|
72
77
|
requirements:
|
73
78
|
- - '>='
|
74
79
|
- !ruby/object:Gem::Version
|
75
80
|
version: '0'
|
76
|
-
none: false
|
77
81
|
prerelease: false
|
82
|
+
type: :development
|
78
83
|
description: This gem wraps maxminds original java library, which should make things faster than using other pure ruby libraries.
|
79
84
|
email:
|
80
85
|
- tobias.schlottke@gmail.com
|
@@ -82,26 +87,14 @@ executables: []
|
|
82
87
|
extensions: []
|
83
88
|
extra_rdoc_files: []
|
84
89
|
files:
|
85
|
-
- .
|
86
|
-
- .ruby-version
|
87
|
-
- Buildfile
|
88
|
-
- Gemfile
|
89
|
-
- README.md
|
90
|
-
- Rakefile
|
91
|
-
- jgeoip.gemspec
|
92
|
-
- lib/java/geoip-api-1.2.10.jar
|
93
|
-
- lib/java/jgeoip-0.2.2.jar
|
94
|
-
- lib/java/jgeoip-0.2.3.jar
|
90
|
+
- lib/java/jgeoip.jar
|
95
91
|
- lib/jgeoip.rb
|
96
92
|
- lib/jgeoip/version.rb
|
97
|
-
- script/benchmark.rb
|
98
|
-
- src/main/java/org/github/tobsch/jgeoip/JGeoIP.java
|
99
|
-
- src/main/java/org/github/tobsch/jgeoip/JGeoIPLibrary.java
|
100
|
-
- src/main/java/org/github/tobsch/jgeoip/LocationProxy.java
|
101
93
|
- test/test_helper.rb
|
102
94
|
- test/unit/jgeoip_test.rb
|
103
95
|
homepage: http://github.com/tobsch/jgeoip
|
104
96
|
licenses: []
|
97
|
+
metadata: {}
|
105
98
|
post_install_message:
|
106
99
|
rdoc_options: []
|
107
100
|
require_paths:
|
@@ -110,25 +103,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
110
103
|
requirements:
|
111
104
|
- - '>='
|
112
105
|
- !ruby/object:Gem::Version
|
113
|
-
segments:
|
114
|
-
- 0
|
115
106
|
version: '0'
|
116
|
-
hash: 2
|
117
|
-
none: false
|
118
107
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
108
|
requirements:
|
120
109
|
- - '>='
|
121
110
|
- !ruby/object:Gem::Version
|
122
|
-
segments:
|
123
|
-
- 0
|
124
111
|
version: '0'
|
125
|
-
hash: 2
|
126
|
-
none: false
|
127
112
|
requirements: []
|
128
113
|
rubyforge_project:
|
129
|
-
rubygems_version:
|
114
|
+
rubygems_version: 2.2.2
|
130
115
|
signing_key:
|
131
|
-
specification_version:
|
116
|
+
specification_version: 4
|
132
117
|
summary: Fast jRuby library for Maxminds GeoIP Databases
|
133
118
|
test_files:
|
134
119
|
- test/test_helper.rb
|
data/.gitignore
DELETED
data/.ruby-version
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
jruby-1.7.4
|
data/Buildfile
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
require 'bundler/setup'
|
2
|
-
|
3
|
-
$:.push File.expand_path("../lib", __FILE__)
|
4
|
-
require "jgeoip/version"
|
5
|
-
|
6
|
-
# add maven repositories
|
7
|
-
repositories.remote << 'http://www.ibiblio.org/maven2'
|
8
|
-
repositories.remote << 'http://repo1.maven.org/maven2'
|
9
|
-
|
10
|
-
define 'jgeoip' do
|
11
|
-
project.version = JGeoIP::VERSION
|
12
|
-
|
13
|
-
# add dependencies from jar
|
14
|
-
compile.using(:target => '1.6', :source => '1.6').with('com.maxmind.geoip:geoip-api:jar:1.2.10')
|
15
|
-
|
16
|
-
# package our shiny little bidder jar
|
17
|
-
package :jar, :file => _("lib/java/jgeoip-#{JGeoIP::VERSION}.jar")
|
18
|
-
|
19
|
-
desc 'copy all dependent jars to lib folder'
|
20
|
-
task :copy_dependencies do
|
21
|
-
cp project.compile.dependencies.collect(&:to_s), project.path_to('lib/java')
|
22
|
-
end
|
23
|
-
|
24
|
-
task :setup => [ :clean, :compile, :package, :copy_dependencies ]
|
25
|
-
end
|
data/Gemfile
DELETED
data/README.md
DELETED
@@ -1,53 +0,0 @@
|
|
1
|
-
# Fast JRuby GeoIP Extension
|
2
|
-
This jRuby extension will help you with all your GeoIP needs in JRuby.
|
3
|
-
It is pretty small and simple right now but will surely grow up soon.
|
4
|
-
You can easily use it with the free GeoIP Library from maxmind.com aswell as the commercial one.
|
5
|
-
|
6
|
-
## example
|
7
|
-
### City Lookup
|
8
|
-
p JGeoIP.new('/opt/MaxMind/GeoLiteCity.dat').city('github.com')
|
9
|
-
|
10
|
-
# => {:city=>"San Francisco", :postal_code=>"94110", :country_code=>"US", :country_name=>"United States", :region=>"CA", :latitude=>37.74839782714844, :longitude=>37.74839782714844, :dma_code=>807, :area_code=>415, :metro_code=>807}
|
11
|
-
|
12
|
-
Will return a Location object with all the result properties and a #to_hash method
|
13
|
-
|
14
|
-
### Organization/ISP Lookup
|
15
|
-
p JGeoIP.new('/opt/MaxMind/GeoIPISP.dat').org('github.com')
|
16
|
-
|
17
|
-
Will return a string holding the organization/isp name
|
18
|
-
|
19
|
-
|
20
|
-
### Distance between two locations (km)
|
21
|
-
p1 = @geo.city('github.com')
|
22
|
-
p2 = @geo.city('facebook.com')
|
23
|
-
|
24
|
-
p p1.distance(p2)
|
25
|
-
# => 46.25354059751858
|
26
|
-
|
27
|
-
## speed?
|
28
|
-
There is a benchmark script included, so you'll be able to check it yourself.
|
29
|
-
We compared it to the GeoIP gem so far (because geoip-c won't compile with jRuby).
|
30
|
-
Results are promising:
|
31
|
-
|
32
|
-
#### JGeoIP
|
33
|
-
JGeoIP (>=0.1.5):
|
34
|
-
0.285000 0.000000 0.285000 ( 0.286000)
|
35
|
-
JGeoIP (old non extension version, <0.1.5):
|
36
|
-
1.047000 0.000000 1.047000 ( 1.048000)
|
37
|
-
|
38
|
-
#### Others
|
39
|
-
pure ruby GeoIP (jRuby):
|
40
|
-
35.614000 0.000000 35.614000 ( 35.614000)
|
41
|
-
geoip-c (MRI 1.9.3-p0):
|
42
|
-
1.140000 0.000000 1.140000 ( 1.145224)
|
43
|
-
pure ruby GeoIP (MRI 1.9.3-p0):
|
44
|
-
30.060000 16.420000 46.480000 ( 60.940723)
|
45
|
-
*Cool, eh?*
|
46
|
-
|
47
|
-
Please note that we did a warmup to give the JIT a chance.
|
48
|
-
The benchmark does 100k lookups.
|
49
|
-
Those results are Java 7, jruby-1.6.5 based. Looking forward to 1.7.0.
|
50
|
-
|
51
|
-
### TODO
|
52
|
-
* integrate the other lookup methods: so far we're just able to do city lookups
|
53
|
-
* check all the license related stuff: the maxmind lib is LGPL, which would be okay for me
|
data/Rakefile
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
require 'bundler/gem_tasks'
|
2
|
-
require 'rake/testtask'
|
3
|
-
|
4
|
-
desc 'Run all the tests'
|
5
|
-
task :default => [ :test ]
|
6
|
-
|
7
|
-
task :test => [ 'test:unit' ]
|
8
|
-
namespace :test do
|
9
|
-
|
10
|
-
task :environment do
|
11
|
-
ENV['RACK_ENV'] = 'test'
|
12
|
-
end
|
13
|
-
|
14
|
-
[ :unit ].each do |category|
|
15
|
-
Rake::TestTask.new(category) do |t|
|
16
|
-
t.libs << "test"
|
17
|
-
t.test_files = Dir["test/#{category}/**/*_test.rb"]
|
18
|
-
t.verbose = true
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
data/jgeoip.gemspec
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
require File.expand_path("../lib/jgeoip/version", __FILE__)
|
3
|
-
|
4
|
-
Gem::Specification.new do |s|
|
5
|
-
s.name = "jgeoip"
|
6
|
-
s.version = JGeoIP::VERSION
|
7
|
-
s.authors = ["Tobias Schlottke"]
|
8
|
-
s.email = ["tobias.schlottke@gmail.com"]
|
9
|
-
s.homepage = "http://github.com/tobsch/jgeoip"
|
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
|
-
s.platform = 'java'
|
13
|
-
|
14
|
-
s.files = `git ls-files`.split("\n")
|
15
|
-
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
-
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
-
s.require_paths = ["lib"]
|
18
|
-
|
19
|
-
# specify any dependencies here; for example:
|
20
|
-
# s.add_development_dependency "rspec"
|
21
|
-
# s.add_runtime_dependency "rest-client"
|
22
|
-
s.add_development_dependency 'shoulda-context'
|
23
|
-
s.add_development_dependency 'buildr', '1.4.6'
|
24
|
-
s.add_development_dependency 'ci_reporter'
|
25
|
-
|
26
|
-
# necessary instead of Mini:Unit as Mini:Unit cannot produce Jenkins-compatible test results
|
27
|
-
s.add_development_dependency 'test-unit'
|
28
|
-
end
|
Binary file
|
data/lib/java/jgeoip-0.2.2.jar
DELETED
Binary file
|
data/lib/java/jgeoip-0.2.3.jar
DELETED
Binary file
|
data/script/benchmark.rb
DELETED
@@ -1,45 +0,0 @@
|
|
1
|
-
require 'benchmark'
|
2
|
-
require 'rubygems'
|
3
|
-
|
4
|
-
def benchmark(description, &block)
|
5
|
-
puts description
|
6
|
-
# do a warmup
|
7
|
-
10_000.times {
|
8
|
-
block.call
|
9
|
-
}
|
10
|
-
# do the actual measurements
|
11
|
-
puts Benchmark.measure {
|
12
|
-
100_000.times {
|
13
|
-
block.call
|
14
|
-
}
|
15
|
-
}
|
16
|
-
end
|
17
|
-
|
18
|
-
jruby = defined?(JRUBY_VERSION)
|
19
|
-
DB_FILE = '/opt/MaxMind/GeoLiteCity.dat'
|
20
|
-
if jruby
|
21
|
-
# check the JGeoIP
|
22
|
-
require File.expand_path('../../lib/jgeoip.rb', __FILE__)
|
23
|
-
|
24
|
-
db = JGeoIP.new(DB_FILE)
|
25
|
-
benchmark 'JGeoIP:' do
|
26
|
-
result = db.city('github.com')
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
# check the pure lib
|
31
|
-
gem 'geoip'
|
32
|
-
require 'geoip'
|
33
|
-
db = GeoIP.new(DB_FILE)
|
34
|
-
benchmark 'pure ruby GeoIP:' do
|
35
|
-
result = db.city('github.com')
|
36
|
-
end
|
37
|
-
|
38
|
-
unless jruby
|
39
|
-
gem 'geoip-c'
|
40
|
-
require 'geoip-c/geoip'
|
41
|
-
db = GeoIP::City.new(DB_FILE)
|
42
|
-
benchmark 'geoip-c:' do
|
43
|
-
db.look_up('207.97.227.239')
|
44
|
-
end
|
45
|
-
end
|
@@ -1,64 +0,0 @@
|
|
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
|
-
|
45
|
-
@JRubyMethod
|
46
|
-
public IRubyObject org(ThreadContext context, IRubyObject searchString) throws IOException {
|
47
|
-
String org = cl.getOrg(searchString.toString());
|
48
|
-
if (org == null) {
|
49
|
-
return context.runtime.getNil();
|
50
|
-
}
|
51
|
-
|
52
|
-
return context.runtime.newString(org);
|
53
|
-
}
|
54
|
-
|
55
|
-
@JRubyMethod
|
56
|
-
public IRubyObject id(ThreadContext context, IRubyObject searchString) throws IOException {
|
57
|
-
Integer id = cl.getID(searchString.toString());
|
58
|
-
if (id == null) {
|
59
|
-
return context.runtime.getNil();
|
60
|
-
}
|
61
|
-
|
62
|
-
return context.runtime.newFixnum((long)id);
|
63
|
-
}
|
64
|
-
}
|
@@ -1,43 +0,0 @@
|
|
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
|
-
}
|
@@ -1,142 +0,0 @@
|
|
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 distance(ThreadContext context, IRubyObject p2) {
|
84
|
-
LocationProxy p2loc = (LocationProxy)p2;
|
85
|
-
return context.runtime.newFloat(location.distance(p2loc.location));
|
86
|
-
}
|
87
|
-
|
88
|
-
@JRubyMethod
|
89
|
-
public IRubyObject inspect(ThreadContext context) {
|
90
|
-
return toHash(context).inspect();
|
91
|
-
}
|
92
|
-
|
93
|
-
@JRubyMethod(name = "city")
|
94
|
-
public IRubyObject getCity(ThreadContext context) {
|
95
|
-
return location.city == null ? context.runtime.getNil() : context.runtime.newString(location.city);
|
96
|
-
}
|
97
|
-
|
98
|
-
@JRubyMethod(name = "postal_code")
|
99
|
-
public IRubyObject getPostalCode(ThreadContext context) {
|
100
|
-
return location.postalCode == null ? context.runtime.getNil() : context.runtime.newString(location.postalCode);
|
101
|
-
}
|
102
|
-
|
103
|
-
@JRubyMethod(name = "country_code")
|
104
|
-
public IRubyObject getCountryCode(ThreadContext context) {
|
105
|
-
return location.countryCode == null ? context.runtime.getNil() : context.runtime.newString(location.countryCode);
|
106
|
-
}
|
107
|
-
|
108
|
-
@JRubyMethod(name = "country_name")
|
109
|
-
public IRubyObject getCountryName(ThreadContext context) {
|
110
|
-
return location.countryName == null ? context.runtime.getNil() : context.runtime.newString(location.countryName);
|
111
|
-
}
|
112
|
-
|
113
|
-
@JRubyMethod(name = "region")
|
114
|
-
public IRubyObject getRegion(ThreadContext context) {
|
115
|
-
return location.region == null ? context.runtime.getNil() : context.runtime.newString(location.region);
|
116
|
-
}
|
117
|
-
|
118
|
-
@JRubyMethod(name = "latitude")
|
119
|
-
public IRubyObject getLatitude(ThreadContext context) {
|
120
|
-
return context.runtime.newFloat(location.latitude);
|
121
|
-
}
|
122
|
-
|
123
|
-
@JRubyMethod(name = "longitude")
|
124
|
-
public IRubyObject getLongitude(ThreadContext context) {
|
125
|
-
return context.runtime.newFloat(location.longitude);
|
126
|
-
}
|
127
|
-
|
128
|
-
@JRubyMethod(name = "dma_code")
|
129
|
-
public IRubyObject getDMACode(ThreadContext context) {
|
130
|
-
return context.runtime.newFixnum(location.dma_code);
|
131
|
-
}
|
132
|
-
|
133
|
-
@JRubyMethod(name = "area_code")
|
134
|
-
public IRubyObject getAreaCode(ThreadContext context) {
|
135
|
-
return context.runtime.newFixnum(location.area_code);
|
136
|
-
}
|
137
|
-
|
138
|
-
@JRubyMethod(name = "metro_code")
|
139
|
-
public IRubyObject getMetroCode(ThreadContext context) {
|
140
|
-
return context.runtime.newFixnum(location.metro_code);
|
141
|
-
}
|
142
|
-
}
|