Sha256: 6ba83739437fc9f847702ca16edfb591ec4ecd751867f92140b2f7b3981e6006

Contents?: true

Size: 1.34 KB

Versions: 2

Compression:

Stored size: 1.34 KB

Contents

module Almanack
  module EventSource
    class IcalFeed
      def initialize(url, options = {})
        @url = url
        @options = options
      end

      def events_between(date_range)
        occurrences_between(date_range).map do |occurrence|
          event_from(occurrence)
        end
      end

      private

      def each_ical_event(&block)
        entities.each do |entity|
          entity.events.each(&block) if entity.respond_to?(:events)
        end
      end

      def occurrences_between(date_range)
        to_date = date_range.max
        from_date = date_range.min

        occurrences = []

        each_ical_event do |ical_event|
          ical_event.occurrences(starting: from_date, before: to_date).each do |occurrence|
            occurrences << occurrence
          end
        end

        occurrences
      end

      def event_from(occurrence)
        Event.new(
          title: occurrence.summary,
          start_date: occurrence.dtstart,
          end_date: occurrence.dtend,
          description: occurrence.description,
          location: occurrence.location
        )
      end

      def entities
        RiCal.parse_string(response.body)
      end

      def connection
        @options[:connection]
      end

      def response
        uri = Addressable::URI.parse(@url)
        connection.get(uri)
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
almanack-1.0.5 lib/almanack/event_source/ical_feed.rb
almanack-1.0.4 lib/almanack/event_source/ical_feed.rb