#!/usr/bin/env ruby
#
# get annotations
#
# this lives at http://www.w3.org/2001/sw/Europe/200209/annodemo/earlget.rb
# 	$Id: earlget.rb,v 1.1 2002/09/29 22:14:51 charles Exp $
#
# usage: earlget.rb <annotated document> [<annotation server> [<username> [<password>]]]
#
# todo: make it accept options for username/password insteaed of strict order
#	make it useful by having something to feed the output to
#	turn all these pieces into a class
#
# License: This software is covered by the W3C Software License.
#	See http://www.w3.org/Consortium/Legal/copyright-software for details 


# net/http is to do the network work
# cgi is for URL-encoding the URI we want to query on
require 'net/http'
require 'cgi'

# read the command line

target = ARGV[0]
server = (ARGV[1] || 'http://earl.w3.org/earl')

username = (ARGV[2] || '')
password = (ARGV[3] || '')

# interpret the command line stuff

server =~ /:\/\/([^\/]+)(\/.*)$/
host = $1
res = $2
uri = { 'server' => host , 'path' => res }
authstring = 'Basic ' + ["#{username}:#{password}"].pack('m').strip
enc_target = CGI.escape(target)
path = uri['path'] + "?w3c_algaeQuery=(ask%0D%0A+%27((http://earl.w3.org/temporary%23regarding+%3Fannotation+#{enc_target})%0D%0A+)+:collect+%27(%3Fannotation))&w3c_submit=query+RDF+DB"

# some things that are fixed data

my_headers = {'Accept' => 'application/xml' ,
	'Authorization' => authstring ,
        'User-agent' => 'chaalsToy / 0.01a' } 
#'application/rdf+xml'  is what it should be

# Do the work: make connection, get the data, and print it out

annoserver = Net::HTTP.new(uri['server'], 80)

resp, data = annoserver.get(path, my_headers)
puts data
