Module: Yarss

Defined in:
lib/yarss.rb,
lib/yarss/feed.rb,
lib/yarss/item.rb,
lib/yarss/version.rb,
lib/yarss/attribute.rb,
lib/yarss/rdf/feed_parser.rb,
lib/yarss/rdf/item_parser.rb,
lib/yarss/rss/feed_parser.rb,
lib/yarss/rss/item_parser.rb,
lib/yarss/atom/feed_parser.rb,
lib/yarss/atom/item_parser.rb

Overview

RSS, RDF and Atom feeds parser.

Defined Under Namespace

Modules: Atom, Attribute, Rdf, Rss Classes: Error, Feed, Item, ParseError, UnknownParserError

Constant Summary collapse

VERSION =

Version number, happy now?

'0.0.3'

Class Method Summary collapse

Class Method Details

.from_file(path) ⇒ Feed

Parse a Feed out of a path to a XML.

Examples:

feed = Yarss.from_file('path/to/feed.rss')

feed.title       # => "Foo's bars"
feed.link        # => 'http://foo.bar/'
feed.description # => 'Bars everywhere!'

feed.items.each do |item|
  item.id         # => 'id'
  item.title      # => 'Hello!'
  item.updated_at # => #<DateTIme ...>
  item.link       # => 'http://foo.bar/1'
  item.author     # => 'Joe'
  item.content    # => '<p>Hi!</p>'
end

Parameters:

  • path (String)

    Path to a XML.

Returns:

Raises:



110
111
112
113
# File 'lib/yarss.rb', line 110

def self.from_file(path)
  data = File.read(path)
  from_string(data, path)
end

.from_io(io) ⇒ Feed

Parse a Feed out of an IO (or whatever responds to read).

Examples:

feed = Yarss.from_io(Pathname.new('path/to/feed.rss'))
feed = Yarss.from_io(File.open('path/to/feed.rss', 'rb'))

feed.title       # => "Foo's bars"
feed.link        # => 'http://foo.bar/'
feed.description # => 'Bars everywhere!'

feed.items.each do |item|
  item.id         # => 'id'
  item.title      # => 'Hello!'
  item.updated_at # => #<DateTIme ...>
  item.link       # => 'http://foo.bar/1'
  item.author     # => 'Joe'
  item.content    # => '<p>Hi!</p>'
end

Parameters:

  • io (#read)

    An IO, a Pathname, something that can be read.

Returns:

Raises:



81
82
83
84
# File 'lib/yarss.rb', line 81

def self.from_io(io)
  data = io.read
  from_string(data, io)
end

.from_string(data, path_or_io = nil) ⇒ Feed

Parse a Feed out of raw XML.

Examples:

feed = Yarss.from_string('...)

feed.title       # => "Foo's bars"
feed.link        # => 'http://foo.bar/'
feed.description # => 'Bars everywhere!'

feed.items.each do |item|
  item.id         # => 'id'
  item.title      # => 'Hello!'
  item.updated_at # => #<DateTIme ...>
  item.link       # => 'http://foo.bar/1'
  item.author     # => 'Joe'
  item.content    # => '<p>Hi!</p>'
end

Parameters:

  • data (String)

    Raw RSS, RDF or Atom XML data.

  • path_or_io (String, #read) (defaults to: nil)

    Path to a file or an IO.

Returns:

Raises:



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/yarss.rb', line 140

def self.from_string(data, path_or_io = nil)
  data = MultiXml.parse(data)

  return Rss::FeedParser.new(data).parse  if data['rss']
  return Atom::FeedParser.new(data).parse if data['feed']
  return Rdf::FeedParser.new(data).parse  if data['rdf:RDF'] || data['RDF']

  msg = "Cannot find parser for #{path_or_io}" if path_or_io
  raise UnknownParserError, msg
rescue MultiXml::ParseError => e
  raise ParseError, e
end

.new(path_or_io) ⇒ Feed

Parse a Feed out of a path to a XML or an IO (or whatever responds to read).

Examples:

feed = Yarss.new('path/to/feed.rss')

feed.title       # => "Foo's bars"
feed.link        # => 'http://foo.bar/'
feed.description # => 'Bars everywhere!'

feed.items.each do |item|
  item.id         # => 'id'
  item.title      # => 'Hello!'
  item.updated_at # => #<DateTIme ...>
  item.link       # => 'http://foo.bar/1'
  item.author     # => 'Joe'
  item.content    # => '<p>Hi!</p>'
end

Parameters:

  • path_or_io (String, #read)

    A path to the XML file or an IO, a Pathname, something that can be read.

Returns:

Raises:



48
49
50
51
52
53
54
# File 'lib/yarss.rb', line 48

def self.new(path_or_io)
  if path_or_io.respond_to?(:read)
    from_io(path_or_io)
  else
    from_file(path_or_io)
  end
end