Sha256: c5068839f43305df2a68dc820086d75ee1522919b859212a444a1eb5e52721ef

Contents?: true

Size: 1.26 KB

Versions: 6

Compression:

Stored size: 1.26 KB

Contents

require 'net/http'
require 'uri'
require 'ri_cal'

module Almanack
  module EventSource
    class IcalFeed
      def initialize(url)
        @url = url
      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(body)
      end

      def body
        uri = URI(@url)
        Net::HTTP.get(uri)
      end

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
almanack-1.0.3 lib/almanack/event_source/ical_feed.rb
almanack-1.0.2 lib/almanack/event_source/ical_feed.rb
almanack-1.0.1 lib/almanack/event_source/ical_feed.rb
almanack-1.0.0 lib/almanack/event_source/ical_feed.rb
almanack-1.0.0.pre1 lib/almanack/event_source/ical_feed.rb
almanack-1.0.0.pre lib/almanack/event_source/ical_feed.rb