class Almanack::EventSource::Ical

Attributes

io[R]

Public Class Methods

from(*args) click to toggle source
# File lib/almanack/event_source/ical.rb, line 32
def self.from(*args)
  self.new(*args)
end
new(io) click to toggle source
# File lib/almanack/event_source/ical.rb, line 6
def initialize(io)
  @io = io
end

Public Instance Methods

events_between(date_range) { |event_from(ical_event)| ... } click to toggle source
# File lib/almanack/event_source/ical.rb, line 10
def events_between(date_range)
  return enum_for(__method__, date_range).to_a unless block_given?

  from, to = [date_range.min, date_range.max]

  each_ical_event do |ical_event|
    if ical_event.rrule.empty?
      if from < ical_event.dtend
        yield event_from(ical_event)
      end
    else
      ical_event.occurrences_between(from, to).each do |occurrence|
        yield event_from(ical_event, occurrence: occurrence)
      end
    end
  end
end
serialized_between(date_range) click to toggle source
# File lib/almanack/event_source/ical.rb, line 28
def serialized_between(date_range)
  { events: events_between(date_range).map(&:serialized) }
end

Private Instance Methods

each_ical_event(&block) click to toggle source
# File lib/almanack/event_source/ical.rb, line 52
def each_ical_event(&block)
  Icalendar::Calendar.parse(read_io).each do |calendar|
    calendar.events.each(&block)
  end
end
event_from(ical_event, occurrence: nil) click to toggle source
# File lib/almanack/event_source/ical.rb, line 38
def event_from(ical_event, occurrence: nil)
  Event.new(
    title: ical_event.summary,
    start_time: occurrence&.start_time || ical_event.dtstart,
    end_time: occurrence&.end_time || ical_event.dtend,
    description: ical_event.description,
    location: ical_event.location
  )
end
read_io() click to toggle source
# File lib/almanack/event_source/ical.rb, line 48
def read_io
  io.respond_to?(:read) ? io.read : io
end