mmdb 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/ext/mmdb/mmdb.c +41 -0
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e0276f5f017337d1f301c655e11e52c090d8114e
4
- data.tar.gz: 3f462b56c286843a40c67011d3152bef5165d0d9
3
+ metadata.gz: 28afec18a26cb57a2d9274d5aca15b99e71036ab
4
+ data.tar.gz: 8a1111e0382d6a033288143fcce6a66d6db6a208
5
5
  SHA512:
6
- metadata.gz: 7b0da96b547bae7bf9ee2edae5f019d56cd57a5904028c6b815921e9d92e9e240ff339f2885d9a0708a35007d4c5d142aa7234bba86a2c3601be48e9338a2f3f
7
- data.tar.gz: 304f0280000e95fbcdfcbd5606b013903e53bd5b51b2aba04e17e42b1402b62aaf19993b099effdf2ca0aac2d939343bf5dc7807090ffda69b15bc835aca2aac
6
+ metadata.gz: a101f8fc21977db024fd74503f05030a91076a38375212c0315c7f2f0ddbee712d0be7684be84e900f71a7e3805ae2e313d4972116fc59798fce414a8c1108ea
7
+ data.tar.gz: eeecbe7c806621a46815525190897ed2c8c4777b286d6feb0560f89d93b01c1d661bb08408d5786a1d8721f9c072401d8f169fb9b9bb9312b9abe6f43dc84964
data/ext/mmdb/mmdb.c CHANGED
@@ -15,6 +15,7 @@ static VALUE rb_sym_continent;
15
15
  static VALUE rb_sym_latitude;
16
16
  static VALUE rb_sym_longitude;
17
17
  static VALUE rb_sym_postcode;
18
+ static VALUE rb_sym_subdivisions;
18
19
 
19
20
  const static char *en = "en";
20
21
  const static char *names = "names";
@@ -29,6 +30,7 @@ const static char *postal = "postal";
29
30
  const static char *code = "code";
30
31
  const static char *iso_code = "iso_code";
31
32
  const static char *postcode = "postcode";
33
+ const static char *subdivisions = "subdivisions";
32
34
 
33
35
  struct MaxMindDB {
34
36
  MMDB_s *mmdb;
@@ -69,6 +71,10 @@ static const rb_data_type_t mmdb_data_type = {
69
71
  };
70
72
 
71
73
  #define check_maxminddb(self) ((struct MaxMindDB*)rb_check_typeddata((self), &mmdb_data_type))
74
+ #define number_of_digits(n, count) do { \
75
+ n /= 10; \
76
+ count++; \
77
+ } while (n != 0)
72
78
 
73
79
  static struct MaxMindDB*
74
80
  get_maxminddb(VALUE self) {
@@ -157,20 +163,54 @@ maxminddb_lookup(VALUE self, VALUE ip) {
157
163
  ret = rb_hash_new();
158
164
  MMDB_entry_data_s data;
159
165
  MMDB_entry_s *entry = &lookuped.entry;
166
+
160
167
  MMDB_get_value(entry, &data, city, names, en, NULL);
161
168
  maxminddb_set_result(ret, rb_sym_city, &data);
169
+
162
170
  MMDB_get_value(entry, &data, country, names, en, NULL);
163
171
  maxminddb_set_result(ret, rb_sym_country, &data);
172
+
164
173
  MMDB_get_value(entry, &data, country, iso_code, NULL);
165
174
  maxminddb_set_result(ret, rb_sym_country_code, &data);
175
+
166
176
  MMDB_get_value(entry, &data, continent, names, en, NULL);
167
177
  maxminddb_set_result(ret, rb_sym_continent, &data);
178
+
168
179
  MMDB_get_value(entry, &data, location, latitude, NULL);
169
180
  maxminddb_set_result(ret, rb_sym_latitude, &data);
181
+
170
182
  MMDB_get_value(entry, &data, location, longitude, NULL);
171
183
  maxminddb_set_result(ret, rb_sym_longitude, &data);
184
+
172
185
  MMDB_get_value(entry, &data, postal, code, NULL);
173
186
  maxminddb_set_result(ret, rb_sym_postcode, &data);
187
+
188
+ MMDB_get_value(entry, &data, subdivisions, NULL);
189
+ if (data.has_data) {
190
+ // subdivisions fields is basically array
191
+ if (data.type == MMDB_DATA_TYPE_ARRAY) {
192
+ VALUE ary = rb_ary_new();
193
+ int n, data_size;
194
+ int count = 0;
195
+ n = data_size = data.data_size;
196
+ number_of_digits(n, count);
197
+ char index[count+1];
198
+
199
+ for (int i = 0; i < data_size; i++) {
200
+ sprintf(index, "%d", i);
201
+ MMDB_get_value(entry, &data, subdivisions, index, names, en, NULL);
202
+ if (data.has_data) {
203
+ if (data.type == MMDB_DATA_TYPE_UTF8_STRING) {
204
+ rb_ary_push(ary, rb_utf8_str_new(data.utf8_string, data.data_size));
205
+ }
206
+ }
207
+ }
208
+
209
+ rb_hash_aset(ret, rb_sym_subdivisions, ary);
210
+ }
211
+ } else {
212
+ rb_hash_aset(ret, rb_sym_subdivisions, Qnil);
213
+ }
174
214
  } else {
175
215
  ret = Qnil;
176
216
  }
@@ -201,6 +241,7 @@ maxminddb_init_rb_variables() {
201
241
  rb_sym_latitude = ID2SYM(rb_intern(latitude));
202
242
  rb_sym_longitude = ID2SYM(rb_intern(longitude));
203
243
  rb_sym_postcode = ID2SYM(rb_intern(postcode));
244
+ rb_sym_subdivisions = ID2SYM(rb_intern(subdivisions));
204
245
  }
205
246
 
206
247
  void
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mmdb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - yoppi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-05 00:00:00.000000000 Z
11
+ date: 2016-10-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler