Created
December 27, 2008 16:07
-
-
Save m0wfo/40274 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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