# -*- encoding : utf-8 -*- =begin Copyright (C) 2008 Sam Roberts This library is free software; you can redistribute it and/or modify it under the same terms as the ruby language itself, see the file COPYING for details. =end require "cgi" require "uri" require "vpim/repo" require "vpim/agent/atomize" module Vpim module Agent # On failure, raise this with an error message. text/plain for now, # text/html later. Will convert to a 404 and a message. class NotFound < Exception def initialize(name, path) super %{Resource "#{name}" under "#{path.prefix}" was not found!} end end class Path def self.split_path(path) begin path = path.to_ary rescue NameError path = path.split("/") end path.map{|w| CGI.unescape(w)} end # URI is the uri being queried, base is where this path is mounted under? def initialize(uri, base = "") @uri = URI.parse(uri.to_s) #pp [uri, base, @uri] if @uri.path.size == 0 @uri.path = "/" end @path = Path.split_path(@uri.path) @base = base.to_str @mark = 0 @base.split.size.times{ shift } end def uri @uri.to_s end def to_path self end # TODO - call this #next def shift if @path[@mark] @path[@mark += 1] end end def append(name, scheme = nil) uri = @uri.dup uri.path += "/" + CGI.escape(name) if scheme uri.scheme = scheme end uri end def prefix(len = nil) len ||= @mark @path[0, len].map{|p| CGI.escape(p)}.join("/") + "/" end end module Form ATOM = Atomize::MIME HTML = "text/html" ICS = "text/calendar" PLAIN = "text/plain" VCF = "text/directory" end # Return an HTML description of a list of resources accessible under this # path. class ResourceList def initialize(description, items) @description = description @items = items end def get(path) return <<__, Form::HTML
#{@description}