Skip to content

Instantly share code, notes, and snippets.

@m0wfo
Created December 27, 2008 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m0wfo/40274 to your computer and use it in GitHub Desktop.
Save m0wfo/40274 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'webrick'
require 'webrick/httpservlet/webdavhandler'
require 'icalendar'
require 'googlecalendar'
class WEBrick::HTTPServlet::WebDAVHandler
def do_PUT(req,resp)
c = File.new(req.path.delete('/$'), 'w+') # Open / create the file iCal has given us {File::RDWR|File::CREAT}
previous_file = ''
eachline = c.each {|l| previous_file << l} # Read file contents into 'previous_file'
puts req.body
end
end
class WEBrick::HTTPServlet::WebDAVHandler
def do_PUT(req,resp)
if req.raw_header.to_s =~ /text\/calendar/ # If iCal gives us something...
c = File.new(req.path.delete('/$'), 'w+') # Open / create the file iCal has given us {File::RDWR|File::CREAT}
previous_file = ''
eachline = c.each {|l| previous_file << l} # Read file contents into 'previous_file'
if previous_file != req.body # If the new PUT body differs from the last saved version...
puts 'Calendar is different.'
c.puts req.body
c.close
puts req.body
else # Otherwise ignore
puts 'Calendar is unchanged.'
end
end
end
end
# Crank up the server
server = WEBrick::HTTPServer.new({:Port => 6453})
server.mount("/", WEBrick::HTTPServlet::WebDAVHandler, Dir.pwd)
trap(:INT){ server.shutdown } # Trap kill commands so we can shut down cleanly
server.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment