ice_cube
ice_cube copied to clipboard
Raise error if from_ical called with invalid string?
IceCube::Schedule.from_ical(ical_string)
will return an IceCube::Schedule
no matter what ical_string
is. I'd like to raise an error if the string is not a valid ICAL schedule (or at least provide that as an option). Before I implement, how do others feel about this?
For what it's worth, I'm using the icalendar gem to do this in an application where we're using ice_cube
. Something like:
class Whatever < ActiveRecord::Base
validate :_schedule_ical_has_valid_syntax, if: :schedule_ical
def _schedule_ical_has_valid_syntax
icalendar = _parse_icalendar
if icalendar.empty? || icalendar.first.events.empty? || !icalendar.first.events.first.valid?
errors.add :schedule_ical, "is not valid ICAL syntax"
end
end
def _schedule_ical_with_wrapper
[
"BEGIN:VCALENDAR",
"VERSION:2.0",
"PRODID:validationid",
"CALSCALE:GREGORIAN",
"BEGIN:VEVENT",
schedule_ical,
"END:VEVENT",
"END:VCALENDAR"
].join("\n")
end
def _parse_icalendar
Icalendar.parse(_schedule_ical_with_wrapper)
end
end
Icalendar seems to be the right tool for the job. I think it makes sense if we start using it for import/export. It has no extra runtime dependencies (same as IceCube) so I'd be :+1: depending on it for this.
I came here to raise an issue for the same thing, and was about to suggest making icalendar a dependancy and using that for parsing and generating ical. If I'm feeling motivated, would you accept a pull request for this?
Just wanted to note that this is no longer the behavior. Calling from_ical
now raises an exception if the format is invalid:
IceCube::Rule.from_ical("FREQ=MONTHLY;BYDAY=1FR") => #<IceCube::MonthlyRule:0x00007
IceCube::Rule.from_ical("foobar") => ArgumentError: Invalid iCal rule component
I think this issue can now be closed.