/usr/share/doc/libkyototycoon2/example/ktrestex.rb is in kyototycoon-doc 0.9.56-1build2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 38 39 40 41 42 43 44 45 46 47 48 49 50 | require 'uri'
require 'net/http'
# RESTful interface of Kyoto Tycoon
class KyotoTycoon
# connect to the server
def open(host = "127.0.0.1", port = 1978, timeout = 30)
@ua = Net::HTTP::new(host, port)
@ua.read_timeout = timeout
@ua.start
end
# close the connection
def close
@ua.finish
end
# store a record
def set(key, value, xt = nil)
key = "/" + URI::encode(key)
req = Net::HTTP::Put::new(key)
if xt
xt = Time::now.to_i + xt
req.add_field("X-Kt-Xt", xt)
end
res = @ua.request(req, value)
res.code.to_i == 201
end
# remove a record
def remove(key)
key = "/" + URI::encode(key)
req = Net::HTTP::Delete::new(key)
res = @ua.request(req)
res.code.to_i == 204
end
# retrieve the value of a record
def get(key)
key = "/" + URI::encode(key)
req = Net::HTTP::Get::new(key)
res = @ua.request(req)
return nil if res.code.to_i != 200
res.body
end
end
# sample usage
kt = KyotoTycoon::new
kt.open("localhost", 1978)
kt.set("japan", "tokyo", 60)
printf("%s\n", kt.get("japan"))
kt.remove("japan")
kt.close
|