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.
-
#feed_link ⇒ String
Feed link URL.
Instance Method Summary collapse
-
#author ⇒ String
Extract the author.
-
#description ⇒ String
Extract the content.
-
#guid ⇒ String
Extract the ID.
-
#initialize(data, feed_link: '') ⇒ 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, feed_link: '') ⇒ ItemParser
Returns a new instance of ItemParser.
23 24 25 26 |
# File 'lib/yarss/rss/item_parser.rb', line 23 def initialize(data, feed_link: '') self.data = data self.feed_link = feed_link end |
Instance Attribute Details
#data ⇒ Hash
Parsed RSS feed item.
14 15 16 |
# File 'lib/yarss/rss/item_parser.rb', line 14 def data @data end |
#feed_link ⇒ String
Feed link URL.
19 20 21 |
# File 'lib/yarss/rss/item_parser.rb', line 19 def feed_link @feed_link end |
Instance Method Details
#author ⇒ String
Extract the author.
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/yarss/rss/item_parser.rb', line 96 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.
113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/yarss/rss/item_parser.rb', line 113 def description content = if data['content:encoded'] data['content:encoded'] elsif data['description'] data['description'] else '' end content = Attribute.value(content) Attribute.absolutize_urls(content, feed_link) end |
#guid ⇒ String
Extract the ID. Use the title if guid is not present and title is.
50 51 52 53 54 55 |
# File 'lib/yarss/rss/item_parser.rb', line 50 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.
85 86 87 88 89 |
# File 'lib/yarss/rss/item_parser.rb', line 85 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.
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/yarss/rss/item_parser.rb', line 34 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.
72 73 74 75 76 77 78 |
# File 'lib/yarss/rss/item_parser.rb', line 72 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.
62 63 64 65 66 67 |
# File 'lib/yarss/rss/item_parser.rb', line 62 def title Attribute.value(data.fetch('title')) rescue KeyError => e return Attribute.value(data['guid']) if data['guid'] raise ParseError, e end |