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(database: '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'], mode: MaxMind::DB::MODE_AUTO) ⇒ 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.

Parameters:

  • database (String) (defaults to: )

    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.

  • mode (Symbol) (defaults to: MaxMind::DB::MODE_AUTO)

    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.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/maxmind/geoip2/reader.rb', line 56

def initialize(*args)
  # This if statement is to let us support calling as though we are using
  # Ruby 2.0 keyword arguments. We can't use keyword argument syntax as
  # we want to be backwards compatible with the old way we accepted
  # parameters, which looked like:
  # def initialize(database, locales = ['en'], options = {})
  if args.length == 1 && args[0].instance_of?(Hash)
    database = args[0][:database]
    locales = args[0][:locales]
    mode = args[0][:mode]
  else
    database = args[0]
    locales = args[1]
    mode = args[2].instance_of?(Hash) ? args[2][:mode] : nil
  end

  if !database.instance_of?(String)
    raise ArgumentError, 'Invalid database parameter'
  end

  locales = ['en'] if locales.nil? || locales.empty?

  options = {}
  options[:mode] = mode if !mode.nil?
  @reader = MaxMind::DB.new(database, options)

  @type = @reader..database_type

  @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.



104
105
106
107
108
109
110
111
# File 'lib/maxmind/geoip2/reader.rb', line 104

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.



128
129
130
# File 'lib/maxmind/geoip2/reader.rb', line 128

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.



147
148
149
# File 'lib/maxmind/geoip2/reader.rb', line 147

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.



261
262
263
# File 'lib/maxmind/geoip2/reader.rb', line 261

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.



166
167
168
169
170
171
172
173
# File 'lib/maxmind/geoip2/reader.rb', line 166

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.



190
191
192
# File 'lib/maxmind/geoip2/reader.rb', line 190

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.



209
210
211
# File 'lib/maxmind/geoip2/reader.rb', line 209

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.



228
229
230
# File 'lib/maxmind/geoip2/reader.rb', line 228

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.



247
248
249
# File 'lib/maxmind/geoip2/reader.rb', line 247

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)


254
255
256
# File 'lib/maxmind/geoip2/reader.rb', line 254

def 
  @reader.
end