Class: Yarss::Rss::FeedParser
- Inherits:
-
Object
- Object
- Yarss::Rss::FeedParser
- Defined in:
- lib/yarss/rss/feed_parser.rb
Overview
Extract title, link, description and items from a parsed RSS feed.
Instance Attribute Summary collapse
-
#data ⇒ Hash
Parsed RSS feed.
Instance Method Summary collapse
-
#description ⇒ String
Extract the description.
-
#feed ⇒ Hash
Extract the channel data.
-
#initialize(data) ⇒ FeedParser
constructor
A new instance of FeedParser.
-
#items ⇒ Array<Item>
Extract and parse the items.
-
#link ⇒ String
Extract the link.
-
#parse ⇒ Feed
Parse out the feed title, link, description and items and wrap them in a data object.
-
#title ⇒ String
Extract the title.
Constructor Details
#initialize(data) ⇒ FeedParser
Returns a new instance of FeedParser.
18 19 20 |
# File 'lib/yarss/rss/feed_parser.rb', line 18 def initialize(data) self.data = data end |
Instance Attribute Details
#data ⇒ Hash
Parsed RSS feed.
15 16 17 |
# File 'lib/yarss/rss/feed_parser.rb', line 15 def data @data end |
Instance Method Details
#description ⇒ String
Extract the description.
73 74 75 76 77 |
# File 'lib/yarss/rss/feed_parser.rb', line 73 def description Attribute.value(feed.fetch('description')) rescue KeyError => e raise ParseError, e end |
#feed ⇒ Hash
Extract the channel data.
42 43 44 45 46 |
# File 'lib/yarss/rss/feed_parser.rb', line 42 def feed @feed ||= data.fetch('rss').fetch('channel') rescue KeyError => e raise ParseError, e end |
#items ⇒ Array<Item>
Extract and parse the items.
84 85 86 87 88 89 90 |
# File 'lib/yarss/rss/feed_parser.rb', line 84 def items items = feed.fetch('item') items = [items] unless items.is_a?(Array) items.map { |d| ItemParser.new(d).parse } rescue KeyError => e raise ParseError, e end |
#link ⇒ String
Extract the link.
64 65 66 67 68 |
# File 'lib/yarss/rss/feed_parser.rb', line 64 def link Attribute.link_value(feed.fetch('link')) rescue KeyError => e raise ParseError, e end |
#parse ⇒ Feed
Parse out the feed title, link, description and items and wrap them in a data object.
28 29 30 31 32 33 34 35 |
# File 'lib/yarss/rss/feed_parser.rb', line 28 def parse Feed.new( title: title, link: link, description: description, items: items ) end |
#title ⇒ String
Extract the title.
53 54 55 56 57 |
# File 'lib/yarss/rss/feed_parser.rb', line 53 def title Attribute.value(feed.fetch('title')) rescue KeyError => e raise ParseError, e end |