Skip to content

Instantly share code, notes, and snippets.

@cyrusstoller
Last active October 6, 2015 01:14
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 cyrusstoller/209981a597f22b221f12 to your computer and use it in GitHub Desktop.
Save cyrusstoller/209981a597f22b221f12 to your computer and use it in GitHub Desktop.
*.ics
.DS_Store
require 'date'
require 'securerandom'
def prompt(msg)
puts "\n#{msg}"
gets.chomp()
end
def pbcopy(input)
str = input.to_s
IO.popen('pbcopy', 'w') { |f| f << str }
str
end
def get_data
what = prompt("What:")
where = prompt("Where:")
month, day, year = prompt("Start Date (mm/dd/yyyy):").split(/[\/-]/).map(&:to_i)
start = prompt("Start Time (HH[:]MM):").gsub(":", "").to_i
hour = start / 100
min = start % 100
timezone = "-04:00"
num_hours = prompt("Num hours:").to_f
start_time = DateTime.new(year, month, day, hour, min, 0, timezone)
end_time = start_time.dup + num_hours/24.0
return what, where, start_time, end_time
end
def linebreak
puts
puts "--------------------------------"
puts
end
def ics_escape(msg)
msg.gsub(",", "\\,")
end
def ics_timestamp(time)
time.new_offset(0).strftime("%Y%m%dT%H%M%SZ")
end
def create_ics(what, where, start_time, end_time)
message = <<EOF
BEGIN:VCALENDAR
PRODID:-//Cyrus Stoller 0.0//EN
VERSION:2.0
METHOD:PUBLISH
CALSCALE:GREGORIAN
BEGIN:VEVENT
DTSTAMP:#{ics_timestamp DateTime.now}
DTSTART:#{ics_timestamp start_time}
DTEND:#{ics_timestamp end_time}
SUMMARY:#{ics_escape what}
UID:harvardpulse.com//#{SecureRandom.hex}
DESCRIPTION:
LOCATION:#{ics_escape where}
END:VEVENT
END:VCALENDAR
EOF
File.open("output.ics", "w") { |file| file << message }
end
def make_markdown(what, where, start_time, end_time)
message = <<EOF
**What:** #{what}
**Where:** #{where}
**When:** #{start_time.strftime("%A %B %-d, %Y")} from #{start_time.strftime("%l:%M%P")} - #{end_time.strftime("%l:%M%P")}
EOF
puts pbcopy(message)
end
def main
details = get_data()
linebreak
make_markdown(*details)
create_ics(*details)
end
main if __FILE__ == $0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment