Module: Ical2hash

Defined in:
lib/ical2hash.rb,
lib/ical2hash/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
'0.2.0'

Class Method Summary collapse

Class Method Details

.convert(ics_txt) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ical2hash.rb', line 8

def self.convert(ics_txt)
  hash = {}
  pointer = hash
  stack = []
  ics_txt.split(/\R/).each do |r|
    next if r == ''

    key, _, value = r.partition(':')
    if key == 'BEGIN'
      stack << pointer
      pointer[value] ||= []
      pointer[value] << {}
      pointer = pointer[value][-1]
    elsif key == 'END'
      pointer = stack.pop
    else
      pointer[key] = value
    end
  end
  hash
end

.revert(ics_hash) ⇒ Object



30
31
32
33
34
35
# File 'lib/ical2hash.rb', line 30

def self.revert(ics_hash)
  pointer = ics_hash
  txt_array = []
  dig(pointer, txt_array)
  "#{txt_array.join("\r\n")}\r\n"
end