Class: MaxMind::GeoIP2::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/maxmind/geoip2/reader.rb

Overview

Reader is a reader for the GeoIP2/GeoLite2 database format. IP addresses can be looked up using the database specific methods.

Example

require 'maxmind/geoip2'

reader = MaxMind::GeoIP2::Reader.new('GeoIP2-Country.mmdb')

record = reader.country('1.2.3.4')
puts record.country.iso_code

reader.close

Instance Method Summary collapse

Constructor Details

#initialize(database, locales = ['en'], options = {}) ⇒ Reader

Create a Reader for looking up IP addresses in a GeoIP2/GeoLite2 database file.

If you're performing multiple lookups, it's most efficient to create one Reader and reuse it.

Once created, the Reader is safe to use for lookups from multiple threads. It is safe to use after forking only if you use MaxMind::DB::MODE_MEMORY or if your version of Ruby supports IO#pread.

Parameters:

  • database (String)

    a path to a GeoIP2/GeoLite2 database file.

  • locales (Array<String>) (defaults to: ['en'])

    a list of locale codes to use in the name property from most preferred to least preferred.

  • options (Hash<Symbol, Symbol>) (defaults to: {})

    options controlling the behavior of the Reader.

Options Hash (options):

  • :mode (Symbol)

    Defines how to open the database. It may be one of MaxMind::DB::MODE_AUTO, MaxMind::DB::MODE_FILE, or MaxMind::DB::MODE_MEMORY. If you don't provide one, the Reader uses MaxMind::DB::MODE_AUTO. Refer to the definition of those constants in MaxMind::DB for an explanation of their meaning.

Raises:

  • (MaxMind::DB::InvalidDatabaseError)

    if the database is corrupt or invalid.

  • (ArgumentError)

    if the mode is invalid.


58
59
60
61
62
63
# File 'lib/maxmind/geoip2/reader.rb', line 58

def initialize(database, locales = ['en'], options = {})
  @reader = MaxMind::DB.new(database, options)
  @type = @reader..database_type
  locales = ['en'] if locales.empty?
  @locales = locales
end

Instance Method Details

#anonymous_ip(ip_address) ⇒ MaxMind::GeoIP2::Model::AnonymousIP

Look up the IP address in the database.

Parameters:

  • ip_address (String)

    a string in the standard notation. It may be IPv4 or IPv6.

Returns:

Raises:

  • (ArgumentError)

    if used against a non-Anonymous IP database or if you attempt to look up an IPv6 address in an IPv4 only database.

  • (AddressNotFoundError)

    if the IP address is not found in the database.

  • (MaxMind::DB::InvalidDatabaseError)

    if the database appears corrupt.


80
81
82
83
84
85
86
87
# File 'lib/maxmind/geoip2/reader.rb', line 80

def anonymous_ip(ip_address)
  flat_model_for(
    Model::AnonymousIP,
    'anonymous_ip',
    'GeoIP2-Anonymous-IP',
    ip_address,
  )
end

#asn(ip_address) ⇒ MaxMind::GeoIP2::Model::ASN

Look up the IP address in an ASN database.

Parameters:

  • ip_address (String)

    a string in the standard notation. It may be IPv4 or IPv6.

Returns:

Raises:

  • (ArgumentError)

    if used against a non-ASN database or if you attempt to look up an IPv6 address in an IPv4 only database.

  • (AddressNotFoundError)

    if the IP address is not found in the database.

  • (MaxMind::DB::InvalidDatabaseError)

    if the database appears corrupt.


104
105
106
# File 'lib/maxmind/geoip2/reader.rb', line 104

def asn(ip_address)
  flat_model_for(Model::ASN, 'asn', 'GeoLite2-ASN', ip_address)
end

#city(ip_address) ⇒ MaxMind::GeoIP2::Model::City

Look up the IP address in a City database.

Parameters:

  • ip_address (String)

    a string in the standard notation. It may be IPv4 or IPv6.

Returns:

Raises:

  • (ArgumentError)

    if used against a non-City database or if you attempt to look up an IPv6 address in an IPv4 only database.

  • (AddressNotFoundError)

    if the IP address is not found in the database.

  • (MaxMind::DB::InvalidDatabaseError)

    if the database appears corrupt.


123
124
125
# File 'lib/maxmind/geoip2/reader.rb', line 123

def city(ip_address)
  model_for(Model::City, 'city', 'City', ip_address)
end

#closevoid

This method returns an undefined value.

Close the Reader and return resources to the system.


237
238
239
# File 'lib/maxmind/geoip2/reader.rb', line 237

def close
  @reader.close
end

#connection_type(ip_address) ⇒ MaxMind::GeoIP2::Model::ConnectionType

Look up the IP address in a Connection Type database.

Parameters:

  • ip_address (String)

    a string in the standard notation. It may be IPv4 or IPv6.

Returns:

Raises:

  • (ArgumentError)

    if used against a non-Connection Type database or if you attempt to look up an IPv6 address in an IPv4 only database.

  • (AddressNotFoundError)

    if the IP address is not found in the database.

  • (MaxMind::DB::InvalidDatabaseError)

    if the database appears corrupt.


142
143
144
145
146
147
148
149
# File 'lib/maxmind/geoip2/reader.rb', line 142

def connection_type(ip_address)
  flat_model_for(
    Model::ConnectionType,
    'connection_type',
    'GeoIP2-Connection-Type',
    ip_address,
  )
end

#country(ip_address) ⇒ MaxMind::GeoIP2::Model::Country

Look up the IP address in a Country database.

Parameters:

  • ip_address (String)

    a string in the standard notation. It may be IPv4 or IPv6.

Returns:

Raises:

  • (ArgumentError)

    if used against a non-Country database or if you attempt to look up an IPv6 address in an IPv4 only database.

  • (AddressNotFoundError)

    if the IP address is not found in the database.

  • (MaxMind::DB::InvalidDatabaseError)

    if the database appears corrupt.


166
167
168
# File 'lib/maxmind/geoip2/reader.rb', line 166

def country(ip_address)
  model_for(Model::Country, 'country', 'Country', ip_address)
end

#domain(ip_address) ⇒ MaxMind::GeoIP2::Model::Domain

Look up the IP address in a Domain database.

Parameters:

  • ip_address (String)

    a string in the standard notation. It may be IPv4 or IPv6.

Returns:

Raises:

  • (ArgumentError)

    if used against a non-Domain database or if you attempt to look up an IPv6 address in an IPv4 only database.

  • (AddressNotFoundError)

    if the IP address is not found in the database.

  • (MaxMind::DB::InvalidDatabaseError)

    if the database appears corrupt.


185
186
187
# File 'lib/maxmind/geoip2/reader.rb', line 185

def domain(ip_address)
  flat_model_for(Model::Domain, 'domain', 'GeoIP2-Domain', ip_address)
end

#enterprise(ip_address) ⇒ MaxMind::GeoIP2::Model::Enterprise

Look up the IP address in an Enterprise database.

Parameters:

  • ip_address (String)

    a string in the standard notation. It may be IPv4 or IPv6.

Returns:

Raises:

  • (ArgumentError)

    if used against a non-Enterprise database or if you attempt to look up an IPv6 address in an IPv4 only database.

  • (AddressNotFoundError)

    if the IP address is not found in the database.

  • (MaxMind::DB::InvalidDatabaseError)

    if the database appears corrupt.


204
205
206
# File 'lib/maxmind/geoip2/reader.rb', line 204

def enterprise(ip_address)
  model_for(Model::Enterprise, 'enterprise', 'Enterprise', ip_address)
end

#isp(ip_address) ⇒ MaxMind::GeoIP2::Model::ISP

Look up the IP address in an ISP database.

Parameters:

  • ip_address (String)

    a string in the standard notation. It may be IPv4 or IPv6.

Returns:

Raises:

  • (ArgumentError)

    if used against a non-ISP database or if you attempt to look up an IPv6 address in an IPv4 only database.

  • (AddressNotFoundError)

    if the IP address is not found in the database.

  • (MaxMind::DB::InvalidDatabaseError)

    if the database appears corrupt.


223
224
225
# File 'lib/maxmind/geoip2/reader.rb', line 223

def isp(ip_address)
  flat_model_for(Model::ISP, 'isp', 'GeoIP2-ISP', ip_address)
end

#metadataMaxMind::DB::Metadata

Return the metadata associated with the database.

Returns:

  • (MaxMind::DB::Metadata)

230
231
232
# File 'lib/maxmind/geoip2/reader.rb', line 230

def 
  @reader.
end