Class: Selene::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/selene/parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Parser

Returns a new instance of Parser.



18
19
20
# File 'lib/selene/parser.rb', line 18

def initialize(data)
  @data = encode(data)
end

Class Method Details

.parse(data) ⇒ Object



14
15
16
# File 'lib/selene/parser.rb', line 14

def self.parse(data)
  new(data).parse
end

Instance Method Details

#create_builder(name) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/selene/parser.rb', line 43

def create_builder(name)
  case name
  when 'daylight' then DaylightSavingsTimeBuilder.new
  when 'standard' then StandardTimeBuilder.new
  when 'valarm' then AlarmBuilder.new
  when 'vcalendar' then CalendarBuilder.new
  when 'vevent' then EventBuilder.new
  when 'vtimezone' then TimeZoneBuilder.new
  else ComponentBuilder.new(name)
  end
end

#encode(data) ⇒ Object



22
23
24
# File 'lib/selene/parser.rb', line 22

def encode(data)
  data.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
end

#parseObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/selene/parser.rb', line 26

def parse
  feed = FeedBuilder.new
  stack = [feed]
  Line.split(@data) do |line|
    if line.begin_component?
      builder = create_builder(line.component_name)
      stack[-1].add(line.component_name, builder)
      stack << builder
    elsif line.end_component?
      stack.pop
    else
      stack[-1].parse(line)
    end
  end
  feed.component
end