Module: MaxMind::DB::Rust
- Defined in:
- lib/maxmind/db/rust.rb,
ext/maxmind_db_rust/lib/maxmind/db/rust.rb
Overview
Rust provides a high-performance Rust-based implementation for reading MaxMind DB files.
MaxMind DB is a binary file format that stores data indexed by IP address subnets (IPv4 or IPv6).
This is a Rust-based implementation that provides significant performance improvements over pure Ruby implementations while maintaining API compatibility with the official MaxMind::DB gem.
Example
require 'maxmind/db/rust'
reader = MaxMind::DB::Rust::Reader.new(
'GeoIP2-City.mmdb',
mode: MaxMind::DB::Rust::MODE_MEMORY
)
record = reader.get('1.1.1.1')
if record.nil?
puts '1.1.1.1 was not found in the database'
else
puts record['country']['iso_code']
puts record['country']['names']['en']
end
reader.close
Using the Iterator
require 'maxmind/db/rust'
reader = MaxMind::DB::Rust::Reader.new('GeoLite2-Country.mmdb')
# Iterate over all networks in the database
reader.each do |network, data|
puts "#{network}: #{data['country']['iso_code']}"
end
# Iterate over networks within a specific subnet
reader.each('192.168.0.0/16') do |network, data|
puts "#{network}: #{data['city']['names']['en']}"
end
# Also accepts IPAddr objects
subnet = IPAddr.new('10.0.0.0/8')
reader.each(subnet) do |network, data|
puts "#{network}: #{data['country']['iso_code']}"
end
reader.close