Skip to content

Instantly share code, notes, and snippets.

@candland
Created May 17, 2011 16:15
Show Gist options
  • Save candland/ae17ab0ff656db0f800c to your computer and use it in GitHub Desktop.
Save candland/ae17ab0ff656db0f800c to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'net/http'
require 'ri_cal'
require 'iconv'
require 'active_support'
class ImportEvents
def self.import
body = nil
Net::HTTP.start('www.supersaas.com') do |http|
response = http.get(SuperSaas::WEB_CAL)
body = response.body
end
ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')
body = ic.iconv(body + ' ')[0..-2]
components = RiCal.parse_string(body)
uids = []
components.each do |cal|
puts cal.events.count
cal.events.each do |e|
# Rails.logger.debug e.inspect
puts "UID: #{e.inspect}"
uids << e.uid
event = Event.find_or_create_by_uid(e.uid)
is_new = event.new_record?
#puts "Start: #{e.dtstart} "
puts "End: #{e.dtend} "
event.attributes = {
:name => e.description.blank? ? "Booked" : e.description,
:start_at => e.dtstart,
:end_at => e.dtend,
:url => Rails.application.routes.url_helpers.calendar_share_path(e.uid.delete('@supersaas.com'))
}
event.save!
Rails.logger.debug "#{is_new ? 'Added' : 'Updated'} #{event.name} on #{event.start_at} | #{e.dtstart}"
end
end
Event.remove_not_id(uids)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment