Skip to content

Commit

Permalink
Merge branch 'pr/115'
Browse files Browse the repository at this point in the history
  • Loading branch information
kostya committed Jul 5, 2021
2 parents 2651d83 + 6ac8e09 commit e6d13da
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
28 changes: 28 additions & 0 deletions spec/redis_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,34 @@ describe Redis do
end
end
end

it "#geosearch with FROMLONLAT BYRADIUS" do
results = redis.geosearch("Nebraska", {longitude: -96.8308123, latitude: 40.8005243}, 100, "mi")
results.size.should eq(2)
results.includes?("Lincoln").should be_true
results.includes?("Omaha").should be_true
end

it "#geosearch with FROMLONLAT BYBOX" do
results = redis.geosearch("Nebraska", {longitude: -96.8308123, latitude: 40.8005243}, {height: 100, width: 100}, "mi")
results.size.should eq(2)
results.includes?("Lincoln").should be_true
results.includes?("Omaha").should be_true
end

it "#geosearch with FROMMEMBER BYRADIUS" do
results = redis.geosearch("Nebraska", "Lincoln", 100, "mi")
results.size.should eq(2)
results.includes?("Lincoln").should be_true
results.includes?("Omaha").should be_true
end

it "#geosearch with FROMMEMBER BYBOX" do
results = redis.geosearch("Nebraska", "Lincoln", {height: 100, width: 100}, "mi")
results.size.should eq(2)
results.includes?("Lincoln").should be_true
results.includes?("Omaha").should be_true
end
end

describe "#flush" do
Expand Down
39 changes: 39 additions & 0 deletions src/redis/commands.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1906,6 +1906,45 @@ class Redis
string_array_command(q)
end

# Return the members of a sorted set of geospatial information that are within the borders of the area specified from either a member of the set or a series of coordiates
# Replaces the deprecated GEORADIUS and GEORADIUSBYMEMBER commands.
#
# **Return value**: Array(String): Returns a simple array or array of arrays, depending on the options passed
# https://redis.io/commands/geosearch#return-value
def geosearch(key, centerpoint : String | GeoCoordinate, shape : GeoRadius | GeoBox, unit, withcoord = nil, withdist = nil, withhash = nil, count = nil, sort = nil)
q = ["GEOSEARCH", namespaced(key)]
if centerpoint.is_a?(String)
q << "FROMMEMBER"
q << centerpoint
elsif centerpoint.is_a?(GeoCoordinate)
q << "FROMLONLAT"
q << centerpoint[:longitude].to_s
q << centerpoint[:latitude].to_s
end
if shape.is_a?(GeoRadius)
q << "BYRADIUS"
q << shape.to_s
elsif shape.is_a?(GeoBox)
q << "BYBOX"
q << shape[:height].to_s
q << shape[:width].to_s
end
q << unit.to_s
q << "WITHCOORD" if withcoord
q << "WITHDIST" if withdist
q << "WITHHASH" if withhash
if count
q << "COUNT"
q << count.to_s
end
q << sort.to_s if sort
string_array_command(q)
end

alias GeoCoordinate = {longitude: Float64, latitude: Float64} | {longitude: String, latitude: String}
alias GeoRadius = Int32 | String
alias GeoBox = {height: Int32 | String, width: Int32 | String}

# Concatenates the source array to the destination array.
# Is there a better way?
private def concat(destination : Array(RedisValue), source)
Expand Down

0 comments on commit e6d13da

Please sign in to comment.