Skip to content

Commit 0daddfb

Browse files
TheSmartnikbadboy
authored andcommitted
Add georadius and georadiusbymember
1 parent 1b8bcc7 commit 0daddfb

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

lib/redis.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2710,6 +2710,40 @@ def pfmerge(dest_key, *source_key)
27102710
end
27112711
end
27122712

2713+
# Query a sorted set representing a geospatial index to fetch members matching a
2714+
# given maximum distance from a point
2715+
#
2716+
# @param [Array] args key, longitude, latitude, radius, unit(m|km|ft|mi)
2717+
# @param ['asc', 'desc'] sort sort returned items from the nearest to the farthest or the farthest to the nearest relative to the center
2718+
# @param [Integer] count limit the results to the first N matching items
2719+
# @param ['WITHDIST', 'WITHCOORD', 'WITHHASH'] options to return additional information
2720+
# @return [Array<String>] may be changed with `options`
2721+
2722+
def georadius(*args, **geoptions)
2723+
geoarguments = _geoarguments(*args, **geoptions)
2724+
2725+
synchronize do |client|
2726+
client.call([:georadius, *geoarguments])
2727+
end
2728+
end
2729+
2730+
# Query a sorted set representing a geospatial index to fetch members matching a
2731+
# given maximum distance from an already existing member
2732+
#
2733+
# @param [Array] args key, member, radius, unit(m|km|ft|mi)
2734+
# @param ['asc', 'desc'] sort sort returned items from the nearest to the farthest or the farthest to the nearest relative to the center
2735+
# @param [Integer] count limit the results to the first N matching items
2736+
# @param ['WITHDIST', 'WITHCOORD', 'WITHHASH'] options to return additional information
2737+
# @return [Array<String>] may be changed with `options`
2738+
2739+
def georadiusbymember(*args, **geoptions)
2740+
geoarguments = _geoarguments(*args, **geoptions)
2741+
2742+
synchronize do |client|
2743+
client.call([:georadiusbymember, *geoarguments])
2744+
end
2745+
end
2746+
27132747
# Interact with the sentinel command (masters, master, slaves, failover)
27142748
#
27152749
# @param [String] subcommand e.g. `masters`, `master`, `slaves`
@@ -2813,6 +2847,14 @@ def method_missing(command, *args)
28132847
end
28142848
}
28152849

2850+
def _geoarguments(*args, options: nil, sort: nil, count: nil)
2851+
args.push sort if sort
2852+
args.push 'count', count if count
2853+
args.push options if options
2854+
2855+
args.uniq
2856+
end
2857+
28162858
def _subscription(method, timeout, channels, block)
28172859
return @client.call([method] + channels) if subscribed?
28182860

test/commands_on_geo_test.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
require_relative "helper"
2+
3+
class TestCommandsGeo < Test::Unit::TestCase
4+
include Helper::Client
5+
6+
def setup
7+
super
8+
r.geoadd("Sicily", 13.361389, 38.115556, "Palermo", 15.087269, 37.502669, "Catania")
9+
end
10+
11+
def test_georadius_with_sort
12+
nearest_cities = r.georadius("Sicily", 15, 37, 200, 'km', sort: 'asc')
13+
assert_equal %w(Catania Palermo), nearest_cities
14+
15+
farthest_cities = r.georadius("Sicily", 15, 37, 200, 'km', sort: 'desc')
16+
assert_equal %w(Palermo Catania), farthest_cities
17+
end
18+
19+
def test_georadius_with_count
20+
city = r.georadius("Sicily", 15, 37, 200, 'km', count: 1)
21+
assert_equal %w(Catania), city
22+
end
23+
24+
def test_georadius_with_options_count_sort
25+
city = r.georadius("Sicily", 15, 37, 200, 'km', sort: :desc, options: :WITHDIST, count: 1)
26+
assert_equal [["Palermo", "190.4424"]], city
27+
end
28+
29+
def test_georadiusbymember_with_sort
30+
nearest_cities = r.georadiusbymember("Sicily", "Catania", 200, 'km', sort: 'asc')
31+
assert_equal %w(Catania Palermo), nearest_cities
32+
33+
farthest_cities = r.georadiusbymember("Sicily", "Catania", 200, 'km', sort: 'desc')
34+
assert_equal %w(Palermo Catania), farthest_cities
35+
end
36+
37+
def test_georadiusbymember_with_count
38+
city = r.georadiusbymember("Sicily", "Catania", 200, 'km', count: 1)
39+
assert_equal %w(Catania), city
40+
end
41+
42+
def test_georadiusbymember_with_options_count_sort
43+
city = r.georadiusbymember("Sicily", "Catania", 200, 'km', sort: :desc, options: :WITHDIST, count: 1)
44+
assert_equal [["Palermo", "166.2742"]], city
45+
end
46+
end

0 commit comments

Comments
 (0)