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 Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

debug, quiet

Constructor Details

#initialize(src, strict = true) ⇒ Parser

Returns a new instance of Parser.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/icalendar/parser.rb', line 45

def initialize(src, strict = true)
  # Setup the parser method hash table
  setup_parsers

  # The default behavior is to raise an error when the parser
  # finds an unknown property. Set this to false to discard
  # unknown properties instead of raising an error.
  @strict = strict

  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 Attribute Details

#strictObject

Defines if this is a strict parser.



43
44
45
# File 'lib/icalendar/parser.rb', line 43

def strict
  @strict
end

Instance Method Details

#next_lineObject

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/icalendar/parser.rb', line 70

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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/icalendar/parser.rb', line 100

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 Calendar.new
      @@logger.debug "Added parsed calendar..."
      calendars << cal
    end
  end

  calendars
end