Class: Icalendar::Parser

Inherits:
Base show all
Defined in:
lib/icalendar/parser.rb

Constant Summary collapse

DATE =

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

'(\d\d\d\d)-?(\d\d)-?(\d\d)'
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

'(\d\d):?(\d\d):?(\d\d)(\.\d+)?(Z|[-+]\d\d:?\d\d)?'

Instance Method Summary collapse

Methods inherited from Base

debug, quiet

Constructor Details

#initialize(src) ⇒ Parser

Returns a new instance of Parser.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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

Instance Method Details

#next_lineObject

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



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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

#parseObject

Parse the calendar into an object representation



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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