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
-
#author ⇒ String
Extract the author.
-
#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
#author ⇒ String
Extract the author.
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/yarss/rss/item_parser.rb', line 87 def = if data['dc:creator'] data['dc:creator'] elsif data['creator'] data['creator'] elsif data['author'] data['author'] else '' end Attribute.value() end |
#description ⇒ String
Extract the content.
104 105 106 107 108 109 110 111 |
# File 'lib/yarss/rss/item_parser.rb', line 104 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.
41 42 43 44 45 46 |
# File 'lib/yarss/rss/item_parser.rb', line 41 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.
76 77 78 79 80 |
# File 'lib/yarss/rss/item_parser.rb', line 76 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 34 |
# File 'lib/yarss/rss/item_parser.rb', line 25 def parse Item.new( id: guid, title: title, updated_at: pub_date, link: link, author: , content: description ) end |
#pub_date ⇒ DateTime
Extract the updated date.
63 64 65 66 67 68 69 |
# File 'lib/yarss/rss/item_parser.rb', line 63 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.
53 54 55 56 57 58 |
# File 'lib/yarss/rss/item_parser.rb', line 53 def title Attribute.value(data.fetch('title')) rescue KeyError => e return Attribute.value(data['guid']) if data['guid'] raise ParseError, e end |