Class: GeoIP2::LookupResult
- Inherits:
-
Data
- Object
- Data
- GeoIP2::LookupResult
- Defined in:
- lib/geoip2/lookup_result.rb,
ext/geoip2/geoip2.c
Instance Method Summary collapse
Constructor Details
#initialize ⇒ Object
304 305 306 307 308 |
# File 'ext/geoip2/geoip2.c', line 304
static VALUE
rb_geoip2_lr_initialize(VALUE self)
{
return Qnil;
}
|
Instance Method Details
#dig(*keys) ⇒ Object
3 4 5 |
# File 'lib/geoip2/lookup_result.rb', line 3 def dig(*keys) get_value(*keys) end |
#get_value(*args) ⇒ Object
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'ext/geoip2/geoip2.c', line 320
static VALUE
rb_geoip2_lr_get_value(int argc, VALUE *argv, VALUE self)
{
VALUE arg;
VALUE rest;
char **path;
int i = 0;
MMDB_lookup_result_s *result = NULL;
MMDB_entry_s entry;
MMDB_entry_data_s entry_data;
char *tmp;
VALUE e;
int status;
rb_scan_args(argc, argv, "1*", &arg, &rest);
switch(TYPE(arg)) {
case T_STRING:
case T_SYMBOL:
{
break;
}
default:
rb_raise(rb_eArgError, "Expected a String or a Symbol");
break;
}
path = malloc(sizeof(char *) * (RARRAY_LEN(rest) + 2));
TypedData_Get_Struct(self,
struct MMDB_lookup_result_s,
&rb_lookup_result_type,
result);
path[i] = rb_geoip2_lr_arg_convert_to_cstring(arg);
while (RARRAY_LEN(rest) != 0) {
++i;
e = rb_ary_shift(rest);
tmp = rb_geoip2_lr_arg_convert_to_cstring(e);
path[i] = tmp;
}
path[i+1] = NULL;
entry = result->entry;
status = MMDB_aget_value(&entry, &entry_data, (const char *const *const)path);
if (status != MMDB_SUCCESS) {
free(path);
/* fprintf(stderr, "%s:%s\n", __FUNCTION__, MMDB_strerror(status)); */
return Qnil;
}
if (!entry_data.has_data) {
free(path);
return Qnil;
}
if (entry_data.type == MMDB_DATA_TYPE_MAP ||
entry_data.type == MMDB_DATA_TYPE_ARRAY) {
// FIXME optimize below code
VALUE array = rb_ary_new();
VALUE hash;
VALUE val;
for (int j = 0; path[j] != NULL; j++) {
rb_ary_push(array, rb_str_new_cstr(path[j]));
}
hash = rb_funcall(self, rb_intern("to_h"), 0);
val = rb_apply(hash, rb_intern("dig"), array);
free(path);
return val;
}
free(path);
return mmdb_entry_data_decode(&entry_data);
}
|
#to_h ⇒ Object
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 |
# File 'ext/geoip2/geoip2.c', line 396
static VALUE
rb_geoip2_lr_to_h(VALUE self)
{
MMDB_lookup_result_s *result = NULL;
MMDB_entry_data_list_s *entry_data_list = NULL;
VALUE hash;
VALUE cache;
int status = 0;
int exception = 0;
cache = rb_ivar_get(self, rb_intern("rb_hash"));
if (!NIL_P(cache)) {
return cache;
}
TypedData_Get_Struct(self,
struct MMDB_lookup_result_s,
&rb_lookup_result_type,
result);
status = MMDB_get_entry_data_list(&result->entry, &entry_data_list);
if (status != MMDB_SUCCESS) {
rb_raise(rb_eGeoIP2Error, "%s", MMDB_strerror(status));
}
hash = rb_protect(mmdb_guard_parse_entry_data_list, (VALUE)entry_data_list, &exception);
MMDB_free_entry_data_list(entry_data_list);
if (exception != 0) {
rb_jump_tag(exception);
}
rb_ivar_set(self, rb_intern("rb_hash"), hash);
return hash;
}
|