Class: Yarss::Rss::ItemParser
- Inherits:
-
Object
- Object
- Yarss::Rss::ItemParser
- Defined in:
- lib/yarss/rss/item_parser.rb
Overview
Extract id, title, updated, link and content from a feed item.
Instance Attribute Summary collapse
-
#data ⇒ Hash
Parsed RSS feed item.
Instance Method Summary collapse
-
#description ⇒ String
Extract the content.
-
#guid ⇒ String
Extract the ID.
-
#initialize(data) ⇒ ItemParser
constructor
A new instance of ItemParser.
-
#link ⇒ String
Extract the link.
-
#parse ⇒ Item
Parse out the feed item id, title, updated, link and content and wrap them in a data object.
-
#pub_date ⇒ DateTime
Extract the updated date.
-
#title ⇒ String
Extract the title.
Constructor Details
#initialize(data) ⇒ ItemParser
Returns a new instance of ItemParser.
15 16 17 |
# File 'lib/yarss/rss/item_parser.rb', line 15 def initialize(data) self.data = data end |
Instance Attribute Details
#data ⇒ Hash
Parsed RSS feed item.
12 13 14 |
# File 'lib/yarss/rss/item_parser.rb', line 12 def data @data end |
Instance Method Details
#description ⇒ String
Extract the content.
84 85 86 87 88 89 90 91 |
# File 'lib/yarss/rss/item_parser.rb', line 84 def description description = Attribute.value(data['description'] || '') return Attribute.value(data['content:encoded']) if description.empty? && data['content:encoded'] description end |
#guid ⇒ String
Extract the ID. Use the title if guid is not present and title is.
40 41 42 43 44 45 |
# File 'lib/yarss/rss/item_parser.rb', line 40 def guid Attribute.value(data.fetch('guid')) rescue KeyError => e return Digest::MD5.hexdigest(data['title']) if data['title'] raise ParseError, e end |
#link ⇒ String
Extract the link.
75 76 77 78 79 |
# File 'lib/yarss/rss/item_parser.rb', line 75 def link Attribute.link_value(data.fetch('link')) rescue KeyError => e raise ParseError, e end |
#parse ⇒ Item
Parse out the feed item id, title, updated, link and content and wrap them in a data object.
25 26 27 28 29 30 31 32 33 |
# File 'lib/yarss/rss/item_parser.rb', line 25 def parse Item.new( id: guid, title: title, updated_at: pub_date, link: link, content: description ) end |
#pub_date ⇒ DateTime
Extract the updated date.
62 63 64 65 66 67 68 |
# File 'lib/yarss/rss/item_parser.rb', line 62 def pub_date DateTime.parse(data.fetch('pubDate')) rescue ArgumentError => e raise ParseError, e rescue KeyError DateTime.now end |
#title ⇒ String
Extract the title. Use guid if title is not present and guid is.
52 53 54 55 56 57 |
# File 'lib/yarss/rss/item_parser.rb', line 52 def title Attribute.value(data.fetch('title')) rescue KeyError => e return Attribute.value(data['guid']) if data['guid'] raise ParseError, e end |