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 Anonymous IP database.
-
#anonymous_plus(ip_address) ⇒ MaxMind::GeoIP2::Model::AnonymousPlus
Look up the IP address in the Anonymous Plus 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.
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? = {} [: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 Anonymous IP database.
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.
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.
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.
172 173 174 |
# File 'lib/maxmind/geoip2/reader.rb', line 172 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.
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.
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.
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.
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.
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.
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 |
#metadata ⇒ MaxMind::DB::Metadata
Return the metadata associated with the database.
279 280 281 |
# File 'lib/maxmind/geoip2/reader.rb', line 279 def @reader. end |