Sha256: 8a6d9f0ec757ababf42571d1ced5054e7d1d182706612f489f23f6fcfad3d4a5

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

require 'date'

module Markdo
  class IcsExporter
    def initialize(task_collection)
      @task_collection = task_collection
    end

    def to_ics
      buf = []

      buf << 'BEGIN:VCALENDAR'
      buf << 'VERSION:2.0'
      buf << 'CALSCALE:GREGORIAN'
      buf << 'METHOD:PUBLISH'
      buf << 'X-WR-CALNAME:Markdo Due Dates'
      buf << events.map { |event| event.to_ics }
      buf << 'END:VCALENDAR'

      buf.
        flatten.
        map { |line| "#{line}\n" }.
        join
    end

    private

    def events
      @task_collection.
        with_attribute('due').
        reject { |task| task.complete? }.
        map { |task|
          due_date = task.attributes['due'].date_value
          summary = clean(task.body)

          if due_date
            Event.new(due_date, due_date, summary)
          end
        }.
        compact
    end

    def clean(line)
      line.sub(%r(@due\(.*\)\s), '')
    end

    class Event
      def initialize(date_start, date_end, summary)
        @date_start = date_start
        @date_end = date_end
        @summary = summary
      end

      def to_ics
        buf = []
        buf << 'BEGIN:VEVENT'
        buf << "DTSTART;VALUE=DATE:#{@date_start.strftime('%Y%m%d')}"
        buf << "DTEND;VALUE=DATE:#{@date_end.strftime('%Y%m%d')}"
        buf << "SUMMARY:#{@summary}"
        buf << 'END:VEVENT'
        buf.join("\n")
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
markdo-0.2.0 lib/markdo/ics_exporter.rb