class Icalendar::Parser

Constants

DATE

date = date-fullyear [“-”] date-month [“-”] date-mday date-fullyear = 4 DIGIT date-month = 2 DIGIT date-mday = 2 DIGIT

LINE

Contentline

NAME

1*(ALPHA / DIGIT / “=”)

PARAM

param = name “=” param-value *(“,” param-value)

PTEXT

*<Any character except CTLs, DQUOTE, “;”, “:”, “,”>

PVALUE

param-value = ptext / quoted-string

QSTR

<“> <Any character except CTLs, DQUOTE> <”>

TIME

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

Public Class Methods

new(src) click to toggle 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

next_line() click to toggle source

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

# 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() click to toggle source

Parse the calendar into an object representation

# 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