63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'ext/maxmind_geoip2/maxmind_geoip2.c', line 63
VALUE mMaxmindGeoIP2_locate(int argc, VALUE *argv, VALUE self)
{
VALUE locate_result = Qnil;
if (argc > 3 || argc == 0) {
rb_raise(rb_eArgError, "wrong number of arguments");
}
char *lang;
if (argc == 2) {
Check_Type(argv[1], T_STRING);
lang = StringValuePtr(argv[1]);
} else {
VALUE loc = rb_iv_get(self, "@_locale");
Check_Type(loc, T_STRING);
lang = StringValuePtr(loc);
}
VALUE ipaddr = argv[0];
Check_Type(ipaddr, T_STRING);
char *ip_address = StringValuePtr(ipaddr);
char *filename;
VALUE file = rb_iv_get(self, "@_file");
Check_Type(file, T_STRING);
filename = StringValuePtr(file);
MMDB_s mmdb;
int status = MMDB_open(filename, MMDB_MODE_MMAP, &mmdb);
if (MMDB_SUCCESS == status)
{
int gai_error, mmdb_error;
MMDB_lookup_result_s result =
MMDB_lookup_string(&mmdb, ip_address, &gai_error, &mmdb_error);
if (result.found_entry)
{
locate_result = rb_hash_new();
rb_hash_aset(locate_result, rb_str_new2("city"), locate_by_path(&result, "city names", lang));
rb_hash_aset(locate_result, rb_str_new2("city_geoname_id"), locate_by_path(&result, "city geoname_id", NULL));
rb_hash_aset(locate_result, rb_str_new2("country"), locate_by_path(&result, "country names", lang));
rb_hash_aset(locate_result, rb_str_new2("country_geoname_id"), locate_by_path(&result, "country geoname_id", NULL));
rb_hash_aset(locate_result, rb_str_new2("country_code"), locate_by_path(&result, "country iso_code", NULL));
rb_hash_aset(locate_result, rb_str_new2("continent"), locate_by_path(&result, "continent names", lang));
rb_hash_aset(locate_result, rb_str_new2("continent_code"), locate_by_path(&result, "continent code", NULL));
rb_hash_aset(locate_result, rb_str_new2("continent_geoname_id"), locate_by_path(&result, "continent geoname_id", NULL));
rb_hash_aset(locate_result, rb_str_new2("subdivision"), locate_by_path(&result, "subdivisions 0 names", lang));
rb_hash_aset(locate_result, rb_str_new2("subdivision_code"), locate_by_path(&result, "subdivisions 0 iso_code", NULL));
rb_hash_aset(locate_result, rb_str_new2("subdivision_geoname_id"), locate_by_path(&result, "subdivisions 0 geoname_id", NULL));
rb_hash_aset(locate_result, rb_str_new2("postal_code"), locate_by_path(&result, "postal code", NULL));
rb_hash_aset(locate_result, rb_str_new2("latitude"), locate_by_path(&result, "location latitude", NULL));
rb_hash_aset(locate_result, rb_str_new2("longitude"), locate_by_path(&result, "location longitude", NULL));
rb_hash_aset(locate_result, rb_str_new2("time_zone"), locate_by_path(&result, "location time_zone", NULL));
}
MMDB_close(&mmdb);
} else {
rb_raise(rb_eIOError, "unable to open file %s", filename);
}
return locate_result;
}
|