Module: FakeRedis::GeoCommands

Defined in:
lib/fakeredis/geo_commands.rb

Constant Summary collapse

DISTANCE_UNITS =
{
  "m" => 1,
  "km" => 1000,
  "ft" => 0.3048,
  "mi" => 1609.34
}
REDIS_DOUBLE_PRECISION =
4
REDIS_GEOHASH_SIZE =
10

Instance Method Summary collapse

Instance Method Details

#geoadd(key, *members) ⇒ Object


15
16
17
18
19
20
21
22
23
24
# File 'lib/fakeredis/geo_commands.rb', line 15

def geoadd(key, *members)
  raise_argument_error("geoadd") if members.empty? || members.size % 3 != 0

  set = (data[key] ||= GeoSet.new)
  prev_size = set.size
  members.each_slice(3) do |member|
    set.add(*member)
  end
  set.size - prev_size
end

#geodist(key, member1, member2, unit = "m") ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fakeredis/geo_commands.rb', line 26

def geodist(key, member1, member2, unit = "m")
  unit = unit.to_s
  raise_command_error("ERR unsupported unit provided. please use #{DISTANCE_UNITS.keys.join(', ')}") unless DISTANCE_UNITS.include?(unit)

  set = (data[key] || GeoSet.new)
  point1 = set.get(member1)
  point2 = set.get(member2)
  if point1 && point2
    distance = point1.distance_to(point2)
    distance_in_units = distance / DISTANCE_UNITS[unit]
    distance_in_units.round(REDIS_DOUBLE_PRECISION).to_s
  end
end

#geohash(key, member) ⇒ Object


40
41
42
43
44
45
46
47
48
# File 'lib/fakeredis/geo_commands.rb', line 40

def geohash(key, member)
  members = Array(member)
  raise_argument_error("geohash") if members.empty?
  set = (data[key] || GeoSet.new)
  members.map do |member|
    point = set.get(member)
    point.geohash(REDIS_GEOHASH_SIZE) if point
  end
end

#geopos(key, member) ⇒ Object


50
51
52
53
54
55
56
57
58
59
# File 'lib/fakeredis/geo_commands.rb', line 50

def geopos(key, member)
  return nil unless data[key]

  members = Array(member)
  set = (data[key] || GeoSet.new)
  members.map do |member|
    point = set.get(member)
    [point.lon.to_s, point.lat.to_s] if point
  end
end

#georadius(*args) ⇒ Object


61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/fakeredis/geo_commands.rb', line 61

def georadius(*args)
  args = args.dup
  raise_argument_error("georadius") if args.size < 5
  key, lon, lat, radius, unit, *rest = args
  raise_argument_error("georadius") unless DISTANCE_UNITS.has_key?(unit)
  radius *= DISTANCE_UNITS[unit]

  set = (data[key] || GeoSet.new)
  center = GeoSet::Point.new(lon, lat, nil)

  do_georadius(set, center, radius, unit, rest)
end

#georadiusbymember(*args) ⇒ Object


74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fakeredis/geo_commands.rb', line 74

def georadiusbymember(*args)
  args = args.dup
  raise_argument_error("georadiusbymember") if args.size < 4
  key, member, radius, unit, *rest = args
  raise_argument_error("georadiusbymember") unless DISTANCE_UNITS.has_key?(unit)
  radius *= DISTANCE_UNITS[unit]

  set = (data[key] || GeoSet.new)
  center = set.get(member)
  raise_command_error("ERR could not decode requested zset member") unless center

  do_georadius(set, center, radius, unit, args)
end