Skip to content

Instantly share code, notes, and snippets.

@siruguri
Last active March 30, 2021 02:52
Show Gist options
  • Save siruguri/8fa338907305c0c9dd5e8f7845ba5dc0 to your computer and use it in GitHub Desktop.
Save siruguri/8fa338907305c0c9dd5e8f7845ba5dc0 to your computer and use it in GitHub Desktop.
Calendaring API in Ruby
require 'date'
class Calendar
def initialize
end
# start_t and end_t are Ruby DateTime objects in this and other methods
# event creation shouldn't cause conflicts
# if is_recurring is true, then the event recurs once a day at the given start and end times. In that case, the start and
# end times are only used to extract the hour and minute information and not the day information
# return an event ID
def add_event(start_t, end_t, is_recurring: false)
end
# Returns Boolean - true if a meeting for this specific time period will not conflict with an existing event
# day is a Ruby Date object
def is_available?(start_t, end_t)
end
# event_id is the one returned by add_event
# update_parameter should be required to be one of: :start_time, :end_time, or :day
# Assume update_value is the right type - DateTime for the first 2 possible value of update_parameter; and Date for the last
# The date portion of the provided DateTime for update value will be ignored; only the hour and minute portion should be used.
# The method should only make changes to non-recurring events.
def update_event(event_id, update_parameter:, update_value:)
end
end
# Example tests.
# These are not the only test cases we will use, but these should give you a flavor of what the test cases look like.
cal = Calendar.new
id1 = cal.add_event(DateTime.new(2020, 1, 2, 2, 0), DateTime.new(2020, 1, 2, 3, 0))
recurring = cal.add_event(DateTime.new(2020, 1, 3, 5, 0), DateTime.new(2020, 1, 3, 6, 0), is_recurring: true)
cal.is_available?(DateTime.new(2020, 1, 2, 2, 0), DateTime.new(2020, 1, 2, 3, 0)) # => should return false
cal.is_available?(DateTime.new(2020, 1, 3, 2, 0), DateTime.new(2020, 1, 3, 3, 0)) # => should return true
cal.is_available?(DateTime.new(2020, 1, 13, 5, 0), DateTime.new(2020, 1, 13, 6, 0)) # => should return false
cal.is_available?(DateTime.new(2020, 1, 3, 0, 30), DateTime.new(2020, 1, 3, 1, 30)) # => should return true
cal.update_event(id1, update_parameter: :start_time, update_value: DateTime.new(2020, 1, 1, 1, 0))
cal.is_available?(DateTime.new(2020, 1, 3, 0, 30), DateTime.new(2020, 1, 3, 1, 30)) # => should return false
# Errors
# These are not all the tests; some error handling tests will reflect the specifications above.
# Raises exception because start time is past end time
cal.update_event(id1, update_parameter: :start_time, update_value: DateTime.new(2020, 1, 1, 4, 0))
# Raises exception because start time is past end time
id1 = cal.add_event(DateTime.new(2020, 1, 2, 4, 0), DateTime.new(2020, 1, 2, 3, 0))
# Raises exception because start time is in the past.
id1 = cal.add_event(DateTime.new(2019, 1, 2, 2, 0), DateTime.new(2019, 1, 2, 3, 0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment