Class Icalendar::Parser
In: lib/icalendar/parser.rb
Parent: Icalendar::Base

Methods

new   next_line   parse  

Constants

DATE = '(\d\d\d\d)-?(\d\d)-?(\d\d)'   date = date-fullyear ["-"] date-month ["-"] date-mday date-fullyear = 4 DIGIT date-month = 2 DIGIT date-mday = 2 DIGIT
TIME = '(\d\d):?(\d\d):?(\d\d)(\.\d+)?(Z|[-+]\d\d:?\d\d)?'   time = time-hour [":"] time-minute [":"] time-second [time-secfrac] [time-zone] time-hour = 2 DIGIT time-minute = 2 DIGIT time-second = 2 DIGIT time-secfrac = "," 1*DIGIT time-zone = "Z" / time-numzone time-numzome = sign time-hour [":"] time-minute
NAME = '[-a-z0-9]+'   1*(ALPHA / DIGIT / "=")
QSTR = '"[^"]*"'   <"> <Any character except CTLs, DQUOTE> <">
LINE = "(#{NAME})(.*(?:#{QSTR})|(?:[^:]*))\:(.*)"   Contentline
PTEXT = '[^";:,]*'   *<Any character except CTLs, DQUOTE, ";", ":", ",">
PVALUE = "#{QSTR}|#{PTEXT}"   param-value = ptext / quoted-string
PARAM = ";(#{NAME})(=?)((?:#{PVALUE})(?:,#{PVALUE})*)"   param = name "=" param-value *("," param-value)

Public Class methods

[Source]

# File lib/icalendar/parser.rb, line 42
    def initialize(src)
      # Setup the parser method hash table
      setup_parsers()

      if src.respond_to?(:gets)
        @file = src
      elsif (not src.nil?) and src.respond_to?(:to_s)
        @file = StringIO.new(src.to_s, 'r')
      else
        raise ArgumentError, "CalendarParser.new cannot be called with a #{src.class} type!"
      end

      @prev_line = @file.gets
      @prev_line.chomp! unless @prev_line.nil?

      @@logger.debug("New Calendar Parser: #{@file.inspect}")
    end

Public Instance methods

Define next line for an IO object. Works for strings now with StringIO

[Source]

# File lib/icalendar/parser.rb, line 62
    def next_line
      line = @prev_line

      if line.nil? 
        return nil 
      end

      # Loop through until we get to a non-continuation line...
      loop do
        nextLine = @file.gets
        @@logger.debug "new_line: #{nextLine}"

        if !nextLine.nil?
          nextLine.chomp!
        end

        # If it's a continuation line, add it to the last.
        # If it's an empty line, drop it from the input.
        if( nextLine =~ /^[ \t]/ )
          line << nextLine[1, nextLine.size]
        elsif( nextLine =~ /^$/ )
        else
          @prev_line = nextLine
          break
        end
      end
      line
    end

Parse the calendar into an object representation

[Source]

# File lib/icalendar/parser.rb, line 92
    def parse
      calendars = []

      @@logger.debug "parsing..."
      # Outer loop for Calendar objects
      while (line = next_line) 
        fields = parse_line(line)

        # Just iterate through until we find the beginning of a calendar object
        if fields[:name] == "BEGIN" and fields[:value] == "VCALENDAR"
          cal = parse_component
          @@logger.debug "Added parsed calendar..."
          calendars << cal
        end
      end

      calendars
    end

[Validate]