Class: MaxMindDB
- Inherits:
-
Object
- Object
- MaxMindDB
- Defined in:
- ext/mmdb/mmdb.c
Instance Method Summary collapse
- #close ⇒ Object
- #initialize(*args) ⇒ Object constructor
-
#lookup(ip) ⇒ Object
mmdb.lookup(“123.45.67.89”) -> Hash.
Constructor Details
#initialize(*args) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'ext/mmdb/mmdb.c', line 124 VALUE maxminddb_initialize(int argc, VALUE* argv, VALUE self) { VALUE db_path; MMDB_s *mmdb = ALLOC(MMDB_s); struct MaxMindDB *ptr; rb_scan_args(argc, argv, "1", &db_path); FilePathValue(db_path); if (MMDB_open(StringValuePtr(db_path), MMDB_MODE_MMAP, mmdb) != MMDB_SUCCESS) { rb_sys_fail_str(db_path); } ptr = ALLOC(struct MaxMindDB); DATA_PTR(self) = ptr; ptr->mmdb = mmdb; return self; } |
Instance Method Details
#close ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'ext/mmdb/mmdb.c', line 232 VALUE maxminddb_close(VALUE self) { struct MaxMindDB *ptr = MaxMindDB(self); if (ptr) { if (ptr->mmdb) { MMDB_close(ptr->mmdb); ptr->mmdb = NULL; } } return Qnil; } |
#lookup(ip) ⇒ Object
mmdb.lookup(“123.45.67.89”) -> Hash
TODO:
To receive `opts` argument that have language code and respond specified language data.
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'ext/mmdb/mmdb.c', line 152 VALUE maxminddb_lookup(VALUE self, VALUE ip) { VALUE ret; int gai_error, mmdb_error; struct MaxMindDB *ptr = MaxMindDB(self); MMDB_lookup_result_s lookuped; MMDB_entry_data_s data; MMDB_entry_s *entry; if (NIL_P(ip)) { return Qnil; } lookuped = MMDB_lookup_string(ptr->mmdb, StringValuePtr(ip), &gai_error, &mmdb_error); if (gai_error) { rb_raise(rb_eTypeError, gai_strerror(gai_error)); } if (mmdb_error) { rb_raise(rb_eTypeError, MMDB_strerror(mmdb_error)); } if (lookuped.found_entry) { ret = rb_hash_new(); entry = &lookuped.entry; MMDB_get_value(entry, &data, city, names, en, NULL); maxminddb_set_result(ret, rb_sym_city, &data); MMDB_get_value(entry, &data, country, names, en, NULL); maxminddb_set_result(ret, rb_sym_country, &data); MMDB_get_value(entry, &data, country, iso_code, NULL); maxminddb_set_result(ret, rb_sym_country_code, &data); MMDB_get_value(entry, &data, continent, names, en, NULL); maxminddb_set_result(ret, rb_sym_continent, &data); MMDB_get_value(entry, &data, location, latitude, NULL); maxminddb_set_result(ret, rb_sym_latitude, &data); MMDB_get_value(entry, &data, location, longitude, NULL); maxminddb_set_result(ret, rb_sym_longitude, &data); MMDB_get_value(entry, &data, postal, code, NULL); maxminddb_set_result(ret, rb_sym_postcode, &data); MMDB_get_value(entry, &data, subdivisions, NULL); if (data.has_data) { // subdivisions fields is basically array if (data.type == MMDB_DATA_TYPE_ARRAY) { VALUE ary = rb_ary_new(); int i; int n, data_size; int count = 0; n = data_size = data.data_size; number_of_digits(n, count); char index[count+1]; for (i = 0; i < data_size; i++) { sprintf(index, "%d", i); MMDB_get_value(entry, &data, subdivisions, index, names, en, NULL); if (data.has_data) { if (data.type == MMDB_DATA_TYPE_UTF8_STRING) { rb_ary_push(ary, rb_utf8_str_new(data.utf8_string, data.data_size)); } } } rb_hash_aset(ret, rb_sym_subdivisions, ary); } } else { rb_hash_aset(ret, rb_sym_subdivisions, Qnil); } } else { ret = Qnil; } return ret; } |