diff --git a/src/commands.cr b/src/commands.cr index 0f34a36..fc077db 100644 --- a/src/commands.cr +++ b/src/commands.cr @@ -4,6 +4,7 @@ require "./commands/list" require "./commands/set" require "./commands/sorted_set" require "./commands/stream" +require "./commands/geo" module Redis # All Redis commands are defined in this module. Any paradigm that needs to @@ -17,6 +18,7 @@ module Redis include Commands::Set include Commands::SortedSet include Commands::Stream + include Commands::Geo # Execute the given command and return the result from the server. Commands # must be an `Enumerable` and its `size` method must be re-entrant. diff --git a/src/commands/geo.cr b/src/commands/geo.cr new file mode 100644 index 0000000..2a38818 --- /dev/null +++ b/src/commands/geo.cr @@ -0,0 +1,67 @@ +# Namespace for types that support `Redis::Commands::Geo` commands. +module Redis::Geo + record Radius, magnitude : String, unit : Unit do + def self.new(magnitude : Int, unit : Unit) + new magnitude.to_s, unit + end + + def to_tuple + {magnitude, unit.to_s} + end + + enum Unit + M + KM + FT + MI + end + end + + enum Sort + ASC + DESC + end +end + +module Redis::Commands::Geo + def geoadd(key : String, *entries : String, nx = nil, xx = nil, ch = nil) + command = {"geoadd", key} + command += {"nx"} if nx + command += {"xx"} if xx + command += {"ch"} if ch + command += entries + + run command + end + + def geopos(key : String, *members : String) + run({"geopos", key, *members}) + end + + def geosearch( + key : String, + *, + fromlonlat lonlat : Tuple(String, String), + byradius radius : Redis::Geo::Radius, + sort : Redis::Geo::Sort? = nil, + count : Int | String | Nil = nil, + withcoord : Bool = false, + withdist : Bool = false, + ) + command = { + "geosearch", + key, + "fromlonlat", + *lonlat, + "byradius", + *radius.to_tuple, + } + + command += {sort.to_s} if sort + command += {"count", count.to_s} if count + command += {"withcoord"} if withcoord + command += {"withdist"} if withdist + + run command + end +end diff --git a/src/connection.cr b/src/connection.cr index 9816b1c..07e1703 100644 --- a/src/connection.cr +++ b/src/connection.cr @@ -212,7 +212,7 @@ module Redis # :nodoc: macro override_return_types(methods) {% for method, return_type in methods %} - {% for methods in [Commands, Commands::Hash, Commands::List, Commands::Set, Commands::SortedSet, Commands::Stream].map(&.methods.select { |m| m.name == method }).reject(&.nil?) %} + {% for methods in [Commands, Commands::Hash, Commands::List, Commands::Set, Commands::SortedSet, Commands::Stream, Commands::Geo].map(&.methods.select { |m| m.name == method }).reject(&.nil?) %} {% for m in methods %} # :nodoc: def {{method.id}}( @@ -336,6 +336,9 @@ module Redis xreadgroup: Array(Value)?, xrevrange: Array, xtrim: Int64, + + geopos: Array, + geosearch: Array, }) end