Class: MaxMindDB

Inherits:
Object
  • Object
show all
Defined in:
ext/mmdb/mmdb.c

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'ext/mmdb/mmdb.c', line 127

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

#closeObject



252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'ext/mmdb/mmdb.c', line 252

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(*args) ⇒ Object

mmdb.lookup(“123.45.67.89”) -> Hash (Default values is English) mmdb.lookup(“123.45.67.89”, “ja”) -> Hash (Values is Japanese)



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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'ext/mmdb/mmdb.c', line 154

VALUE
maxminddb_lookup(int argc, VALUE* argv, VALUE self) {
    VALUE ret;

    int gai_error, mmdb_error;
    const char *ip, *lang;
    struct MaxMindDB *ptr = MaxMindDB(self);
    MMDB_lookup_result_s lookuped;
    MMDB_entry_data_s data;
    MMDB_entry_s *entry;

    if (NIL_P(argv[0])) {
        return Qnil;
    }
    ip = StringValuePtr(argv[0]);

    switch (argc) {
        case 1:
            lang = en;
            break;
        case 2:
            lang = StringValuePtr(argv[1]);
            break;
    }


    lookuped = MMDB_lookup_string(ptr->mmdb, ip, &gai_error, &mmdb_error);
    if (gai_error) {
        rb_raise(rb_eTypeError, "%s", gai_strerror(gai_error));
    }
    if (mmdb_error) {
        rb_raise(rb_eTypeError, "%s", MMDB_strerror(mmdb_error));
    }

    if (lookuped.found_entry) {
        ret = rb_hash_new();
        entry = &lookuped.entry;

        MMDB_get_value(entry, &data, city, names, lang, NULL);
        maxminddb_set_result(ret, rb_sym_city, &data);

        MMDB_get_value(entry, &data, country, names, lang, 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, lang, 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();
                unsigned int i, n;
                unsigned int cnt = 0;
                char *index;

                // need to specify index as char*
                n = data.data_size;
                number_of_digits(n, cnt);
                index = (char *)malloc(sizeof(char)*cnt+1);
                memset(index, 0, sizeof(char)*cnt+1);

                for (i = 0; i < data.data_size; i++) {
                    sprintf(index, "%d", i);
                    MMDB_get_value(entry, &data, subdivisions, index, names, lang, 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));
                        }
                    }
                }

                free(index);

                rb_hash_aset(ret, rb_sym_subdivisions, ary);
            }
        } else {
            rb_hash_aset(ret, rb_sym_subdivisions, Qnil);
        }
    } else {
        ret = Qnil;
    }

    return ret;
}