Class: MaxMind::GeoIP2::Reader
- Inherits:
-
Object
- Object
- MaxMind::GeoIP2::Reader
- 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
-
#anonymous_ip(ip_address) ⇒ MaxMind::GeoIP2::Model::AnonymousIP
Look up the IP address in the database.
-
#asn(ip_address) ⇒ MaxMind::GeoIP2::Model::ASN
Look up the IP address in an ASN database.
-
#city(ip_address) ⇒ MaxMind::GeoIP2::Model::City
Look up the IP address in a City database.
-
#close ⇒ void
Close the Reader and return resources to the system.
-
#connection_type(ip_address) ⇒ MaxMind::GeoIP2::Model::ConnectionType
Look up the IP address in a Connection Type database.
-
#country(ip_address) ⇒ MaxMind::GeoIP2::Model::Country
Look up the IP address in a Country database.
-
#domain(ip_address) ⇒ MaxMind::GeoIP2::Model::Domain
Look up the IP address in a Domain database.
-
#enterprise(ip_address) ⇒ MaxMind::GeoIP2::Model::Enterprise
Look up the IP address in an Enterprise database.
-
#initialize(database: , locales: ['en'], mode: MaxMind::DB::MODE_AUTO) ⇒ Reader
constructor
Create a Reader for looking up IP addresses in a GeoIP2/GeoLite2 database file.
-
#isp(ip_address) ⇒ MaxMind::GeoIP2::Model::ISP
Look up the IP address in an ISP database.
-
#metadata ⇒ MaxMind::DB::Metadata
Return the metadata associated with the database.
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.
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? = {} [:mode] = mode if !mode.nil? @reader = MaxMind::DB.new(database, ) @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.
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.
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.
147 148 149 |
# File 'lib/maxmind/geoip2/reader.rb', line 147 def city(ip_address) model_for(Model::City, 'city', 'City', ip_address) end |
#close ⇒ void
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.
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.
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.
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.
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.
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 |
#metadata ⇒ MaxMind::DB::Metadata
Return the metadata associated with the database.
254 255 256 |
# File 'lib/maxmind/geoip2/reader.rb', line 254 def @reader. end |