-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- City - Country - Continent
- Loading branch information
Showing
16 changed files
with
200 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,5 @@ | ||
# frozen_string_literal:true | ||
|
||
require 'barong/app' | ||
|
||
# Store sessions in cookies | ||
|
||
Rails.application.config.session_store :cookie_store, key: '_barong_session' | ||
Barong::App.set(:session_expire_time, '1800', type: :integer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# frozen_string_literal: true | ||
|
||
module Barong | ||
# MaxmindDB reader adapter | ||
module GeoIP | ||
class << self | ||
attr_accessor :lang | ||
|
||
# Usage: country, city = Barong::GeoIP.info(request_ip, :country, :city) | ||
def info(ip, *keys) | ||
record = reader.get(ip) | ||
keys.map { |key| fetch(record, key) } | ||
end | ||
|
||
# Usage: city = Barong::GeoIP.get(ip: ip, key: :city) | ||
def get(ip:, key:) | ||
fetch(reader.get(ip), key) | ||
end | ||
|
||
private | ||
def reader | ||
@reader ||= MaxMind::DB.new(Barong::App.config.barong_maxminddb_path, mode: MaxMind::DB::MODE_MEMORY) | ||
end | ||
|
||
def fetch(record, key) | ||
return unless record | ||
|
||
case key.to_sym | ||
when :country | ||
return country(record) | ||
when :continent | ||
return continent(record) | ||
when :city | ||
return city(record) | ||
end | ||
end | ||
|
||
def country(record) | ||
record['country']['names'][lang] if record['country'] | ||
end | ||
|
||
def continent(record) | ||
record['continent']['names'][lang] if record['continent'] | ||
end | ||
|
||
def city(record) | ||
if record['city'] | ||
record['city']['names'][lang] | ||
elsif record['subdivisions'] | ||
record['subdivisions'].first['names'][lang] | ||
else | ||
nil | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
shared_context 'geoip mock' do | ||
let(:london_ip) { '196.245.163.202' } | ||
let(:tokyo_ip) { '140.227.60.114' } | ||
|
||
before do | ||
class DummyReader | ||
def get(ip) | ||
case ip | ||
when '196.245.163.202' | ||
{ | ||
'country' => { 'names' => { 'en' => 'United Kingdom' } }, | ||
'city' => { 'names' => { 'en' => 'London' } }, | ||
'continent' => { 'names' => { 'en' => 'Europe' } } | ||
} | ||
when '140.227.60.114' | ||
{ | ||
'country' => { 'names' => { 'en' => 'Japan' } }, | ||
'city' => { 'names' => { 'en' => 'Tokyo' } }, | ||
'continent' => { 'names' => { 'en' => 'Asia' } } | ||
} | ||
else | ||
{} | ||
end | ||
end | ||
end | ||
|
||
reader = DummyReader.new | ||
|
||
allow(Barong::GeoIP).to receive(:reader).and_return(reader) | ||
end | ||
end |