Sha256: b1b6158b760e1dc005c667bb16f11a90a10fa4a8d5649f29140f1e64c5865476

Contents?: true

Size: 1.15 KB

Versions: 3

Compression:

Stored size: 1.15 KB

Contents

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

module Almanack
  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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
almanack-0.0.1.alpha3 lib/almanack/ical_feed.rb
almanack-0.0.1.alpha2 lib/almanack/ical_feed.rb
almanack-0.0.1.alpha1 lib/almanack/ical_feed.rb