Class: GeoIP2::Database

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

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'ext/geoip2/geoip2.c', line 233

static VALUE
rb_geoip2_db_initialize(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;
}

Instance Method Details

#closeObject



249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'ext/geoip2/geoip2.c', line 249

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



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'ext/geoip2/geoip2.c', line 263

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;
  return obj;
}