Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
This is the Ruby API for the GeoIP2 and GeoLite2 webservices and databases.
gem install maxmind-geoip2
IP geolocation is inherently imprecise. Locations are often near the center of the population. Any location provided by a GeoIP2 database or web service should not be used to identify a particular address or household.
To use this API, you must create a new MaxMind::GeoIP2::Reader
object
with the path to the database file as the first argument to the
constructor. You may then call the method corresponding to the database you
are using.
If the lookup succeeds, the method call will return a model class for the record in the database. This model in turn contains multiple container objects for the different parts of the data such as the city in which the IP address is located.
If the record is not found, a MaxMind::GeoIP2::AddressNotFoundError
exception is thrown. If the database is invalid or corrupt, a
MaxMind::DB::InvalidDatabaseError
exception will be thrown.
See the API documentation for more details.
require 'maxmind/geoip2'
# This creates the Reader object which should be reused across lookups.
reader = MaxMind::GeoIP2::Reader.new(
database: '/usr/share/GeoIP/GeoIP2-City.mmdb',
)
record = reader.city('128.101.101.101')
puts record.country.iso_code # US
puts record.country.name # United States
puts record.country.names['zh-CN'] # '美国'
puts record.most_specific_subdivision.name # Minnesota
puts record.most_specific_subdivision.iso_code # MN
puts record.city.name # Minneapolis
puts record.postal.code # 55455
puts record.location.latitude # 44.9733
puts record.location.longitude # -93.2323
puts record.traits.network # 128.101.101.101/32
require 'maxmind/geoip2'
# This creates the Reader object which should be reused across lookups.
reader = MaxMind::GeoIP2::Reader.new(
database: '/usr/share/GeoIP/GeoIP2-Country.mmdb',
)
record = reader.country('128.101.101.101')
puts record.country.iso_code # US
puts record.country.name # United States
puts record.country.names['zh-CN'] # '美国'
require 'maxmind/geoip2'
# This creates the Reader object which should be reused across lookups.
reader = MaxMind::GeoIP2::Reader.new(
database: '/usr/share/GeoIP/GeoIP2-Enterprise.mmdb',
)
record = reader.enterprise('128.101.101.101')
puts record.country.confidence # 99
puts record.country.iso_code # US
puts record.country.name # United States
puts record.country.names['zh-CN'] # '美国'
puts record.most_specific_subdivision.confidence # 77
puts record.most_specific_subdivision.name # Minnesota
puts record.most_specific_subdivision.iso_code # MN
puts record.city.confidence # 60
puts record.city.name # Minneapolis
puts record.postal.code # 55455
puts record.location.accuracy_radius # 50
puts record.location.latitude # 44.9733
puts record.location.longitude # -93.2323
puts record.traits.network # 128.101.101.101/32
require 'maxmind/geoip2'
# This creates the Reader object which should be reused across lookups.
reader = MaxMind::GeoIP2::Reader.new(
database: '/usr/share/GeoIP/GeoIP2-Anonymous-IP.mmdb',
)
record = reader.anonymous_ip('128.101.101.101')
puts "Anonymous" if record.is_anonymous
require 'maxmind/geoip2'
# This creates the Reader object which should be reused across lookups.
reader = MaxMind::GeoIP2::Reader.new(
database: '/usr/share/GeoIP/GeoLite2-ASN.mmdb',
)
record = reader.asn('128.101.101.101')
puts record.autonomous_system_number # 1234
puts record.autonomous_system_organization # Example Ltd
require 'maxmind/geoip2'
# This creates the Reader object which should be reused across lookups.
reader = MaxMind::GeoIP2::Reader.new(
database: '/usr/share/GeoIP/GeoIP2-Connection-Type.mmdb',
)
record = reader.connection_type('128.101.101.101')
puts record.connection_type # Cable/DSL
require 'maxmind/geoip2'
# This creates the Reader object which should be reused across lookups.
reader = MaxMind::GeoIP2::Reader.new(
database: '/usr/share/GeoIP/GeoIP2-Domain.mmdb',
)
record = reader.domain('128.101.101.101')
puts record.domain # example.com
require 'maxmind/geoip2'
# This creates the Reader object which should be reused across lookups.
reader = MaxMind::GeoIP2::Reader.new(
database: '/usr/share/GeoIP/GeoIP2-ISP.mmdb',
)
record = reader.isp('128.101.101.101')
puts record.autonomous_system_number # 217
puts record.autonomous_system_organization # University of Minnesota
puts record.isp # University of Minnesota
puts record.organization # University of Minnesota
To use this API, you must create a new MaxMind::GeoIP2::Client
object
with your account ID and license key. To use the GeoLite2 web service, you
may also set the host
parameter to geolite.info
. You may then you call
the method corresponding to a specific end point, passing it the IP address
you want to look up.
If the request succeeds, the method call will return a model class for the end point you called. This model in turn contains multiple record classes, each of which represents part of the data returned by the web service.
If there is an error, a structured exception is thrown.
See the API documentation for more details.
require 'maxmind/geoip2'
# This creates a Client object that can be reused across requests.
# Replace "42" with your account ID and "license_key" with your license
# key.
client = MaxMind::GeoIP2::Client.new(
account_id: 42,
license_key: 'license_key',
# To use the GeoLite2 web service instead of the GeoIP2 web service, set
# the host parameter to "geolite.info":
# host: 'geolite.info',
# To use the Sandbox GeoIP2 web service instead of the production GeoIP2
# web service, set the host parameter to "sandbox.maxmind.com":
# host: 'sandbox.maxmind.com',
)
# Replace "city" with the method corresponding to the web service that
# you are using, e.g., "country", "insights". Please note that Insights
# is only supported by the GeoIP2 web service and not the GeoLite2 web
# service.
record = client.city('128.101.101.101')
puts record.country.iso_code # US
puts record.country.name # United States
puts record.country.names['zh-CN'] # 美国
puts record.most_specific_subdivision.name # Minnesota
puts record.most_specific_subdivision.iso_code # MN
puts record.city.name # Minneapolis
puts record.postal.code # 55455
puts record.location.latitude # 44.9733
puts record.location.longitude # -93.2323
puts record.traits.network # 128.101.101.101/32
We strongly discourage you from using a value from any names
property
as a key in a database or array.
These names may change between releases. Instead we recommend using one of the following:
MaxMind::GeoIP2::Record::City
- city.geoname_id
MaxMind::GeoIP2::Record::Continent
- continent.code
or
continent.geoname_id
MaxMind::GeoIP2::Record::Country
and
MaxMind::GeoIP2::Record::RepresentedCountry
- country.iso_code
or
country.geoname_id
MaxMind::GeoIP2::Record::Subdivision
- subdivision.iso_code
or
subdivision.geoname_id
While many of the end points return the same basic records, the attributes which can be populated vary between end points. In addition, while an end point may offer a particular piece of data, MaxMind does not always have every piece of data for any given IP address.
See the GeoIP2 web service documentation for details on what data each end point may return.
The only piece of data which is always returned is the ip_address
attribute in the MaxMind::GeoIP2::Record::Traits
record.
GeoNames offers web services and downloadable
databases with data on geographical features around the world, including
populated places. They offer both free and paid premium data. Each feature
is unique identified by a geoname_id
, which is an integer.
Many of the records returned by the GeoIP2 web services and databases
include a geoname_id
property. This is the ID of a geographical feature
(city, region, country, etc.) in the GeoNames database.
Some of the data that MaxMind provides is also sourced from GeoNames. We source things like place names, ISO codes, and other similar data from the GeoNames premium data set.
If the problem you find is that an IP address is incorrectly mapped, please submit your correction to MaxMind.
If you find some other sort of mistake, like an incorrect spelling, please check the GeoNames site first. Once you've searched for a place and found it on the GeoNames map view, there are a number of links you can use to correct data ("move", "edit", "alternate names", etc.). Once the correction is part of the GeoNames data set, it will be automatically incorporated into future MaxMind releases.
If you are a paying MaxMind customer and you're not sure where to submit a correction, please contact MaxMind support for help.
Please report all issues with this code using the GitHub issue tracker.
If you are having an issue with a MaxMind service that is not specific to the client API, please see our support page.
This code requires Ruby version 2.5 or higher.
Patches and pull requests are encouraged. Please include unit tests whenever possible.
This library uses Semantic Versioning.
This software is Copyright (c) 2020-2023 by MaxMind, Inc.
This is free software, licensed under the Apache License, Version 2.0 or the MIT License, at your option.
FAQs
Unknown package
We found that maxmind-geoip2 demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.