All Projects → maxmind → GeoIP2-ruby

maxmind / GeoIP2-ruby

Licence: Apache-2.0, MIT licenses found Licenses found Apache-2.0 LICENSE-APACHE MIT LICENSE-MIT
Ruby API for GeoIP2 webservice client and database reader

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to GeoIP2-ruby

MaxMind-DB-Reader-ruby
Ruby reader for the MaxMind DB Database Format
Stars: ✭ 35 (-14.63%)
Mutual labels:  geoip2, geoip, maxmind, mmdb
GeoIP2-perl
Perl API for MaxMind's GeoIP2 web services and databases
Stars: ✭ 18 (-56.1%)
Mutual labels:  geoip2, geoip, maxmind, mmdb
MaxMind-DB-Reader-python
Python MaxMind DB reader extension
Stars: ✭ 131 (+219.51%)
Mutual labels:  geoip2, geoip, maxmind, mmdb
Geoip2 Php
PHP API for GeoIP2 webservice client and database reader
Stars: ✭ 1,956 (+4670.73%)
Mutual labels:  geoip2, geoip, maxmind, mmdb
MaxMind-DB-Writer-perl
Create MaxMind DB database files
Stars: ✭ 63 (+53.66%)
Mutual labels:  geoip2, geoip, maxmind, mmdb
MaxMind-DB-Reader-java
Java reader for the MaxMind DB format
Stars: ✭ 88 (+114.63%)
Mutual labels:  geoip2, geoip, maxmind, mmdb
mmdbwriter
Go library for writing MaxMind DB (mmdb) files
Stars: ✭ 44 (+7.32%)
Mutual labels:  geoip2, geoip, maxmind, mmdb
locus
MMDB reader for geolocation and ASN lookup of IP addresses
Stars: ✭ 93 (+126.83%)
Mutual labels:  geoip2, geoip, mmdb
geoip2-rs
Fast GeoIP2 Reader for Rust
Stars: ✭ 25 (-39.02%)
Mutual labels:  geoip2, geoip, maxmind
geoip2
GeoIP2 Reader for Go
Stars: ✭ 54 (+31.71%)
Mutual labels:  geoip2, geoip, maxmind
nodejs-geoip2ws
Maxmind GeoIP2 Web Services for Node.js
Stars: ✭ 47 (+14.63%)
Mutual labels:  geoip2, geoip, maxmind
GeoLite2-Country
GeoLite2-Country.mmdb.gz CDN files based on Free Open Source CDN jsDelivr!
Stars: ✭ 69 (+68.29%)
Mutual labels:  geoip, maxmind
lua-resty-maxminddb
A Lua library for reading MaxMind's Geolocation database
Stars: ✭ 72 (+75.61%)
Mutual labels:  geoip, maxmind
sawmill
Sawmill is a JSON transformation Java library
Stars: ✭ 92 (+124.39%)
Mutual labels:  geoip, maxmind
GeoLite2-City
GeoLite2-City.mmdb.gz CDN files based on Free Open Source CDN jsDelivr!
Stars: ✭ 170 (+314.63%)
Mutual labels:  geoip, maxmind
pf-azure-sentinel
Parse pfSense/OPNSense logs using Logstash, GeoIP tag entities, add additional context to logs, then send to Azure Sentinel for analysis.
Stars: ✭ 24 (-41.46%)
Mutual labels:  geoip, maxmind
GeoIP2-CN
小巧精悍、准确、实用 GeoIP2 数据库
Stars: ✭ 3,965 (+9570.73%)
Mutual labels:  geoip2, geoip
Recon-X
Advanced Reconnaissance tool to enumerate attacking surface of the target.
Stars: ✭ 27 (-34.15%)
Mutual labels:  geoip
Geolocator-2
Learn how to find and work with locations in Django, the Yelp API, and Google Maps api.
Stars: ✭ 24 (-41.46%)
Mutual labels:  geoip2
minfraud-api-ruby
Ruby API for minFraud Score, Insights, and Factors
Stars: ✭ 14 (-65.85%)
Mutual labels:  maxmind

GeoIP2 Ruby API

Description

This is the Ruby API for the GeoIP2 and GeoLite2 webservices and databases.

Installation

gem install maxmind-geoip2

IP Geolocation Usage

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.

Database Reader

Usage

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.

City Example

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

Country Example

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'] # '美国'

Enterprise Example

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

Anonymous IP Example

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

ASN Example

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

Connection Type Example

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

Domain Example

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

ISP Example

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

Web Service Client

Usage

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.

Example

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',
)

# 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

Values to use for Database or Array Keys

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

What data is returned?

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.

Integration with GeoNames

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.

Reporting data problems

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.

Support

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.

Requirements

This code requires Ruby version 2.5 or higher.

Contributing

Patches and pull requests are encouraged. Please include unit tests whenever possible.

Versioning

This library uses Semantic Versioning.

Copyright and License

This software is Copyright (c) 2020-2022 by MaxMind, Inc.

This is free software, licensed under the Apache License, Version 2.0 or the MIT License, at your option.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].