ice_cube icon indicating copy to clipboard operation
ice_cube copied to clipboard

Raise error if from_ical called with invalid string?

Open barelyknown opened this issue 9 years ago • 4 comments

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?

barelyknown avatar Jun 08 '15 17:06 barelyknown

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

barelyknown avatar Jun 08 '15 19:06 barelyknown

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.

avit avatar Jan 31 '16 05:01 avit

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?

Wardrop avatar Oct 12 '16 23:10 Wardrop

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.

JF-Lalonde avatar Jan 11 '21 22:01 JF-Lalonde