geo_locale 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +34 -3
- data/geo_locale.gemspec +7 -8
- data/lib/geo_locale/country_code.rb +46 -0
- data/lib/geo_locale/locale.rb +79 -0
- data/lib/geo_locale/version.rb +1 -1
- data/lib/geo_locale.rb +10 -4
- data/spec/spec_helper.rb +10 -3
- data/spec/tests/geolocation_spec.rb +146 -0
- data/spec/tests/initialize_spec.rb +5 -19
- data/spec/tests/locale_spec.rb +110 -0
- metadata +19 -16
- data/lib/geo_locale/geo_locale.rb +0 -25
- data/spec/tests/geo_ip_part_spec.rb +0 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2810f63b7da90f39c9449254810fc4a53c89185c
|
4
|
+
data.tar.gz: c89638f968f4ec925c6b6e9263e1b8600164c839
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3de8168f9d7121b3eaa847246a53095687edb278a1212620c5de2941097184aedbc06a1d680b9dfc8104ffdfc2b114d47fe53d4218d23e4e0ad28587d48e934b
|
7
|
+
data.tar.gz: 3eec2c6def2b37bc874d77255cf95ea6db5a05fd905b33d563c75a7a293e594de6ea104905772309570a8643200d327d38bc4d9053eb51eedee6a9eeb607e360
|
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# GeoLocale
|
2
2
|
|
3
|
-
|
3
|
+
Simple wrapper for GeoIP gem with some redundancy on GeoKit to get a two-letter country code or locale from an IP address.
|
4
|
+
|
5
|
+
Basic use-case: Detect application user's origin and display app in that language.
|
4
6
|
|
5
7
|
## Installation
|
6
8
|
|
@@ -21,12 +23,41 @@ Or install it yourself as:
|
|
21
23
|
## Usage
|
22
24
|
Configure default locales
|
23
25
|
```ruby
|
26
|
+
# in /config/initializers/geo_locale.rb
|
24
27
|
GeoLocale.configure do |config|
|
25
|
-
config.
|
26
|
-
config.
|
28
|
+
config.default_country = "de" # set to nil or false if you want to catch fails in geolocation
|
29
|
+
config.default_locale = "en" # country->locale conversion is minimal for now, set this explicitly to ensure GeoLocale.locale
|
30
|
+
config.default_lcid = "en-us"
|
31
|
+
config.overrides["ee"] = "en" # hash used to override standard country codes
|
27
32
|
end
|
28
33
|
```
|
29
34
|
|
35
|
+
To get country code:
|
36
|
+
GeoLocale.country_code(ip: "")
|
37
|
+
Use in controller like so:
|
38
|
+
```ruby
|
39
|
+
GeoLocale.country_code(request.remote_ip)
|
40
|
+
=> "en"
|
41
|
+
```
|
42
|
+
|
43
|
+
To get locale:
|
44
|
+
GeoLocale.locale(country_code: "", ip: "", lcid: false)
|
45
|
+
```ruby
|
46
|
+
GeoLocale.locale(country_code: "gb")
|
47
|
+
=> "en"
|
48
|
+
GeoLocale.locale(country_code: "gb", lcid: true)
|
49
|
+
=> "en-gb"
|
50
|
+
GeoLocale.locale(ip: "97.77.25.20")
|
51
|
+
=> "en"
|
52
|
+
GeoLocale.locale(ip: "97.77.25.20", lcid: true)
|
53
|
+
=> "en-us"
|
54
|
+
```
|
55
|
+
country_code is prioritized as it does not require external calls.
|
56
|
+
```ruby
|
57
|
+
GeoLocale.locale(ip: "97.77.25.20", country_code: "lv")
|
58
|
+
=> "lv"
|
59
|
+
```
|
60
|
+
|
30
61
|
## Contributing
|
31
62
|
|
32
63
|
1. Fork it ( https://github.com/Epigene/geo_locale/fork )
|
data/geo_locale.gemspec
CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |spec|
|
|
7
7
|
spec.name = "geo_locale"
|
8
8
|
spec.version = GeoLocale::VERSION
|
9
9
|
spec.date = Date.today.to_s
|
10
|
-
spec.platform = Gem::Platform::RUBY
|
10
|
+
#spec.platform = Gem::Platform::RUBY
|
11
11
|
spec.authors = ["Epigene"]
|
12
12
|
spec.email = ["augusts.bautra@gmail.com"]
|
13
13
|
spec.summary = ['A Rails 4+ gem for getting user country']
|
@@ -19,15 +19,14 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
20
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
21
21
|
spec.require_paths = ["lib"]
|
22
|
-
|
22
|
+
spec.rubyforge_project = "geo_locale"
|
23
23
|
|
24
|
-
spec.add_development_dependency "bundler", "~> 1.7"
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.7.0"
|
25
25
|
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
-
spec.add_development_dependency 'rspec'
|
27
|
-
spec.add_development_dependency 'rails'
|
26
|
+
spec.add_development_dependency 'rspec', "~> 3.1.0"
|
28
27
|
|
29
|
-
#spec.add_dependency "rest-client"
|
30
|
-
#spec.add_dependency "activesupport"
|
31
28
|
spec.add_dependency 'geoip'
|
32
|
-
spec.add_dependency '
|
29
|
+
spec.add_dependency 'geokit'
|
30
|
+
spec.add_dependency 'geokit-rails'
|
31
|
+
|
33
32
|
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module GeoLocale
|
2
|
+
IP_REGEX = /\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/
|
3
|
+
|
4
|
+
def self.country_code (ip: "") # ip = "12.12.12.12"
|
5
|
+
country_code = GeoLocale.geo_ip_try(ip)
|
6
|
+
if country_code.present?
|
7
|
+
return country_code
|
8
|
+
else
|
9
|
+
country_code = GeoLocale.geokit_try(ip)
|
10
|
+
if country_code.present?
|
11
|
+
return country_code
|
12
|
+
else
|
13
|
+
# final fallback, config default
|
14
|
+
return GeoLocale.config.default_country
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.geo_ip_try (ip)
|
20
|
+
begin
|
21
|
+
return nil unless ip.match(GeoLocale::IP_REGEX).present?
|
22
|
+
code = GeoIP.new("#{GeoLocale::ROOT}/lib/data/GeoIP.dat").country(ip).country_code2.downcase
|
23
|
+
if code == "--"
|
24
|
+
return nil
|
25
|
+
else
|
26
|
+
if GeoLocale.config.overrides["#{code}"].present?
|
27
|
+
return GeoLocale.config.overrides["#{code}"]
|
28
|
+
else
|
29
|
+
return code
|
30
|
+
end
|
31
|
+
end
|
32
|
+
rescue
|
33
|
+
nil
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.geokit_try (ip)
|
38
|
+
return nil unless ip.match(GeoLocale::IP_REGEX).present?
|
39
|
+
begin
|
40
|
+
return Geokit::Geocoders::MultiGeocoder.geocode(ip).country_code.downcase
|
41
|
+
rescue
|
42
|
+
nil
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module GeoLocale
|
2
|
+
|
3
|
+
def self.locale (ip: "", country_code: "", lcid: false)
|
4
|
+
if country_code.present?
|
5
|
+
unless lcid
|
6
|
+
locale = GeoLocale::LOCALE[country_code]
|
7
|
+
return figure_out_returnable_locale(locale: locale)
|
8
|
+
else
|
9
|
+
lcid_string = GeoLocale::LCID[country_code]
|
10
|
+
return figure_out_returnable_lcid(lcid_string: lcid_string)
|
11
|
+
end
|
12
|
+
|
13
|
+
elsif ip.present?
|
14
|
+
country_code = GeoLocale.country_code(ip: ip)
|
15
|
+
unless lcid
|
16
|
+
locale = GeoLocale::LOCALE[country_code]
|
17
|
+
return figure_out_returnable_locale(locale: locale)
|
18
|
+
else
|
19
|
+
lcid_string = GeoLocale::LCID[country_code]
|
20
|
+
return figure_out_returnable_lcid(lcid_string: lcid_string)
|
21
|
+
end
|
22
|
+
|
23
|
+
else
|
24
|
+
unless lcid
|
25
|
+
locale = GeoLocale::LOCALE["#{GeoLocale.config.default_country}"]
|
26
|
+
return figure_out_returnable_locale(locale: locale)
|
27
|
+
else
|
28
|
+
lcid_string = GeoLocale::LCID["#{GeoLocale.config.default_country}"]
|
29
|
+
return figure_out_returnable_lcid(lcid_string: lcid_string)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
LOCALE = {
|
35
|
+
"lv" => "lv",
|
36
|
+
"lt" => "lt",
|
37
|
+
"ee" => "et",
|
38
|
+
"ru" => "ru",
|
39
|
+
"ie" => "en", # Ireland
|
40
|
+
"gb" => "en", # UK a.k.a Great Britain
|
41
|
+
"uk" => "en", # fallback England
|
42
|
+
"us" => "en", # US&A
|
43
|
+
"pl" => "pl",
|
44
|
+
"de" => "de",
|
45
|
+
"fr" => "fr"
|
46
|
+
}
|
47
|
+
|
48
|
+
LCID = {
|
49
|
+
"lv" => "lv",
|
50
|
+
"lt" => "lt",
|
51
|
+
"ee" => "et",
|
52
|
+
"ru" => "ru",
|
53
|
+
"ie" => "en-ie", # Ireland
|
54
|
+
"gb" => "en-gb", # UK a.k.a Great Britain
|
55
|
+
"uk" => "en-gb", # fallback England
|
56
|
+
"us" => "en-us", # US&A
|
57
|
+
"pl" => "pl",
|
58
|
+
"de" => "de",
|
59
|
+
"fr" => "fr"
|
60
|
+
}
|
61
|
+
|
62
|
+
|
63
|
+
def self.figure_out_returnable_locale(locale:)
|
64
|
+
if locale.present?
|
65
|
+
return locale
|
66
|
+
else
|
67
|
+
return GeoLocale.config.default_locale
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.figure_out_returnable_lcid(lcid_string:)
|
72
|
+
if lcid_string.present?
|
73
|
+
return lcid_string
|
74
|
+
else
|
75
|
+
return GeoLocale.config.default_lcid
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
data/lib/geo_locale/version.rb
CHANGED
data/lib/geo_locale.rb
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
require "geo_locale/version"
|
2
|
-
require "geo_locale/
|
2
|
+
require "geo_locale/country_code"
|
3
|
+
require "geo_locale/locale"
|
4
|
+
require 'geoip'
|
5
|
+
require 'geokit'
|
6
|
+
require 'geokit-rails'
|
3
7
|
|
4
8
|
module GeoLocale
|
5
9
|
class << self
|
@@ -12,11 +16,13 @@ module GeoLocale
|
|
12
16
|
end
|
13
17
|
|
14
18
|
class Config
|
15
|
-
attr_accessor :
|
19
|
+
attr_accessor :default_country, :default_locale, :default_lcid, :overrides
|
16
20
|
|
17
21
|
def initialize
|
18
|
-
@
|
19
|
-
@
|
22
|
+
@default_country = "us"
|
23
|
+
@default_locale = "en"
|
24
|
+
@default_lcid = "en-us"
|
25
|
+
@overrides = {}
|
20
26
|
end
|
21
27
|
end
|
22
28
|
|
data/spec/spec_helper.rb
CHANGED
@@ -2,8 +2,15 @@ require 'bundler/setup'
|
|
2
2
|
Bundler.setup
|
3
3
|
|
4
4
|
require "geo_locale"
|
5
|
-
require "rails"
|
6
5
|
|
7
6
|
RSpec.configure do |config|
|
8
|
-
|
9
|
-
|
7
|
+
LV_IPS =["81.198.65.13", "85.9.209.244", "83.241.46.175", "78.84.147.188", "159.148.168.103"]
|
8
|
+
LT_IPS =["77.79.35.98", "158.129.84.6", "91.211.246.238"]
|
9
|
+
EE_IPS =["188.92.162.172", "46.36.219.51", "84.50.44.25"]
|
10
|
+
RU_IPS =["92.255.197.224", "213.85.92.10", "80.78.245.89"]
|
11
|
+
US_IPS =["97.77.25.20", "192.185.83.90", "199.180.150.25"]
|
12
|
+
GB_IPS =["78.157.209.36", "79.143.87.234", "195.40.6.43"]
|
13
|
+
IR_IPS =["77.104.75.76", "94.74.132.65"]
|
14
|
+
IE_IPS =["54.170.245.171", "54.171.4.57", "176.34.86.253"]
|
15
|
+
PL_IPS =["83.3.115.58", "109.207.54.81", "88.199.29.50"]
|
16
|
+
end
|
@@ -0,0 +1,146 @@
|
|
1
|
+
# rspec spec/tests/geolocation_spec.rb
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe "Geolocation" do
|
6
|
+
lv_ip = LV_IPS.sample
|
7
|
+
lt_ip = LT_IPS.sample
|
8
|
+
ee_ip = EE_IPS.sample
|
9
|
+
ru_ip = RU_IPS.sample
|
10
|
+
us_ip = US_IPS.sample
|
11
|
+
gb_ip = GB_IPS.sample
|
12
|
+
ir_ip = IR_IPS.sample
|
13
|
+
ie_ip = IE_IPS.sample
|
14
|
+
pl_ip = PL_IPS.sample
|
15
|
+
|
16
|
+
before :each do
|
17
|
+
GeoLocale.config = nil
|
18
|
+
GeoLocale.configure {}
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "GeoIP" do
|
22
|
+
it "should return nil for 0.0.0.0" do
|
23
|
+
expect(GeoLocale.geo_ip_try("0.0.0.0")).to eq nil
|
24
|
+
end
|
25
|
+
it "should return nil for localhost" do
|
26
|
+
expect(GeoLocale.geo_ip_try("localhost")).to eq nil
|
27
|
+
end
|
28
|
+
it "should return lv for #{lv_ip}" do
|
29
|
+
expect(GeoLocale.geo_ip_try(lv_ip) ).to eq "lv"
|
30
|
+
end
|
31
|
+
it "should return lt for #{lt_ip}" do
|
32
|
+
expect(GeoLocale.geo_ip_try(lt_ip)).to eq "lt"
|
33
|
+
end
|
34
|
+
it "should return ee for #{ee_ip}" do
|
35
|
+
expect(GeoLocale.geo_ip_try(ee_ip)).to eq "ee"
|
36
|
+
end
|
37
|
+
it "should return et for #{ee_ip} after override" do
|
38
|
+
GeoLocale.config.overrides["ee"] = "et"
|
39
|
+
expect(GeoLocale.geo_ip_try("#{ee_ip}")).to eq "et"
|
40
|
+
end
|
41
|
+
it "should return ru for #{ru_ip}" do
|
42
|
+
expect(GeoLocale.geo_ip_try("#{ru_ip}")).to eq "ru"
|
43
|
+
end
|
44
|
+
it "should return us for #{us_ip}" do
|
45
|
+
expect(GeoLocale.geo_ip_try("#{us_ip}")).to eq "us"
|
46
|
+
end
|
47
|
+
it "should return gb for #{gb_ip}" do # Great Britain
|
48
|
+
expect(GeoLocale.geo_ip_try("#{gb_ip}")).to eq "gb"
|
49
|
+
end
|
50
|
+
it "should return ir for #{ir_ip}" do # Iran
|
51
|
+
expect(GeoLocale.geo_ip_try("#{ir_ip}")).to eq "ir"
|
52
|
+
end
|
53
|
+
it "should return ie for #{ie_ip}" do # Ireland
|
54
|
+
expect(GeoLocale.geo_ip_try("#{ie_ip}")).to eq "ie"
|
55
|
+
end
|
56
|
+
it "should return pl for #{pl_ip}" do # Poland
|
57
|
+
expect(GeoLocale.geo_ip_try("#{pl_ip}")).to eq "pl"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "GeoKit" do # enable these to see IPs fail
|
62
|
+
# xit "should return nil for #{lv_ip}" do
|
63
|
+
# expect(GeoLocale.geokit_try("#{lv_ip}")).to eq nil
|
64
|
+
# end
|
65
|
+
# xit "should return nil for localhost" do
|
66
|
+
# expect(GeoLocale.geokit_try("localhost")).to eq nil
|
67
|
+
# end
|
68
|
+
xit "should return lv for #{lv_ip}" do
|
69
|
+
puts lv_ip
|
70
|
+
expect(GeoLocale.geokit_try(lv_ip)).to eq "lv"
|
71
|
+
end
|
72
|
+
# xit "should return lt for #{lt_ip}" do
|
73
|
+
# expect(GeoLocale.geokit_try(lt_ip)).to eq "lt"
|
74
|
+
# end
|
75
|
+
# xit "should return ee for #{ee_ip}" do
|
76
|
+
# expect(GeoLocale.geokit_try(ee_ip)).to eq "ee"
|
77
|
+
# end
|
78
|
+
# xit "should return et for #{ee_ip} after override" do
|
79
|
+
# GeoLocale.config.overrides["ee"] = "et"
|
80
|
+
# expect(GeoLocale.geokit_try("#{ee_ip}")).to eq "et"
|
81
|
+
# end
|
82
|
+
# xit "should return ru for #{ru_ip}" do
|
83
|
+
# expect(GeoLocale.geokit_try("#{ru_ip}")).to eq "ru"
|
84
|
+
# end
|
85
|
+
# xit "should return us for #{us_ip}" do
|
86
|
+
# expect(GeoLocale.geokit_try("#{us_ip}")).to eq "us"
|
87
|
+
# end
|
88
|
+
# xit "should return gb for #{gb_ip}" do # Great Britain
|
89
|
+
# expect(GeoLocale.geokit_try("#{gb_ip}")).to eq "gb"
|
90
|
+
# end
|
91
|
+
# xit "should return ir for #{ir_ip}" do # Iran
|
92
|
+
# expect(GeoLocale.geokit_try("#{ir_ip}")).to eq "ir"
|
93
|
+
# end
|
94
|
+
# xit "should return ie for #{ie_ip}" do # Ireland
|
95
|
+
# expect(GeoLocale.geokit_try("#{ie_ip}")).to eq "ie"
|
96
|
+
# end
|
97
|
+
# xit "should return pl for #{pl_ip}" do # Poland
|
98
|
+
# expect(GeoLocale.geokit_try("#{pl_ip}")).to eq "pl"
|
99
|
+
# end
|
100
|
+
end
|
101
|
+
|
102
|
+
describe "Cascade" do
|
103
|
+
it "should return config default and set value for 0.0.0.0" do
|
104
|
+
expect(GeoLocale.country_code(ip: "0.0.0.0")).to eq "us"
|
105
|
+
GeoLocale.config.default_country = "de"
|
106
|
+
expect(GeoLocale.country_code(ip: "0.0.0.0")).to eq "de"
|
107
|
+
end
|
108
|
+
it "should return config default and set value for localhost" do
|
109
|
+
expect(GeoLocale.country_code(ip: "localhost")).to eq "us"
|
110
|
+
GeoLocale.config.default_country = "fr"
|
111
|
+
expect(GeoLocale.country_code(ip: "localhost")).to eq "fr"
|
112
|
+
end
|
113
|
+
it "should return lv for #{lv_ip}" do
|
114
|
+
expect(GeoLocale.country_code(ip: lv_ip)).to eq "lv"
|
115
|
+
end
|
116
|
+
it "should return lt for #{lt_ip}" do
|
117
|
+
expect(GeoLocale.country_code(ip: lt_ip)).to eq "lt"
|
118
|
+
end
|
119
|
+
it "should return ee for #{ee_ip}" do
|
120
|
+
expect(GeoLocale.country_code(ip: ee_ip)).to eq "ee"
|
121
|
+
end
|
122
|
+
it "should return et for #{ee_ip} after override" do
|
123
|
+
GeoLocale.config.overrides["ee"] = "et"
|
124
|
+
expect(GeoLocale.country_code(ip: "#{ee_ip}")).to eq "et"
|
125
|
+
end
|
126
|
+
it "should return ru for #{ru_ip}" do
|
127
|
+
expect(GeoLocale.country_code(ip: "#{ru_ip}")).to eq "ru"
|
128
|
+
end
|
129
|
+
it "should return us for #{us_ip}" do
|
130
|
+
expect(GeoLocale.country_code(ip: "#{us_ip}")).to eq "us"
|
131
|
+
end
|
132
|
+
it "should return gb for #{gb_ip}" do # Great Britain
|
133
|
+
expect(GeoLocale.country_code(ip: "#{gb_ip}")).to eq "gb"
|
134
|
+
end
|
135
|
+
it "should return ir for #{ir_ip}" do # Iran
|
136
|
+
expect(GeoLocale.country_code(ip: "#{ir_ip}")).to eq "ir"
|
137
|
+
end
|
138
|
+
it "should return ie for #{ie_ip}" do # Ireland
|
139
|
+
expect(GeoLocale.country_code(ip: "#{ie_ip}")).to eq "ie"
|
140
|
+
end
|
141
|
+
it "should return pl for #{pl_ip}" do # Poland
|
142
|
+
expect(GeoLocale.country_code(ip: "#{pl_ip}")).to eq "pl"
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
@@ -3,25 +3,11 @@
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
5
|
describe "Initializing" do
|
6
|
-
|
7
|
-
|
8
|
-
end
|
9
|
-
|
10
|
-
it "should set dev_country to en" do
|
11
|
-
expect(GeoLocale.config.dev_country).to eq "en"
|
6
|
+
it "should set default_country to us" do
|
7
|
+
expect(GeoLocale.config.default_country).to eq "us"
|
12
8
|
end
|
13
|
-
it "should
|
14
|
-
|
9
|
+
it "should change default_country to de" do
|
10
|
+
GeoLocale.config.default_country = "de"
|
11
|
+
expect(GeoLocale.config.default_country).to eq "de"
|
15
12
|
end
|
16
|
-
it "should change local_country to de" do
|
17
|
-
GeoLocale.config.local_country = "de"
|
18
|
-
expect(GeoLocale.config.local_country).to eq "de"
|
19
|
-
end
|
20
|
-
it "should change dev_country to fr" do
|
21
|
-
GeoLocale.configure do |c|
|
22
|
-
c.dev_country = "fr"
|
23
|
-
end
|
24
|
-
expect(GeoLocale.config.dev_country).to eq "fr"
|
25
|
-
end
|
26
|
-
|
27
13
|
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
# rspec spec/tests/locale_spec.rb
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe "Locale interpret" do
|
6
|
+
lv_ip = LV_IPS.sample
|
7
|
+
lt_ip = LT_IPS.sample
|
8
|
+
ee_ip = EE_IPS.sample
|
9
|
+
ru_ip = RU_IPS.sample
|
10
|
+
us_ip = US_IPS.sample
|
11
|
+
gb_ip = GB_IPS.sample
|
12
|
+
ir_ip = IR_IPS.sample
|
13
|
+
ie_ip = IE_IPS.sample
|
14
|
+
pl_ip = PL_IPS.sample
|
15
|
+
|
16
|
+
before :each do
|
17
|
+
GeoLocale.config = nil
|
18
|
+
GeoLocale.configure {}
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "from country code" do
|
22
|
+
context "with unchanged defaults" do
|
23
|
+
it "should return en if no arguments given" do
|
24
|
+
expect( GeoLocale.locale(country_code: "") ).to eq "en"
|
25
|
+
end
|
26
|
+
it 'should return lv for country_code: "lv"' do
|
27
|
+
expect( GeoLocale.locale(country_code: "lv") ).to eq "lv"
|
28
|
+
end
|
29
|
+
it 'should return en for country_code: "gb"' do
|
30
|
+
expect( GeoLocale.locale(country_code: "gb") ).to eq "en"
|
31
|
+
end
|
32
|
+
it 'should return en-gb for country_code: "gb", lcid: true' do
|
33
|
+
expect( GeoLocale.locale(country_code: "gb", lcid: true) ).to eq "en-gb"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
context "with customized config" do
|
37
|
+
it "should return de if no arguments given" do
|
38
|
+
GeoLocale.config.default_country = "de"
|
39
|
+
expect( GeoLocale.locale(country_code: "") ).to eq "de"
|
40
|
+
end
|
41
|
+
it 'should return lv for country_code: "lv"' do
|
42
|
+
GeoLocale.config.default_country = "de"
|
43
|
+
expect( GeoLocale.locale(country_code: "lv") ).to eq "lv"
|
44
|
+
end
|
45
|
+
it 'should return en for country_code: "gb"' do
|
46
|
+
GeoLocale.config.default_country = "de"
|
47
|
+
expect( GeoLocale.locale(country_code: "gb") ).to eq "en"
|
48
|
+
end
|
49
|
+
it 'should return en-gb for country_code: "gb", lcid: true' do
|
50
|
+
GeoLocale.config.default_country = "de"
|
51
|
+
expect( GeoLocale.locale(country_code: "gb", lcid: true) ).to eq "en-gb"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "from IP" do
|
57
|
+
context "with unchanged defaults" do
|
58
|
+
it "should return en if no arguments given" do
|
59
|
+
expect( GeoLocale.locale(ip: "") ).to eq "en"
|
60
|
+
end
|
61
|
+
it "should return lv for #{lv_ip}" do
|
62
|
+
expect( GeoLocale.locale(ip: lv_ip) ).to eq "lv"
|
63
|
+
end
|
64
|
+
it "should return en for #{us_ip} " do
|
65
|
+
expect( GeoLocale.locale(ip: us_ip) ).to eq "en"
|
66
|
+
end
|
67
|
+
it "should return en-ie for #{ie_ip}, lcid: true" do
|
68
|
+
expect( GeoLocale.locale(ip: ie_ip, lcid: true) ).to eq "en-ie"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
context "with customized config" do
|
72
|
+
it "should return en if only default_contry configured and no arguments given" do
|
73
|
+
GeoLocale.config.default_country = "no"
|
74
|
+
expect( GeoLocale.locale(ip: "") ).to eq "en"
|
75
|
+
end
|
76
|
+
it "should return en if only default_locale configured and no arguments given" do
|
77
|
+
GeoLocale.config.default_locale = "no"
|
78
|
+
expect( GeoLocale.locale(ip: "") ).to eq "en"
|
79
|
+
end
|
80
|
+
it 'should return "no" if both defaults configured and no arguments given' do
|
81
|
+
GeoLocale.config.default_country = "no"
|
82
|
+
GeoLocale.config.default_locale = "no"
|
83
|
+
expect( GeoLocale.locale(ip: "") ).to eq "no"
|
84
|
+
end
|
85
|
+
it "should return lv for #{lv_ip}" do
|
86
|
+
GeoLocale.config.default_country = "no"
|
87
|
+
GeoLocale.config.default_locale = "no"
|
88
|
+
expect( GeoLocale.locale(ip: lv_ip) ).to eq "lv"
|
89
|
+
end
|
90
|
+
it "should return en for #{gb_ip}" do
|
91
|
+
GeoLocale.config.default_country = "no"
|
92
|
+
GeoLocale.config.default_locale = "no"
|
93
|
+
expect( GeoLocale.locale(ip: gb_ip) ).to eq "en"
|
94
|
+
end
|
95
|
+
it "should return en-ie for #{ie_ip}, lcid: true" do
|
96
|
+
GeoLocale.config.default_country = "no"
|
97
|
+
GeoLocale.config.default_locale = "no"
|
98
|
+
expect( GeoLocale.locale(ip: ie_ip, lcid: true) ).to eq "en-ie"
|
99
|
+
end
|
100
|
+
it "should return lt for #{lt_ip}, lcid: true" do
|
101
|
+
GeoLocale.config.default_country = "no"
|
102
|
+
GeoLocale.config.default_locale = "no"
|
103
|
+
expect( GeoLocale.locale(ip: lt_ip, lcid: true) ).to eq "lt"
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: geo_locale
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Epigene
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01-
|
11
|
+
date: 2015-01-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 1.7.0
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 1.7.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -42,24 +42,24 @@ dependencies:
|
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
47
|
+
version: 3.1.0
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
54
|
+
version: 3.1.0
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: geoip
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
|
-
type: :
|
62
|
+
type: :runtime
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
@@ -67,7 +67,7 @@ dependencies:
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: geokit
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - ">="
|
@@ -81,7 +81,7 @@ dependencies:
|
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
84
|
+
name: geokit-rails
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
87
|
- - ">="
|
@@ -110,11 +110,13 @@ files:
|
|
110
110
|
- geo_locale.gemspec
|
111
111
|
- lib/data/GeoIP.dat
|
112
112
|
- lib/geo_locale.rb
|
113
|
-
- lib/geo_locale/
|
113
|
+
- lib/geo_locale/country_code.rb
|
114
|
+
- lib/geo_locale/locale.rb
|
114
115
|
- lib/geo_locale/version.rb
|
115
116
|
- spec/spec_helper.rb
|
116
|
-
- spec/tests/
|
117
|
+
- spec/tests/geolocation_spec.rb
|
117
118
|
- spec/tests/initialize_spec.rb
|
119
|
+
- spec/tests/locale_spec.rb
|
118
120
|
homepage: ''
|
119
121
|
licenses:
|
120
122
|
- MIT
|
@@ -134,12 +136,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
134
136
|
- !ruby/object:Gem::Version
|
135
137
|
version: '0'
|
136
138
|
requirements: []
|
137
|
-
rubyforge_project:
|
139
|
+
rubyforge_project: geo_locale
|
138
140
|
rubygems_version: 2.2.2
|
139
141
|
signing_key:
|
140
142
|
specification_version: 4
|
141
143
|
summary: '["A Rails 4+ gem for getting user country"]'
|
142
144
|
test_files:
|
143
145
|
- spec/spec_helper.rb
|
144
|
-
- spec/tests/
|
146
|
+
- spec/tests/geolocation_spec.rb
|
145
147
|
- spec/tests/initialize_spec.rb
|
148
|
+
- spec/tests/locale_spec.rb
|
@@ -1,25 +0,0 @@
|
|
1
|
-
module GeoLocale
|
2
|
-
|
3
|
-
def self.country_code
|
4
|
-
country_code = GeoLocale.geo_ip_try
|
5
|
-
if country_code.present?
|
6
|
-
return
|
7
|
-
else
|
8
|
-
country_code = GeoLocale.config.local_country
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
def self.geo_ip_try
|
13
|
-
begin
|
14
|
-
return GeoIP.new("#{GeoLocale::ROOT}/lib/data/GeoIP.dat").country(request.remote_ip).country_code2.downcase
|
15
|
-
rescue
|
16
|
-
nil
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def _try
|
21
|
-
# request.location is handled by Geocoder, fails often
|
22
|
-
# request.location.country_code.downcase
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
# rspec spec/tests/geo_ip_part_spec.rb
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
describe "GeoIP part" do
|
6
|
-
before :all do
|
7
|
-
|
8
|
-
end
|
9
|
-
|
10
|
-
it "status should be sent" do
|
11
|
-
# request = stub(host: 'dietbikini.com')
|
12
|
-
# GeoLocale.any_instance.stub request: request
|
13
|
-
helper.request.stub(:path).and_return("/asdf.asdf")
|
14
|
-
request = ActionController::TestRequest.new(host: "www.google.lv", remote_ip: "146.185.168.135")
|
15
|
-
expect(GeoLocale.geo_ip_try).to eq "xx"
|
16
|
-
end
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
end
|