geolocal 0.8.1 → 0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -0
- data/config/geolocal.rb +1 -1
- data/lib/generators/geolocal/templates/geolocal.rb +1 -1
- data/lib/geolocal.rb +2 -29
- data/lib/geolocal/configuration.rb +49 -34
- data/lib/geolocal/provider/base.rb +1 -1
- data/lib/geolocal/provider/db_ip.rb +3 -0
- data/lib/geolocal/version.rb +1 -1
- data/lib/tasks/geolocal.rake +3 -3
- data/spec/geolocal/provider/base_spec.rb +2 -2
- data/spec/spec_helper.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 65d27c3ffbd6c516bc62cb08663fd536472a24ef
|
4
|
+
data.tar.gz: 85c77e798dab700c8ddb602fce533fad7794b46a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8e94cb66a6465c12d7b8111fc99eca1cc2a800bcc7e0e98abfbe42f690cb03ba835ba88665be74330e85eeb9e314bcd8ee98f902ff9fcad989b9717455047cbe
|
7
|
+
data.tar.gz: c97b4d0bc05f06481587c39b1410450036cf83887ee2188870b2e007c6a940914382c44e4a7bea0076a342fa400c098ab1b61f7f5ac52dcc6ff0f8edfc1f192c
|
data/README.md
CHANGED
@@ -24,6 +24,7 @@ The config file describes the ranges you're interested in.
|
|
24
24
|
Here's an example:
|
25
25
|
|
26
26
|
```ruby
|
27
|
+
require 'geolocal/configuration'
|
27
28
|
Geolocal.configure do |config|
|
28
29
|
config.countries = {
|
29
30
|
us: 'US',
|
@@ -38,6 +39,7 @@ from the default provider (see the [providers](#providers) section) and
|
|
38
39
|
creates the desired methods:
|
39
40
|
|
40
41
|
```ruby
|
42
|
+
require 'geolocal'
|
41
43
|
Geolocal.in_us?(request.remote_ip)
|
42
44
|
Geolocal.in_spain?('2a05:af06::') # optional IPv6 support
|
43
45
|
Geolocal.in_central_america?('200.16.66.0')
|
data/config/geolocal.rb
CHANGED
data/lib/geolocal.rb
CHANGED
@@ -1,32 +1,5 @@
|
|
1
|
-
require 'geolocal/
|
2
|
-
|
3
|
-
require 'geolocal/provider/base'
|
4
|
-
|
5
|
-
|
1
|
+
# If you're wondering why nothing's here, you probably wanted to require 'geolocal/configuration'
|
2
|
+
# Once you define a configuration and generate the geolocation code, this module will have contents.
|
6
3
|
module Geolocal
|
7
4
|
require 'geolocal/railtie' if defined? Rails
|
8
|
-
|
9
|
-
def self.configuration
|
10
|
-
@configuration ||= Configuration.new
|
11
|
-
end
|
12
|
-
|
13
|
-
def self.configure
|
14
|
-
yield(configuration)
|
15
|
-
end
|
16
|
-
|
17
|
-
def self.reset_configuration
|
18
|
-
@configuration = nil
|
19
|
-
end
|
20
|
-
|
21
|
-
def self.provider
|
22
|
-
@provider ||= configuration.load_provider.new
|
23
|
-
end
|
24
|
-
|
25
|
-
def self.download
|
26
|
-
provider.download
|
27
|
-
end
|
28
|
-
|
29
|
-
def self.update
|
30
|
-
provider.update
|
31
|
-
end
|
32
5
|
end
|
@@ -1,45 +1,60 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
def initialize
|
6
|
-
# configuration defaults
|
7
|
-
@provider = 'Geolocal::Provider::DB_IP'
|
8
|
-
@module = 'Geolocal'
|
9
|
-
@file = nil # default is computed
|
10
|
-
@tmpdir = 'tmp/geolocal'
|
11
|
-
@ipv4 = true
|
12
|
-
@ipv6 = true
|
13
|
-
@quiet = false
|
14
|
-
@countries = {}
|
15
|
-
end
|
1
|
+
module Geolocal
|
2
|
+
class Configuration
|
3
|
+
OPTIONS = [ :provider, :module, :file, :tmpdir, :ipv4, :ipv6, :quiet, :countries ]
|
4
|
+
attr_accessor(*OPTIONS)
|
16
5
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
6
|
+
def initialize
|
7
|
+
# configuration defaults
|
8
|
+
@provider = 'Geolocal::Provider::DB_IP'
|
9
|
+
@module = 'Geolocal'
|
10
|
+
@file = nil # default is computed
|
11
|
+
@tmpdir = 'tmp/geolocal'
|
12
|
+
@ipv4 = true
|
13
|
+
@ipv6 = true
|
14
|
+
@quiet = false
|
15
|
+
@countries = {}
|
16
|
+
end
|
17
|
+
|
18
|
+
# if not set, defaults to lib/module-name
|
19
|
+
def file
|
20
|
+
@file || "lib/#{module_file @module}.rb"
|
21
|
+
end
|
22
|
+
|
23
|
+
def require_provider_file
|
24
|
+
begin
|
25
|
+
# normal ruby/gem load path
|
26
|
+
Kernel.require module_file(@provider)
|
27
|
+
rescue LoadError
|
28
|
+
# used when running source code locally
|
29
|
+
Kernel.require "./lib/#{module_file(@provider)}.rb"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def load_provider
|
34
|
+
require_provider_file
|
35
|
+
@provider.split('::').reduce(Module, :const_get)
|
36
|
+
end
|
21
37
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
38
|
+
def to_hash
|
39
|
+
# returned keys will always be symbols
|
40
|
+
OPTIONS.reduce({}) { |a,v| a.merge! v => self.send(v) }
|
41
|
+
end
|
42
|
+
|
43
|
+
def module_file modname
|
44
|
+
modname.gsub('::', '/').gsub(/([a-z])([A-Z])/, '\1_\2').downcase
|
29
45
|
end
|
30
46
|
end
|
31
47
|
|
32
|
-
|
33
|
-
|
34
|
-
@
|
48
|
+
|
49
|
+
def self.configuration
|
50
|
+
@configuration ||= Configuration.new
|
35
51
|
end
|
36
52
|
|
37
|
-
def
|
38
|
-
|
39
|
-
OPTIONS.reduce({}) { |a,v| a.merge! v => self.send(v) }
|
53
|
+
def self.configure
|
54
|
+
yield(configuration)
|
40
55
|
end
|
41
56
|
|
42
|
-
def
|
43
|
-
|
57
|
+
def self.reset_configuration
|
58
|
+
@configuration = nil
|
44
59
|
end
|
45
60
|
end
|
@@ -47,7 +47,7 @@ module Geolocal
|
|
47
47
|
end
|
48
48
|
|
49
49
|
def countries
|
50
|
-
@countries ||= config[:countries].sort_by { |k, v| k }.reduce({}) { |a, (k, v)|
|
50
|
+
@countries ||= config[:countries].sort_by { |k, v| k.to_s }.reduce({}) { |a, (k, v)|
|
51
51
|
k = k.to_s.gsub(/[ -]/, '_')
|
52
52
|
raise "invalid identifier: '#{k}'" if k =~ /^[^A-Za-z_]|[^A-Za-z0-9_]|^\s*$/
|
53
53
|
a.merge! k.to_s.downcase => Array(v).map { |c| c.to_s.upcase }.sort.to_set
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'geolocal/provider/base'
|
2
|
+
|
1
3
|
require 'csv'
|
2
4
|
require 'net/http'
|
3
5
|
require 'fileutils'
|
@@ -69,6 +71,7 @@ class Geolocal::Provider::DB_IP < Geolocal::Provider::Base
|
|
69
71
|
end
|
70
72
|
|
71
73
|
# a bit of debugging code to print all non-matched country codes. should be deleted one day.
|
74
|
+
# The Countries gem doesn't know about these country codes from the csv: CS FX UK YU TP and blank
|
72
75
|
def check_country_codes(countries, row)
|
73
76
|
@known_codes ||= countries.reduce(Set.new) { |a,(_,v)| a.merge v; a }
|
74
77
|
unless @known_codes.include?(row[2])
|
data/lib/geolocal/version.rb
CHANGED
data/lib/tasks/geolocal.rake
CHANGED
@@ -7,18 +7,18 @@ config='config/geolocal'
|
|
7
7
|
# `rake geolocal config=contrib/continents`
|
8
8
|
config=ENV['config'] if ENV['config']
|
9
9
|
puts "loading geolocal configuration from #{config}"
|
10
|
-
require './'+config
|
10
|
+
require './' + config
|
11
11
|
|
12
12
|
|
13
13
|
namespace :geolocal do
|
14
14
|
desc "Downloads the most recent geocoding information"
|
15
15
|
task :download do
|
16
|
-
Geolocal.download
|
16
|
+
Geolocal.configuration.load_provider.new.download
|
17
17
|
end
|
18
18
|
|
19
19
|
desc "Updates your geocoding statements to use new data."
|
20
20
|
task :update => :download do
|
21
|
-
Geolocal.update
|
21
|
+
Geolocal.configuration.load_provider.new.update
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
@@ -50,7 +50,7 @@ describe Geolocal::Provider::Base do
|
|
50
50
|
describe '#countries' do
|
51
51
|
it 'preprocesses the countries' do
|
52
52
|
Geolocal.configure do |config|
|
53
|
-
config.countries = { ua: :UA,
|
53
|
+
config.countries = { ua: :UA, Na: ['MX', 'CA', :US] }
|
54
54
|
end
|
55
55
|
|
56
56
|
expect(provider.countries).to eq({ 'na' => Set['CA', 'MX', 'US'], 'ua' => Set['UA'] })
|
@@ -58,7 +58,7 @@ describe Geolocal::Provider::Base do
|
|
58
58
|
|
59
59
|
it 'preprocesses countries with an odd name' do
|
60
60
|
Geolocal.configure do |config|
|
61
|
-
config.countries = { 'U s-a'
|
61
|
+
config.countries = { 'U s-a' => 'US', mex: 'MX' }
|
62
62
|
end
|
63
63
|
|
64
64
|
expect(provider.countries).to eq({ 'mex' => Set['MX'], 'u_s_a' => Set['US'] })
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: geolocal
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: '0.9'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Scott Bronson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|