Skip to content

Instantly share code, notes, and snippets.

@benjamintanweihao
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save benjamintanweihao/9cc843b5e32963008007 to your computer and use it in GitHub Desktop.

Select an option

Save benjamintanweihao/9cc843b5e32963008007 to your computer and use it in GitHub Desktop.
DiCE Ruby Client.
#!/usr/bin/env ruby
require 'json'
require 'socket'
class DiceClient
def initialize(host, port)
@host = host
@port = port
puts "Connecting to server..."
end
def put(key, value)
op({"op" => "put", "key" => key, "value" => value})
end
def get(key)
op({"op" => "get", "key" => key})
end
def remove(key)
op({"op" => "remove", "key" => key})
end
def op(data)
@socket = TCPSocket.new(@host, @port)
@socket.write(JSON.generate(data))
@socket.read.to_json
end
end
client = DiceClient.new('localhost', 1155)
puts "=== PUT"
puts client.put("elixir", "awesome sauce")
puts client.put("php", "lolwut")
puts "=== GET"
puts client.get("elixir")
puts client.get("php")
puts "=== REMOVE"
puts client.remove("elixir")
puts "=== PUT"
puts client.put("elixir", "really awesome sauce")
puts "=== GET"
puts client.get("elixir")
puts client.get("php")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment