Skip to content

Commit fdf7509

Browse files
committed
Fix get_value with symbolized keys
1 parent a333d49 commit fdf7509

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

ext/geoip2/geoip2.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,9 @@ rb_geoip2_lr_get_value(int argc, VALUE *argv, VALUE self)
418418
VALUE array = rb_ary_new();
419419
VALUE hash;
420420
VALUE val;
421+
bool symbolize_keys = RTEST(rb_iv_get(self, "@symbolize_keys"));
421422
for (int j = 0; path[j] != NULL; j++) {
422-
rb_ary_push(array, rb_str_new_cstr(path[j]));
423+
rb_ary_push(array, symbolize_keys ? ID2SYM(rb_intern(path[j])) : rb_str_new_cstr(path[j]));
423424
}
424425
hash = rb_funcall(self, rb_intern("to_h"), 0);
425426
val = rb_apply(hash, rb_intern("dig"), array);

test/test_geoip2.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,47 @@ class GeoIP2Test < Test::Unit::TestCase
140140
end
141141
end
142142

143+
sub_test_case "with symbolize_keys" do
144+
setup do
145+
@db = GeoIP2::Database.new(mmdb_test_data("GeoIP2-City-Test.mmdb"), symbolize_keys: true)
146+
end
147+
148+
teardown do
149+
@db.close
150+
end
151+
152+
data do
153+
random_ip_data("::81.2.69.142/127")
154+
end
155+
156+
test "London" do |ip|
157+
result = @db.lookup(ip).to_h
158+
expected = {
159+
geoname_id: 2643743,
160+
names: {
161+
de: "London",
162+
en: "London",
163+
es: "Londres",
164+
fr: "Londres",
165+
ja: "ロンドン",
166+
"pt-BR": "Londres",
167+
ru: "Лондон"
168+
}
169+
}
170+
assert_equal(expected, result[:city])
171+
end
172+
173+
test "get_value still works with string-keys" do
174+
result = @db.lookup("81.2.69.142")
175+
assert_instance_of(Hash, result.get_value("city", "names"))
176+
end
177+
178+
test "get_value still works with symbol-keys" do
179+
result = @db.lookup("81.2.69.142")
180+
assert_instance_of(Hash, result.get_value(:city, :names))
181+
end
182+
end
183+
143184
sub_test_case "connection type" do
144185
setup do
145186
@db = GeoIP2::Database.new(mmdb_test_data("GeoIP2-Connection-Type-Test.mmdb"))

0 commit comments

Comments
 (0)