To request a truncation, POST to /
{"format":"json","url":"http://www.trunc.us"}
Truncation response:
{"trunct":{"token":"3bU65R","url":"http://www.trunc.us"}}
To expand a token, GET /
GET /3bU65R.json
Expand response:
{"trunct":{"token":"3bU65R","url":"http://www.trunc.us"}}
require 'rubygems'
require 'json'
require 'uri'
require 'net/http'
class TrunctClient
attr_accessor :host, :port
def initialize(options={})
self.host = options[:host]
self.port = options[:port] || 80
end
def trunc(url)
req = Net::HTTP::Post.new("/", initheader = {'Content-Type' =>'application/json'})
req.body = {"url" => url, "format" => "json"}.to_json
response = Net::HTTP.new(host, port).start {|http| http.request(req) }
data = JSON::parse(response.body)
data["trunct"]["token"]
end
def expand(token)
url = URI.parse("http://#{host}/#{token}.json")
req = Net::HTTP::Get.new(url.path)
res = Net::HTTP.start(host, port) {|http|
http.request(req)
}
data = JSON::parse(res.body)
data["trunct"]["url"]
end
end