Class: BubbleWrap::RSSParser

Inherits:
Object
  • Object
show all
Defined in:
motion/rss_parser.rb

Defined Under Namespace

Classes: RSSItem

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, data = false) ⇒ RSSParser

Returns a new instance of RSSParser.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'motion/rss_parser.rb', line 49

def initialize(input, data=false)
  if data
    data_to_parse = input.respond_to?(:to_data) ? input.to_data : input
    @source = data_to_parse
    @parser = NSXMLParser.alloc.initWithData(@source)
  else
    url = input.is_a?(NSURL) ? input : NSURL.alloc.initWithString(input)
    @source = url
    # Delay the initialization of the underlying NSXMLParser so it
    # doesn't load the content of the url until it's triggered.
    @parser = Proc.new{ NSXMLParser.alloc.initWithContentsOfURL(url) }
  end
  self.state = :initializes
  self
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



25
26
27
# File 'motion/rss_parser.rb', line 25

def debug
  @debug
end

#delegateObject

Returns the value of attribute delegate.



25
26
27
# File 'motion/rss_parser.rb', line 25

def delegate
  @delegate
end

#docObject

Returns the value of attribute doc.



25
26
27
# File 'motion/rss_parser.rb', line 25

def doc
  @doc
end

#parser(parser, parseErrorOccurred: parse_error) ⇒ Object

method called when the parser encounters an error error can be retrieved with parserError



93
94
95
# File 'motion/rss_parser.rb', line 93

def parser
  @parser
end

#parser_errorObject

Returns the value of attribute parser_error.



25
26
27
# File 'motion/rss_parser.rb', line 25

def parser_error
  @parser_error
end

#sourceObject

Returns the value of attribute source.



25
26
27
# File 'motion/rss_parser.rb', line 25

def source
  @source
end

#stateObject

Returns the value of attribute state.



26
27
28
# File 'motion/rss_parser.rb', line 26

def state
  @state
end

Instance Method Details

#parse(&block) ⇒ Object

Starts the parsing and send each parsed item through its block.

Usage:

feed.parse do |item|
  puts item.link
end


78
79
80
81
82
83
84
# File 'motion/rss_parser.rb', line 78

def parse(&block)
  @block = block
  @parser = @parser.call if @parser.respond_to?(:call)
  @parser.shouldProcessNamespaces = true
  @parser.delegate ||= self
  @parser.parse
end

#parserDidEndDocument(parser) ⇒ Object

delegate getting called when the parsing is done If a block was set, it will be called on each parsed items



130
131
132
133
# File 'motion/rss_parser.rb', line 130

def parserDidEndDocument(parser)
  puts "done parsing" if debug
  self.state = :is_done
end

#parserDidStartDocument(parser) ⇒ Object

Delegate getting called when parsing starts



87
88
89
90
# File 'motion/rss_parser.rb', line 87

def parserDidStartDocument(parser)
  puts "starting parsing.." if debug
  self.state = :parses
end

#parserErrorObject



135
136
137
# File 'motion/rss_parser.rb', line 135

def parserError
  @parser_error || @parser.parserError
end