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.



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
86
# File 'lib/maxmind/geoip2/reader.rb', line 57

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 Anonymous IP 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.



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

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

#anonymous_plus(ip_address) ⇒ MaxMind::GeoIP2::Model::AnonymousPlus

Look up the IP address in the Anonymous Plus 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 Plus 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.



129
130
131
132
133
134
135
136
# File 'lib/maxmind/geoip2/reader.rb', line 129

def anonymous_plus(ip_address)
  flat_model_for(
    Model::AnonymousPlus,
    'anonymous_plus',
    'GeoIP-Anonymous-Plus',
    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.



153
154
155
# File 'lib/maxmind/geoip2/reader.rb', line 153

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.



172
173
174
# File 'lib/maxmind/geoip2/reader.rb', line 172

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.



286
287
288
# File 'lib/maxmind/geoip2/reader.rb', line 286

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.



191
192
193
194
195
196
197
198
# File 'lib/maxmind/geoip2/reader.rb', line 191

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.



215
216
217
# File 'lib/maxmind/geoip2/reader.rb', line 215

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.



234
235
236
# File 'lib/maxmind/geoip2/reader.rb', line 234

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.



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

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.



272
273
274
# File 'lib/maxmind/geoip2/reader.rb', line 272

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)


279
280
281
# File 'lib/maxmind/geoip2/reader.rb', line 279

def 
  @reader.
end