-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiny_redis.rb
37 lines (31 loc) · 920 Bytes
/
tiny_redis.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Copyright 2017 Maksym Melnychok
# MIT License - https://opensource.org/licenses/MIT
#
# inspired by https://github.com/ptrofimov/tinyredisclient
require 'socket'
class TinyRedis
RN = "\r\n"
def initialize(host='localhost', port=6379)
@socket = TCPSocket.new(host, port)
end
def method_missing(method, *args)
args.unshift method
data = ["*#{args.size}", *args.map {|arg| "$#{arg.to_s.size}#{RN}#{arg}"}]
@socket.write(data.join(RN) << RN)
parse_response
end
def parse_response
case line = @socket.gets
when /^\+(.*)\r\n$/ then $1
when /^:(\d+)\r\n$/ then $1.to_i
when /^-(.*)\r\n$/ then raise "Redis error: #{$1}"
when /^\$([-\d]+)\r\n$/
$1.to_i >= 0 ? @socket.read($1.to_i+2)[0..-3] : nil
when /^\*([-\d]+)\r\n$/
$1.to_i > 0 ? (1..$1.to_i).inject([]) { |a,_| a << parse_response } : nil
end
end
def close
@socket.close
end
end