Class: GeoIP2::LookupResult
- Inherits:
-
Object
- Object
- GeoIP2::LookupResult
- Defined in:
- lib/geoip2/lookup_result.rb,
ext/geoip2/geoip2.c
Instance Method Summary collapse
- #dig(*keys) ⇒ Object
- #get_value(*args) ⇒ Object
- #initialize ⇒ Object constructor
- #netmask ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize ⇒ Object
381 382 383 384 385 |
# File 'ext/geoip2/geoip2.c', line 381
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
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 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
# File 'ext/geoip2/geoip2.c', line 397
static VALUE
rb_geoip2_lr_get_value(int argc, VALUE *argv, VALUE self)
{
VALUE arg;
VALUE rest;
char **path;
VALUE path_tmp;
int i = 0;
LookupResult *result = NULL;
MMDB_entry_s entry;
MMDB_entry_data_s entry_data;
char *tmp;
VALUE e;
int status;
VALUE val;
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;
}
result = lookup_result_get_open(self);
/* ALLOCV_N ties the allocation to path_tmp: it is released automatically even
* if a later key conversion raises (e.g. a non-String key, or a String with
* an embedded NUL that makes StringValueCStr raise), and it raises
* NoMemoryError instead of returning NULL on allocation failure. */
path = ALLOCV_N(char *, path_tmp, RARRAY_LEN(rest) + 2);
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->result.entry;
status = MMDB_aget_value(&entry, &entry_data, (const char *const *const)path);
if (status != MMDB_SUCCESS) {
/* fprintf(stderr, "%s:%s\n", __FUNCTION__, MMDB_strerror(status)); */
ALLOCV_END(path_tmp);
return Qnil;
}
if (!entry_data.has_data) {
ALLOCV_END(path_tmp);
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;
bool symbolize_keys = RTEST(rb_iv_get(self, "@symbolize_keys"));
for (int j = 0; path[j] != NULL; j++) {
rb_ary_push(array, symbolize_keys ? ID2SYM(rb_intern(path[j])) : rb_str_new_cstr(path[j]));
}
hash = rb_funcall(self, rb_intern("to_h"), 0);
val = rb_apply(hash, rb_intern("dig"), array);
ALLOCV_END(path_tmp);
return val;
}
val = mmdb_entry_data_decode(&entry_data);
ALLOCV_END(path_tmp);
return val;
}
|
#netmask ⇒ Object
478 479 480 481 482 483 484 485 486 487 488 489 |
# File 'ext/geoip2/geoip2.c', line 478
static VALUE
rb_geoip2_lr_netmask(VALUE self)
{
LookupResult *result = NULL;
TypedData_Get_Struct(self,
LookupResult,
&rb_lookup_result_type,
result);
return UINT2NUM(result->result.netmask);
}
|
#to_h ⇒ Object
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 |
# File 'ext/geoip2/geoip2.c', line 491
static VALUE
rb_geoip2_lr_to_h(VALUE self)
{
LookupResult *result = NULL;
MMDB_entry_data_list_s *entry_data_list = NULL;
VALUE hash;
VALUE cache;
int status = 0;
int exception = 0;
if (rb_ivar_defined(self, rb_intern("rb_hash")) == Qtrue) {
cache = rb_ivar_get(self, rb_intern("rb_hash"));
if (!NIL_P(cache)) {
return cache;
}
}
result = lookup_result_get_open(self);
status = MMDB_get_entry_data_list(&result->result.entry, &entry_data_list);
if (status != MMDB_SUCCESS) {
rb_raise(rb_eGeoIP2Error, "%s", MMDB_strerror(status));
}
ParseRequest parse_request = { entry_data_list, RTEST(rb_iv_get(self, "@symbolize_keys")) };
hash = rb_protect(mmdb_guard_parse_entry_data_list, (VALUE)&parse_request, &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;
}
|