Skip to content

Instantly share code, notes, and snippets.

@fieldse
Created March 14, 2018 13:40
Show Gist options
  • Save fieldse/86fa47e664a5bf5a51f1dda39e4c367a to your computer and use it in GitHub Desktop.
Save fieldse/86fa47e664a5bf5a51f1dda39e4c367a to your computer and use it in GitHub Desktop.
# calendar_ical.rb
# Todo - date string format
def get_date_format(time_obj)
# Convert standard ruby datetime obj to ISO 8601 format
# time_obj - should be datetime object
# Time format:
# timestring.to_time.iso8601 # Incorrect format! We need 19970610T172345Z
# 1997 06 10 T 17 23 45 Z
# year mo da T hr mi se Z
fmt = '%Y%m%dT%H%M%SZ'
return time_obj.strftime(fmt)
end
# Create iCal format for events
def calendar_format_ical(start_date, end_date, title, description)
# def calendar_format_ical(start_date, end_date, title, description, client_id: "bobs_english", username: 'Ruzuku', email: 'calendar@ruzuku.com')
# Returns:
# event.ics (plaintext file)
# Start date and end date should be in ISO 8601 format
# ie.: 19970714T170000Z
# Parameters:
# username = 'Ruzuku'
# client_id = "bobs_english"
# email = 'calendar@ruzuku.com'
# uid = uid1@ruzuku.com
# Questions:
# - what's PRODID?
# -> specifies the identifier for the product that created the iCalendar object. (?)
# ? See https://www.kanzaki.com/docs/ical/prodid.html
# - what's UID? User id? -> Unique identifier: MUST be unique.
# - what should be the email/organizer? (defaulting to calendar@ruzuku.com)
# Todo:
# [X] Get timestamp
# [ ] Get client id?
# [ ] Get email address?
# [X] Figure out prod_id (?)
# [ ] uid - combine hash of timestamp and organization name
# [X] write to file
# [ ] escape string sequences / html
# Placeholder params
prod_id = '-//Ruzuku.com/calendar_event//NONSGML v1.0//EN'
client_id = "bobs_english"
location = "ruzuku.com"
username = 'Ruzuku'
email = 'calendar@ruzuku.com'
# Combine uid
timestamp = get_date_format(Time.now) # Timestamp now (string)
uid = timestamp + client_id + '@ruzuku.com' # Combined UID - [timestamp][client_id]@ruzuku.com
template = """
BEGIN:VCALENDAR
VERSION:2.0
PRODID:#{prod_id}
BEGIN:VEVENT
UID:#{uid}
LOCATION:#{location}
DTSTAMP:#{timestamp}
SEQUENCE:0
ORGANIZER:CN=#{username}:MAILTO:#{email}
DTSTART:#{start_date}
DTEND:#{end_date}
SUMMARY:#{description}
END:VEVENT
END:VCALENDAR
"""
# Print
puts template
# Write to file
fd = 'event.ics'
open(fd, 'w') do |f|
f.puts template
puts 'Event written to file: #{fd}'
end
return fd
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment