Class: Feedtxt::Parser

Inherits:
Object
  • Object
show all
Includes:
LogUtils::Logging
Defined in:
lib/feedtxt/parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Parser

Note: lets keep/use same API as RSS::Parser for now



17
18
19
# File 'lib/feedtxt/parser.rb', line 17

def initialize( text )
  @text = text
end

Class Method Details

.parse(text, opts = {}) ⇒ Object

convenience class/factory method



12
13
14
# File 'lib/feedtxt/parser.rb', line 12

def self.parse( text, opts={} )
  self.new( text ).parse
end

Instance Method Details

#parseObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/feedtxt/parser.rb', line 22

def parse
  ## auto-detect format
  ##   use "best" matching format (e.g. first match by pos(ition))

  klass = YAML           ## default to yamlparser for now
  pos   = 9_999_999     ## todo:use  MAX INTEGER or something!!

  json = @text.index( /#{JSON::FEED_BEGIN}/ )
  if json    # found e.g. not nil? incl. 0
    pos   = json
    klass = JSON
  end

  ini = @text.index( /#{INI::FEED_BEGIN}/ )
  if ini && ini < pos  # found e.g. not nil? and match before last?
    pos   = ini
    klass = INI
  end

  yaml = @text.index( /#{YAML::FEED_BEGIN}/ )
  if yaml && yaml < pos  # found e.g. not nil? and match before last?
    pos   = yaml
    klass = YAML
  end

  feed = klass.parse( @text )
  feed
end