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.2'

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')

puts "#{feed.title}, #{feed.link}, #{feed.description}"

feed.items.each do |item|
  puts "#{item.id}, #{item.title}, #{item.updated_at}, #{item.link}"
  puts item.content
end

Parameters:

  • path (String)

    Path to a XML.

Returns:

Raises:



92
93
94
95
# File 'lib/yarss.rb', line 92

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'))

puts "#{feed.title}, #{feed.link}, #{feed.description}"

feed.items.each do |item|
  puts "#{item.id}, #{item.title}, #{item.updated_at}, #{item.link}"
  puts item.content
end

Parameters:

  • io (#read)

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

Returns:

Raises:



69
70
71
72
# File 'lib/yarss.rb', line 69

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('...)

puts "#{feed.title}, #{feed.link}, #{feed.description}"

feed.items.each do |item|
  puts "#{item.id}, #{item.title}, #{item.updated_at}, #{item.link}"
  puts item.content
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:



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/yarss.rb', line 116

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')

puts "#{feed.title}, #{feed.link}, #{feed.description}"

feed.items.each do |item|
  puts "#{item.id}, #{item.title}, #{item.updated_at}, #{item.link}"
  puts item.content
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:



42
43
44
45
46
47
48
# File 'lib/yarss.rb', line 42

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