Sha256: a8a4963709c6b07d3da3b0d87c991bfe3d8844985040ae0ca95488721656a404

Contents?: true

Size: 1.33 KB

Versions: 9

Compression:

Stored size: 1.33 KB

Contents

module Almanack
  module Representation
    class IcalFeed
      attr_reader :calendar

      def initialize(calendar)
        @calendar = calendar
      end

      def ical
        @ical ||= ical_calendar
      end

      def to_s
        ical.to_s
      end

      def self.from(calendar)
        self.new(calendar)
      end

      private

      def events
        calendar.events_between(now..lookahead)
      end

      def ical_calendar
        events.each_with_object(RiCal.Calendar) do |event, calendar_component|
          calendar_component.add_subcomponent ical_event_for(event)
        end
      end

      def ical_event_for(event)
        ical_event = RiCal.Event
        ical_event.summary = event.title
        ical_event.dtstart = event.start_time.utc
        ical_event.dtend = (event.end_time || event.start_time + default_event_duration ).utc
        ical_event.description = event.description if event.description
        ical_event.location = event.location if event.location
        ical_event
      end

      def lookahead
        now + calendar.feed_lookahead * ONE_DAY
      end

      def default_event_duration
        # Three hours is the duration for events missing end dates, a
        # recommendation suggested by Meetup.com.
        3 * ONE_HOUR
      end

      def now
        @now ||= Time.now
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
almanack-1.1.2 lib/almanack/representation/ical_feed.rb
almanack-1.1.1 lib/almanack/representation/ical_feed.rb
almanack-1.1.0 lib/almanack/representation/ical_feed.rb
almanack-1.1.0.beta6 lib/almanack/representation/ical_feed.rb
almanack-1.1.0.beta5 lib/almanack/representation/ical_feed.rb
almanack-1.1.0.beta4 lib/almanack/representation/ical_feed.rb
almanack-1.1.0.beta3 lib/almanack/representation/ical_feed.rb
almanack-1.1.0.beta2 lib/almanack/representation/ical_feed.rb
almanack-1.1.0.beta1 lib/almanack/representation/ical_feed.rb