geo_locale 0.1.0 → 0.1.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: aad903af3f55a1262afb745660501b52d00eece6
4
- data.tar.gz: 8b480b35329b6d97dee4fd90825354d1db8f8b60
3
+ metadata.gz: 13683f5ea4a417b4ba4935733dd2f6def4c8e61b
4
+ data.tar.gz: ecf16608e29da3c55ceff586e93060bf08e50d98
5
5
  SHA512:
6
- metadata.gz: 337eb1b972d2d643d87dc9782929db9f101038702a69e2e87d5750ba88062a970cc5b785ce53c8058fb8b33d7c752d30710c77cb33f3fe875c65da7365032333
7
- data.tar.gz: 1ac2a1f5efcccb9ba885dac6572aa68fd5d7d6bdb7e83b8eac4a865a3a557c1156090e79077b72e625dee8c7009cac11afa189782b0d6b05870a90fc59792a4c
6
+ metadata.gz: 9e6b16e1edf3fe8555bb31bf04fa97b54e28741b94aaa359c2f7a9d20eb42a3324cb8f755a98478577f394b3eb92e44694e048f06e3b91c775339f11f579daa1
7
+ data.tar.gz: 2eed2ced8a8a41db5e41f2e17d59475dbbdc777a955465ef6925d272fb0f99172c8f4b9c7de82cf43cef18ee4a8fc6434d377de932a1f3020db06fc03aa00cc0
@@ -0,0 +1,21 @@
1
+ # Change Log
2
+ All notable changes to this project will be documented in this file.
3
+ This project adheres to [Semantic Versioning](http://semver.org/).
4
+
5
+ ## [Release candidate][ - - ]
6
+ ### Changed
7
+ -
8
+
9
+ ## [0.1.1] - 2015-10-02
10
+ ### Added
11
+ - Localhost (127.0.0.1) is now quick-resolved to a locale defined in configuration for faster devving
12
+
13
+ ## [0.1.0] - 2015-01-01
14
+ ### Added
15
+ - Initial release with basic functionality described in README
16
+ ### Changed
17
+ - Nothing
18
+ ### Fixed
19
+ - Nothing
20
+ ### Removed
21
+ - Nothing
data/README.md CHANGED
@@ -1,34 +1,25 @@
1
1
  # GeoLocale
2
-
3
2
  Simple wrapper for GeoIP gem with some redundancy on GeoKit to get a two-letter country code or locale from an IP address.
4
3
 
5
- Basic use-case: Detect application user's origin and display app in that language.
4
+ ## Basic use-case
5
+ Detect application user's origin and display app in that language.
6
6
 
7
7
  ## Requirements
8
-
9
- Ruby 2.0.0 and greater
8
+ Ruby 2.0.0 and greater.
10
9
 
11
10
  ## Installation
12
11
 
13
- Add this line to your application's Gemfile:
14
-
15
12
  ```ruby
16
- gem 'geo_locale'
13
+ # in Gemfile
14
+ gem 'geo_locale' # and bundle
17
15
  ```
18
16
 
19
- And then execute:
20
-
21
- $ bundle
22
-
23
- Or install it yourself as:
24
-
25
- $ gem install geo_locale
26
-
27
17
  ## Usage
28
18
  Configure default locales
29
19
  ```ruby
30
20
  # in /config/initializers/geo_locale.rb
31
21
  GeoLocale.configure do |config|
22
+ config.localhost_country = "lv" # set the country to return for localhost, this country's locale and lcid will also be used for localhost
32
23
  config.default_country = "de" # set to nil or false if you want to catch fails in geolocation
33
24
  config.default_locale = "en" # country->locale conversion is minimal for now, set this explicitly to ensure GeoLocale.locale returns useful value
34
25
  config.default_lcid = "en-us"
@@ -36,7 +27,7 @@ GeoLocale.configure do |config|
36
27
  end
37
28
  ```
38
29
 
39
- To get country code:
30
+ ### To get country code:
40
31
 
41
32
  def self.country_code (ip: "")
42
33
 
@@ -46,7 +37,7 @@ GeoLocale.country_code(ip: request.remote_ip)
46
37
  => "en"
47
38
  ```
48
39
 
49
- To get locale:
40
+ ### To get locale:
50
41
 
51
42
  def self.locale (ip: "", country_code: "", lcid: false)
52
43
  ```ruby
@@ -69,6 +60,7 @@ GeoLocale.locale(ip: "97.77.25.20", country_code: "lv")
69
60
 
70
61
  1. Extend the country_code => locale hash in lib/geo_locale/locale.rb
71
62
  2. Open the hash to configuration overrides
63
+ 3. Automagically make initializer file for rails projects
72
64
 
73
65
  ## Contributing
74
66
 
@@ -21,9 +21,10 @@ Gem::Specification.new do |spec|
21
21
  spec.require_paths = ["lib"]
22
22
  spec.rubyforge_project = "geo_locale"
23
23
 
24
- spec.add_development_dependency "bundler", "~> 1.7.0"
24
+ spec.add_development_dependency "bundler"
25
25
  spec.add_development_dependency "rake", "~> 10.0"
26
26
  spec.add_development_dependency 'rspec', "~> 3.1.0"
27
+ spec.add_development_dependency 'pry'
27
28
 
28
29
  spec.add_dependency 'geoip'
29
30
  spec.add_dependency 'geokit'
@@ -8,6 +8,8 @@ require 'geokit-rails'
8
8
  module GeoLocale
9
9
  class << self
10
10
  attr_accessor :config
11
+ GeoLocale::ROOT = File.expand_path("../..", __FILE__)
12
+ GeoLocale::LOCALHOST_IP = "127.0.0.1"
11
13
  end
12
14
 
13
15
  def self.configure
@@ -16,7 +18,7 @@ module GeoLocale
16
18
  end
17
19
 
18
20
  class Config
19
- attr_accessor :default_country, :default_locale, :default_lcid, :overrides
21
+ attr_accessor :localhost_country, :default_country, :default_locale, :default_lcid, :overrides
20
22
 
21
23
  def initialize
22
24
  @default_country = "us"
@@ -27,6 +29,4 @@ module GeoLocale
27
29
  end
28
30
 
29
31
  GeoLocale.configure {}
30
- ROOT = File.expand_path("../..", __FILE__)
31
-
32
32
  end
@@ -1,7 +1,8 @@
1
1
  module GeoLocale
2
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
3
 
4
- def self.country_code (ip: "") # ip = "12.12.12.12"
4
+ def self.country_code(ip: "") # ip = "12.12.12.12"
5
+ return GeoLocale.config.localhost_country if ip == GeoLocale::LOCALHOST_IP && GeoLocale.config.localhost_country.present?
5
6
  country_code = GeoLocale.geo_ip_try(ip)
6
7
  if country_code.present?
7
8
  return country_code
@@ -16,31 +17,33 @@ module GeoLocale
16
17
  end
17
18
  end
18
19
 
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}"]
20
+ private
21
+
22
+ def self.geo_ip_try(ip)
23
+ begin
24
+ return nil unless ip.match(GeoLocale::IP_REGEX).present?
25
+ code = GeoIP.new("#{GeoLocale::ROOT}/lib/data/GeoIP.dat").country(ip).country_code2.downcase
26
+ if code == "--"
27
+ return nil
28
28
  else
29
- return code
29
+ if GeoLocale.config.overrides["#{code}"].present?
30
+ return GeoLocale.config.overrides["#{code}"]
31
+ else
32
+ return code
33
+ end
30
34
  end
35
+ rescue
36
+ nil
31
37
  end
32
- rescue
33
- nil
34
38
  end
35
- end
36
39
 
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
40
+ def self.geokit_try(ip)
41
+ return nil unless ip.match(GeoLocale::IP_REGEX).present?
42
+ begin
43
+ return Geokit::Geocoders::MultiGeocoder.geocode(ip).country_code.downcase
44
+ rescue
45
+ nil
46
+ end
43
47
  end
44
- end
45
48
 
46
- end
49
+ end
@@ -1,34 +1,8 @@
1
1
  module GeoLocale
2
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
3
+ def self.locale (ip: "", country_code: nil, lcid: false)
4
+ country_code ||= GeoLocale.country_code(ip: ip)
5
+ GeoLocale.get_locale_or_lcid(country_code: country_code, lcid: lcid)
32
6
  end
33
7
 
34
8
  LOCALE = {
@@ -59,21 +33,33 @@ module GeoLocale
59
33
  "fr" => "fr"
60
34
  }
61
35
 
36
+ private
62
37
 
63
- def self.figure_out_returnable_locale(locale: "")
64
- if locale.present?
65
- return locale
66
- else
67
- return GeoLocale.config.default_locale
38
+ def self.get_locale_or_lcid(country_code:, lcid: false)
39
+ if lcid
40
+ lcid_string = GeoLocale::LCID[country_code]
41
+ return figure_out_returnable_lcid(lcid_string: lcid_string)
42
+ else
43
+ locale = GeoLocale::LOCALE[country_code]
44
+ return figure_out_returnable_locale(locale: locale)
45
+ end
68
46
  end
69
- end
70
47
 
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
48
+
49
+ def self.figure_out_returnable_locale(locale: "")
50
+ if locale.present?
51
+ return locale
52
+ else
53
+ return GeoLocale.config.default_locale
54
+ end
55
+ end
56
+
57
+ def self.figure_out_returnable_lcid(lcid_string: "")
58
+ if lcid_string.present?
59
+ return lcid_string
60
+ else
61
+ return GeoLocale.config.default_lcid
62
+ end
76
63
  end
77
- end
78
64
 
79
- end
65
+ end
@@ -1,3 +1,3 @@
1
1
  module GeoLocale
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -2,6 +2,7 @@ require 'bundler/setup'
2
2
  Bundler.setup
3
3
 
4
4
  require "geo_locale"
5
+ require "pry"
5
6
 
6
7
  RSpec.configure do |config|
7
8
  LV_IPS =["81.198.65.13", "85.9.209.244", "83.241.46.175", "78.84.147.188", "159.148.168.103"]
@@ -143,4 +143,4 @@ describe "Geolocation" do
143
143
  end
144
144
  end
145
145
 
146
- end
146
+ end
@@ -35,8 +35,9 @@ describe "Locale interpret" do
35
35
  end
36
36
  context "with customized config" do
37
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"
38
+ GeoLocale.config.default_locale = "ch"
39
+ puts GeoLocale.config.inspect
40
+ expect( GeoLocale.locale(country_code: "") ).to eq "ch"
40
41
  end
41
42
  it 'should return lv for country_code: "lv"' do
42
43
  GeoLocale.config.default_country = "de"
@@ -107,4 +108,4 @@ describe "Locale interpret" do
107
108
 
108
109
 
109
110
 
110
- end
111
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ # rspec spec/tests/localhost_spec.rb
4
+ describe "Geolocation" do
5
+ before :each do
6
+ GeoLocale.configure do |config|
7
+ config.localhost_country = "lt"
8
+ end
9
+ end
10
+
11
+ describe "Localhost" do
12
+ describe "#country_code" do
13
+ it "should return the configured country code" do
14
+ expect(GeoLocale.country_code(ip: "127.0.0.1")).to eq "lt"
15
+ end
16
+ it "should return the gem default country_code if not configured" do
17
+ GeoLocale.config = nil
18
+ GeoLocale.configure {}
19
+ expect(GeoLocale.config.default_country).to eq "us"
20
+ expect(GeoLocale.country_code(ip: "127.0.0.1")).to eq "us"
21
+ end
22
+ end
23
+
24
+ describe "#locale" do
25
+ it "should return the configured locale" do
26
+ expect(GeoLocale.locale(ip: "127.0.0.1")).to eq "lt"
27
+ end
28
+ end
29
+
30
+ describe "#locale/lcid" do
31
+ it "should return the configured lcid" do
32
+ expect(GeoLocale.locale(ip: "127.0.0.1", lcid: true)).to eq "lt"
33
+ end
34
+ end
35
+ end
36
+
37
+ end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geo_locale
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
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-12 00:00:00.000000000 Z
11
+ date: 2015-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 1.7.0
19
+ version: '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: 1.7.0
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: 3.1.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: geoip
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -103,6 +117,7 @@ extra_rdoc_files: []
103
117
  files:
104
118
  - ".gitignore"
105
119
  - ".rspec"
120
+ - CHANGELOG.md
106
121
  - Gemfile
107
122
  - LICENSE.txt
108
123
  - README.md
@@ -117,6 +132,7 @@ files:
117
132
  - spec/tests/geolocation_spec.rb
118
133
  - spec/tests/initialize_spec.rb
119
134
  - spec/tests/locale_spec.rb
135
+ - spec/tests/localhost_spec.rb
120
136
  homepage: ''
121
137
  licenses:
122
138
  - MIT
@@ -146,3 +162,4 @@ test_files:
146
162
  - spec/tests/geolocation_spec.rb
147
163
  - spec/tests/initialize_spec.rb
148
164
  - spec/tests/locale_spec.rb
165
+ - spec/tests/localhost_spec.rb