geolocal 0.8 → 0.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +33 -12
- data/contrib/{array-vs-range-benchmark.rb → benchmark-array-vs-range.rb} +0 -0
- data/contrib/benchmark-continents.rb +26 -0
- data/contrib/continents.rb +7 -0
- data/lib/geolocal/provider/base.rb +4 -0
- data/lib/geolocal/version.rb +1 -1
- data/spec/geolocal/provider/db_ip_spec.rb +5 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 47113ceb0b482073f831d29622e2bc7beb06131f
|
4
|
+
data.tar.gz: b311e669ffa4b394edc577e6d4d20c307366872b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d66866807b4ac24c99024652453cdbd677f0ea1689df1aa367832bf49e9071e1b295f0ec9293c8fe8f5aac703b2eddeba745254ed4d46de94dd145d1adb4db75
|
7
|
+
data.tar.gz: b0ab1135e3bc4b53af4849dd6ed8163c951a78e66c01d6e12c5c28bc1b6b6cc0227ca61f99d7b9c9101fd92c69ffb24c737f269bceff30b10500f8689199c94f
|
data/README.md
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
# Geolocal
|
2
2
|
|
3
|
-
|
3
|
+
Geocode IP addresses with a single Ruby if statement.
|
4
4
|
No network access, no context switches, no delay. Just one low-calorie lookup:
|
5
|
-
`Geolocal.in_spain?(request.remote_ip)
|
5
|
+
`Geolocal.in_spain?(request.remote_ip)`. 500,000 individual lookups
|
6
|
+
per second is fairly typical performance.
|
7
|
+
|
6
8
|
|
7
9
|
## Installation
|
8
10
|
|
@@ -22,7 +24,7 @@ The config file describes the ranges you're interested in.
|
|
22
24
|
Here's an example:
|
23
25
|
|
24
26
|
```ruby
|
25
|
-
Geolocal.configure do
|
27
|
+
Geolocal.configure do |config|
|
26
28
|
config.countries = {
|
27
29
|
us: 'US',
|
28
30
|
spain: 'ES',
|
@@ -43,7 +45,9 @@ Geolocal.in_central_america?('200.16.66.0')
|
|
43
45
|
|
44
46
|
#### The in\_*area*? method
|
45
47
|
|
46
|
-
The `rake geolocal:update` task generates
|
48
|
+
The `rake geolocal:update` task generates a Ruby file defining the methods you asked for.
|
49
|
+
You can pass:
|
50
|
+
|
47
51
|
* a string: `Geolocal.in_us?("10.1.2.3")`
|
48
52
|
* an [IPAddr](http://www.ruby-doc.org/stdlib-2.2.0/libdoc/ipaddr/rdoc/IPAddr.html) object:
|
49
53
|
`Geolocal.in_eu?(IPAddr.new('2.16.54.0'))`
|
@@ -51,6 +55,7 @@ The `rake geolocal:update` task generates the Ruby file defining these methods.
|
|
51
55
|
|
52
56
|
It returns true if the IP address is in the area, false if not.
|
53
57
|
|
58
|
+
|
54
59
|
## Config
|
55
60
|
|
56
61
|
Here are the supported configuration options:
|
@@ -64,6 +69,13 @@ Here are the supported configuration options:
|
|
64
69
|
* **ipv6**: whether the ranges should support ipv6 addresses.
|
65
70
|
|
66
71
|
|
72
|
+
## Providers
|
73
|
+
|
74
|
+
This gem currently only supoports the [DB-IP](https://db-ip.com/about/) Countries database.
|
75
|
+
There are lots of other databases available and this gem is organized to support them one day.
|
76
|
+
Patches welcome.
|
77
|
+
|
78
|
+
|
67
79
|
## Examples
|
68
80
|
|
69
81
|
There are some examples in the [contrib](https://github.com/bronson/geolocal/tree/master/contrib) directory.
|
@@ -91,33 +103,42 @@ Geolocal.configure do |config|
|
|
91
103
|
end
|
92
104
|
```
|
93
105
|
|
106
|
+
Now you can use it in your app: `cookie_warning if Geolocal.in_eu?(request.remote_ip)`
|
107
|
+
|
94
108
|
If European Union membership ever changes, just run `bundle update countries`
|
95
109
|
and `rake geolocal` to bring your app back up to date.
|
96
110
|
|
97
111
|
|
112
|
+
## Performance
|
98
113
|
|
99
|
-
|
114
|
+
It depends on how large an area you're looking up. `Geolocal.in_antarctica?` will
|
115
|
+
take less than half the time of `Geolocal.in_asia?`. Generally, you can
|
116
|
+
expect to do better than a million lookups every two seconds.
|
100
117
|
|
101
|
-
|
102
|
-
|
103
|
-
|
118
|
+
To see for yourself, run the continents benchmark:
|
119
|
+
|
120
|
+
```sh
|
121
|
+
rake geolocal config=contrib/continents.rb
|
122
|
+
ruby contrib/benchmark-continents.rb
|
123
|
+
```
|
104
124
|
|
105
125
|
|
106
126
|
## Alternatives
|
107
127
|
|
108
128
|
The [Geocoder gem](https://github.com/alexreisner/geocoder) offers
|
109
129
|
[local database services](https://github.com/alexreisner/geocoder#ip-address-local-database-services).
|
110
|
-
|
111
|
-
Geolocal also doesn't add any dependencies to your deploy, potentially making it easier to get working
|
112
|
-
environments like Heroku.
|
130
|
+
It offers more options and more providers than Geolocal, but it's a little more complex and not as fast.
|
131
|
+
Geolocal also doesn't add any dependencies to your deploy, potentially making it easier to get working
|
132
|
+
with oddball environments like Heroku.
|
113
133
|
|
114
134
|
|
115
135
|
## TODO
|
116
136
|
|
117
|
-
- [ ] performance information / benchmarks?
|
118
137
|
- [ ] Add support for cities
|
119
138
|
- [ ] other sources for this data? [MainFacts](http://mainfacts.com/ip-address-space-addresses), [NirSoft](http://www.nirsoft.net/countryip/)
|
120
139
|
Also maybe allow providers to accept their own options?
|
140
|
+
- [ ] release 1.0!
|
141
|
+
- [ ] Detect nesting? Putting in_eu?, in_europe?, and in_france? generates a lot of redundant overlap.
|
121
142
|
- [ ] Add support for for-pay features like lat/lon and timezones?
|
122
143
|
|
123
144
|
|
File without changes
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# Shows timings for the generated continent selectors
|
2
|
+
# You must first run:
|
3
|
+
#
|
4
|
+
# rake geolocal config=contrib/continents
|
5
|
+
|
6
|
+
require 'benchmark'
|
7
|
+
require_relative '../tmp/geolocal.rb'
|
8
|
+
|
9
|
+
|
10
|
+
N = 1_000_000
|
11
|
+
|
12
|
+
puts
|
13
|
+
puts "running #{N} lookups on each continent:"
|
14
|
+
puts
|
15
|
+
|
16
|
+
Benchmark.bm(15, 'africa/asia') do |x|
|
17
|
+
spread = 2**32/N - 500 # tries to cover the IPv4 range reasonably well
|
18
|
+
|
19
|
+
x.report('africa:') { N.times { |i| Geolocal.in_africa?(i*spread, 2) } }
|
20
|
+
x.report('antarcitca:') { N.times { |i| Geolocal.in_antarctica?(i*spread, 2) } }
|
21
|
+
x.report('asia:') { N.times { |i| Geolocal.in_asia?(i*spread, 2) } }
|
22
|
+
x.report('australia:') { N.times { |i| Geolocal.in_australia?(i*spread, 2) } }
|
23
|
+
x.report('europe:') { N.times { |i| Geolocal.in_europe?(i*spread, 2) } }
|
24
|
+
x.report('north_america:') { N.times { |i| Geolocal.in_north_america?(i*spread, 2) } }
|
25
|
+
x.report('south_america:') { N.times { |i| Geolocal.in_south_america?(i*spread, 2) } }
|
26
|
+
end
|
data/contrib/continents.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'ipaddr'
|
2
|
+
require 'fileutils'
|
2
3
|
|
3
4
|
|
4
5
|
module Geolocal
|
@@ -62,6 +63,7 @@ module Geolocal
|
|
62
63
|
|
63
64
|
read_ranges(countries) { |*args| add_to_results(results, *args) }
|
64
65
|
|
66
|
+
FileUtils.mkdir_p File.dirname(config[:file])
|
65
67
|
File.open(config[:file], 'w') do |file|
|
66
68
|
output(file, results)
|
67
69
|
end
|
@@ -125,6 +127,8 @@ module Geolocal
|
|
125
127
|
file.write <<EOL
|
126
128
|
# This file is autogenerated by the Geolocal gem
|
127
129
|
|
130
|
+
require 'ipaddr'
|
131
|
+
|
128
132
|
module #{modname}
|
129
133
|
|
130
134
|
def self.search address, family, v4module, v6module
|
data/lib/geolocal/version.rb
CHANGED
@@ -59,6 +59,8 @@ describe Geolocal::Provider::DB_IP do
|
|
59
59
|
<<EOL
|
60
60
|
# This file is autogenerated by the Geolocal gem
|
61
61
|
|
62
|
+
require 'ipaddr'
|
63
|
+
|
62
64
|
module Geolocal
|
63
65
|
|
64
66
|
def self.search address, family, v4module, v6module
|
@@ -78,7 +80,8 @@ EOL
|
|
78
80
|
}
|
79
81
|
|
80
82
|
def run_test example_body
|
81
|
-
|
83
|
+
outdir = "tmp/test-#{$$}"
|
84
|
+
outfile = "#{outdir}/geolocal.rb"
|
82
85
|
if File.exist?(outfile)
|
83
86
|
File.delete(outfile)
|
84
87
|
end
|
@@ -93,6 +96,7 @@ EOL
|
|
93
96
|
provider.update
|
94
97
|
expect(File.read outfile).to eq example_header + example_body
|
95
98
|
File.delete(outfile)
|
99
|
+
Dir.rmdir(outdir)
|
96
100
|
end
|
97
101
|
|
98
102
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: geolocal
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 0.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Scott Bronson
|
@@ -82,7 +82,8 @@ files:
|
|
82
82
|
- README.md
|
83
83
|
- Rakefile
|
84
84
|
- config/geolocal.rb
|
85
|
-
- contrib/array-vs-range
|
85
|
+
- contrib/benchmark-array-vs-range.rb
|
86
|
+
- contrib/benchmark-continents.rb
|
86
87
|
- contrib/continents.rb
|
87
88
|
- geolocal.gemspec
|
88
89
|
- lib/generators/geolocal/USAGE
|