Sha256: 8c74251e61979b61f7b45b97c14b1063318150ad250d7f160d22b5a66b0d38bf

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

require 'date'

module ICS

  class Event

    attr_accessor :action
    attr_accessor :alarmuid
    attr_accessor :attach
    attr_accessor :created
    attr_accessor :description
    attr_accessor :dtend
    attr_accessor :dtstamp
    attr_accessor :dtstart
    attr_accessor :location
    attr_accessor :sequence
    attr_accessor :status
    attr_accessor :summary
    attr_accessor :transp
    attr_accessor :trigger
    attr_accessor :uid
    attr_accessor :url
    attr_accessor :x_wr_alarmuid

    def initialize(attributes = {})
      attributes.each do |key, val|
        send("#{key}=", val)
      end
    end

    class << self

      # Given an exported ical file, parse it and create events.
      def file(file)
        # format line endings.
        content = file.readlines.map(&:chomp).join($/)
        line_ending = $/
        content.split("BEGIN:VEVENT#{line_ending}")[1..-1].map do |data_string|
          data_string = data_string.split("END:VEVENT#{line_ending}").first
          parse(data_string)
        end
      end

      # Parse data and return new Event.
      def parse(str)
        attributes = str.split($/).inject({}) do |hash, line|
          key, value = line.split(':', 2)
          next hash if key =~ /^BEGIN$|^END$/ # Ignore any other book ends.
          value = value.chomp if value
          key =
            key.
            split(';', 2).
            first. # Ignore extra data other than just the name of the attribute.
            gsub('-', '_') # underscore.
          hash[key.downcase.to_sym] = value
          hash
        end

        new(attributes)
      end

    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ics-0.2 lib/ics/event.rb