我知道这是几年前发布的,但我最近很难为此找到一个好的最新宝石。我发现的是YotpoLtd 的 Geoip2。
在我的 Gemfile
gem 'geoip2'
设置/配置
Geoip2.configure do |conf|
# Mandatory
conf.license_key = 'Your MaxMind License Key'
conf.user_id = 'Your MaxMind User Id'
# Optional
conf.host = 'geoip.maxmind.com' # Or any host that you would like to work with
conf.base_path = '/geoip/v2.0' # Or any other version of this API
conf.parallel_requests = 5 # Or any other amount of parallel requests that you would like to use
end
使用
data = Geoip2.omni('0.0.0.0') #this call is synchronous
*注意:我相信您可以将“omni”替换为产品层的名称:城市、国家等
Errors
如果有错误,返回的哈希会有一个错误对象,所以只需检查它是否存在
if data.error
# error handling
else #still might want to check for data's existence ( if data )
#access object as you will
data.city.names.en
data.postal.code
end
有关返回的哈希的更多信息,请参阅MaxMind Web 服务文档