これが数年前に投稿されたことは知っていますが、最近、これに適した最新の宝石を見つけるのに苦労しました。私が見つけたのは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」を製品階層の名前(都市、国など)に置き換えることができると思います
エラーエラー
が発生した場合、返されるハッシュにはエラーオブジェクトが含まれるため、その存在を確認するだけです。
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
返されるハッシュの詳細については、MaxMindWebサービスのドキュメントを参照してください。