#!/usr/bin/ruby

require 'json'
require 'net/http'

$url = 'http://localhost:8000/jsonrpc.yaws'

def json_call(function, params)
  uri = URI.parse $url
  req = Net::HTTP::Post.new(uri.path)
  req.add_field 'Content-Type', 'application/json'
  req.body =
    {
    :version => '1.1',
    :method => function,
    :params => params,
    :id => 0}.to_json
  res = Net::HTTP.start(uri.host, uri.port){|http|http.request(req)}
  JSON.parse(res.body)['result']
end

def read(key)
  json_call('read', [key])
end

def write(key, value)
  json_call('write', [key, value])
end

result = write(ARGV[0],ARGV[1])
puts result

