require 'net/dav' require 'vortex_client/string_utils' require 'highline/import' require 'time' # Utilities for managing content in the web content management system Vortex. # All calls are done with the webdav protocol. module Vortex class Connection < Net::DAV # Create a new connection to Vortex. Prompts for username and password if not # supplied. Overrides Net::DAV.initialize() # # Examples: # # vortex = Vortex::Connection.new("https://www-dav.server.com",user,pass) # # vortex = Vortex::Connection.new("https://www-dav.server.com") => # Username: tiger # Password: ***** def initialize(uri, *args) @uri = uri @uri = URI.parse(@uri) if @uri.is_a? String @have_curl = false # This defaults to true in Net::DAV @handler = NetHttpHandler.new(@uri) @handler.verify_server = false # This defaults to true in Net::DAV if(args != []) @handler.user = args[0] @handler.pass = args[1] else @handler.user = ask("Username: ") {|q| q.echo = true} @handler.pass = ask("Password: ") {|q| q.echo = "*"} # false => no echo end return @handler end # Returns true if resource or collection exists. # # Example: # # vortex.exists?("https://www-dav.server.com/folder/index.html") def exists?(uri) @uri = uri @uri = URI.parse(@uri) if @uri.is_a? String begin self.propfind(@uri.path) rescue Net::HTTPServerException => e return false if(e.to_s =~ /404/) end return true end # Publish a document object to the web. # # Publishes a object by performing a PUT request to object.url with object.content # and then performing a PROPPATCH request to object.url with object.properties # # Example: # # vortex = Vortex::Connection.new("https://www-dav.server.com") # article = Vortex::StructuredArticle(:title=>"My title") # vortex.publish(article) def publish(object) if(object.is_a? HtmlArticle) uri = @uri.merge(object.url) # puts "URL: '" + uri.to_s self.put_string(uri, object.content) self.proppatch(uri, object.properties) # puts object.properties return uri.to_s else warn "Unknown vortex resource: " + object.class.to_s end end private # Disable Net::DAV.credentials def credentials(user, pass) end end class Resource end class PlainFile < Resource # File in webdav. Named PlainFile so it won't get mixed up with standard File class. end # Plain HTML files with title, introduction and keywords set as webdav properties. # # Examples: # # article = HtmlArticle.new(:title => "Sample Title", # :introduction => "Introduction", # :body => "

Hello world

") # vortex.publish(article) class HtmlArticle < PlainFile attr_accessor :title, :introduction, :body, :filename, :modifiedDate, :publishedDate, :owner, :url, :author, :date # Create a new article of type html-article: plain html file with introduction stored as a webdav property. def initialize(options={}) options.each{|k,v|send("#{k}=",v)} end def url if(@url) @url else if(title) StringUtils.create_filename(title) + ".html" else warn "Article must have either a full url or title. " end end end def properties props = 'article' + 'article' + 'utf-8' if(@publishedDate and @publishedDate != "") if(@publishedDate.kind_of? Time) @publishedDate = @publishedDate.httpdate.to_s end props += '' + @publishedDate + '' end if(date and date != "") if(date.kind_of? Time) date = @date.httpdate.to_s end if(@publishedDate == nil or @publishedDate != "") props += '' + date + '' end props += '' + date + '' + '' + date + '' + '' + date + '' + '' + date + '' end if(title) props += '' + title + '' end if(owner) props += '' + owner + '' end if(introduction and introduction != "") props += '' + introduction + '' end if(author and author != "") props += '' + '' + '' + author + '' + '' + '' end return props end def content content = '' + '' + title + '' + ' ' + '' if(body) content += body end content += '' end end # Vortex article stored as JSON data. class StructuredArticle < PlainFile attr_accessor :title, :introduction, :content, :filename, :modifiedDate, :publishedDate, :owner, :url # Create an article # Options: # # :title => "Title" mandatory def initialize(options={}) options.each{|k,v|send("#{k}=",v)} end def dav_properties "" end def dav_content "" end end # Collection (folder) class Collection < Resource end class ArticleListingCollection < Collection attr_accessor :title, :url, :folderename, :introduction, :navigationTitle, :sortByDate, :sortByTitle, :owner def initialize(options={}) options.each{|k,v|send("#{k}=",v)} end def properties() "article-listing" + "article-listing" + "#{userTitle}" + "#{navigationTitle}" + "#{owner}" end end end