Skip to content

Instantly share code, notes, and snippets.

@m0wfo m0wfo/gcalsync.rb
Created Dec 27, 2008

Embed
What would you like to do?
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
You can’t perform that action at this time.