Class: GeoIP2::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/geoip2/database.rb,
ext/geoip2/geoip2.c

Instance Method Summary collapse

Constructor Details

#initialize(path, symbolize_keys: false) ⇒ Database

Returns a new instance of Database.



3
4
5
6
# File 'lib/geoip2/database.rb', line 3

def initialize(path, symbolize_keys: false)
  @symbolize_keys = !!symbolize_keys
  open_mmdb(path)
end

Instance Method Details

#closeObject



274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'ext/geoip2/geoip2.c', line 274

static VALUE
rb_geoip2_db_close(VALUE self)
{
  MMDB_s *mmdb;

  TypedData_Get_Struct(self, struct MMDB_s, &rb_mmdb_type, mmdb);

  if (!mmdb_is_closed(mmdb)) {
    mmdb_close(mmdb);
  }

  return Qnil;
}

#lookup(ip) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'ext/geoip2/geoip2.c', line 288

static VALUE
rb_geoip2_db_lookup(VALUE self, VALUE ip)
{
  char *ip_str;
  MMDB_s *mmdb;
  MMDB_lookup_result_s result;
  MMDB_lookup_result_s *result_ptr;
  VALUE obj;

  Check_Type(ip, T_STRING);
  ip_str = StringValueCStr(ip);

  TypedData_Get_Struct(self, struct MMDB_s, &rb_mmdb_type, mmdb);
  result = mmdb_lookup(mmdb, ip_str, false);

  if (!result.found_entry) {
    return Qnil;
  }

  obj = TypedData_Make_Struct(rb_cGeoIP2LookupResult,
                              struct MMDB_lookup_result_s,
                              &rb_lookup_result_type,
                              result_ptr);
  result_ptr->found_entry = result.found_entry;
  result_ptr->entry = result.entry;
  result_ptr->netmask = result.netmask;

  rb_iv_set(obj, "@symbolize_keys", rb_iv_get(self, "@symbolize_keys"));

  return obj;
}

#open_mmdb(path) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'ext/geoip2/geoip2.c', line 258

static VALUE
rb_geoip2_db_open_mmdb(VALUE self, VALUE path)
{
  char *db_path;
  MMDB_s *mmdb;

  Check_Type(path, T_STRING);

  db_path = StringValueCStr(path);

  TypedData_Get_Struct(self, struct MMDB_s, &rb_mmdb_type, mmdb);
  mmdb_open(db_path, mmdb);

  return Qnil;
}