geolocal 0.8 → 0.8.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9967a2d045173bb94fd0a2099f9751d6c485d5e2
4
- data.tar.gz: 68ccf53dc8987083ea502b65d27742c6f23e29c2
3
+ metadata.gz: 47113ceb0b482073f831d29622e2bc7beb06131f
4
+ data.tar.gz: b311e669ffa4b394edc577e6d4d20c307366872b
5
5
  SHA512:
6
- metadata.gz: 95eb28b17e6676f41ea1684eb156ae562bd56a8d91dca3368c9a43f21f5c039e4969722a5f5830439406436de5ba32ada049f91b79f284645f1810c82a013dad
7
- data.tar.gz: 218051f783714e1cdfdf988f3e6e76da636fcc3d4c0b71f4016ed5932e409ea1b246698e0baf13bab07ba5b7235c0333540bece81d149aa4515b3fdf20780626
6
+ metadata.gz: d66866807b4ac24c99024652453cdbd677f0ea1689df1aa367832bf49e9071e1b295f0ec9293c8fe8f5aac703b2eddeba745254ed4d46de94dd145d1adb4db75
7
+ data.tar.gz: b0ab1135e3bc4b53af4849dd6ed8163c951a78e66c01d6e12c5c28bc1b6b6cc0227ca61f99d7b9c9101fd92c69ffb24c737f269bceff30b10500f8689199c94f
data/README.md CHANGED
@@ -1,8 +1,10 @@
1
1
  # Geolocal
2
2
 
3
- Allows IP addresses to geocoded with a single Ruby if statement.
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 the Ruby file defining these methods. You can pass:
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
- ## Providers
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
- This gem currently only supoports the [DB-IP](https://db-ip.com/about/) Countries database.
102
- There are lots of other databases available and this gem is organized to support them one day.
103
- Patches welcome.
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
- Geolocal is simpler, faster, and production ready, but the Geocoder gem offers more options and more providers.
111
- Geolocal also doesn't add any dependencies to your deploy, potentially making it easier to get working with oddball
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
 
@@ -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
@@ -1,3 +1,10 @@
1
+ # to generate selectors for each continent:
2
+ #
3
+ # rake geolocal config=contrib/continents
4
+ #
5
+ # Now you can call Geolocal.in_africa?, in_antarcitca?, in_asia?, etc.
6
+
7
+
1
8
  require 'geolocal'
2
9
  require 'countries'
3
10
 
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Geolocal
2
- VERSION = "0.8"
2
+ VERSION = "0.8.1"
3
3
  end
@@ -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
- outfile = 'tmp/geolocal.rb'
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: '0.8'
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-benchmark.rb
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