Class: EventDb::EventReader::YamlParser

Inherits:
Object
  • Object
show all
Includes:
LogUtils::Logging
Defined in:
lib/eventdb/reader.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ YamlParser

Returns a new instance of YamlParser.



39
40
41
# File 'lib/eventdb/reader.rb', line 39

def initialize( text )
  @text = text
end

Class Method Details

.parse(text) ⇒ Object



34
# File 'lib/eventdb/reader.rb', line 34

def self.parse( text )  new( text ).parse; end

Instance Method Details

#parseObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/eventdb/reader.rb', line 43

def parse
  events = []
  recs = YAML.load( @text )

  recs.each do |rec|
    if rec.has_key?( 'title' ) ||
       rec.has_key?( 'link'  ) ||
       rec.has_key?( 'place' )
       title      = rec['title']
       link       = rec['link']
       place      = rec['place']
       start_date = rec['start_date']    # note: already parsed into a date (NOT string) by yaml parser!!
       end_date   = rec['end_date']      # note: already parsed into a date (NOT string) by yaml parser!!
    else   ## assume "default" format
        title      = rec['name']
        link       = rec['url']
        place      = rec['location']
        start_date = rec['start']    # note: already parsed into a date (NOT string) by yaml parser!!
        end_date   = rec['end']      # note: already parsed into a date (NOT string) by yaml parser!!
    end

    event = Event.new( title, link,
                       place,
                       start_date, end_date )
    ## pp event

    events << event
  end

  events
end