Initial commit
This commit is contained in:
29
lib/barong/geo_ip.rb
Normal file
29
lib/barong/geo_ip.rb
Normal file
@@ -0,0 +1,29 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Barong
|
||||
# MaxmindDB reader adapter
|
||||
module GeoIP
|
||||
class << self
|
||||
attr_accessor :lang
|
||||
|
||||
# Usage: city = Barong::GeoIP.get(ip: ip, key: :city)
|
||||
def info(ip:, key:)
|
||||
record = reader.get(ip)
|
||||
return unless record
|
||||
|
||||
case key.to_sym
|
||||
when :country
|
||||
return record['country']['names'][lang] if record['country']
|
||||
when :continent
|
||||
return record['continent']['names'][lang] if record['continent']
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def reader
|
||||
@reader ||= MaxMind::DB.new(Barong::App.config.maxminddb_path, mode: MaxMind::DB::MODE_MEMORY)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user