Class: Yarss::Rss::ItemParser

Inherits:
Object
  • Object
show all
Defined in:
lib/yarss/rss/item_parser.rb

Overview

Extract id, title, updated, link and content from a feed item.

validator.w3.org/feed/docs/rss2.html#hrelementsOfLtitemgt

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ ItemParser

Returns a new instance of ItemParser.

Parameters:

  • data (Hash)

    Parsed RSS feed item.



15
16
17
# File 'lib/yarss/rss/item_parser.rb', line 15

def initialize(data)
  self.data = data
end

Instance Attribute Details

#dataHash

Parsed RSS feed item.

Returns:

  • (Hash)


12
13
14
# File 'lib/yarss/rss/item_parser.rb', line 12

def data
  @data
end

Instance Method Details

#authorString

Extract the author.

Returns:

  • (String)

Raises:



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/yarss/rss/item_parser.rb', line 87

def author
  author = if data['dc:creator']
             data['dc:creator']
           elsif data['creator']
             data['creator']
           elsif data['author']
             data['author']
           else
             ''
           end

  Attribute.value(author)
end

#descriptionString

Extract the content.

Returns:

  • (String)


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

#guidString

Extract the ID. Use the title if guid is not present and title is.

Returns:

  • (String)

Raises:



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

Extract the link.

Returns:

  • (String)

Raises:



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

#parseItem

Parse out the feed item id, title, updated, link and content and wrap them in a data object.

Returns:

Raises:

  • (ParseError)

    If a required field is not found.



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:     author,
    content:    description
  )
end

#pub_dateDateTime

Extract the updated date.

Returns:

  • (DateTime)


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

#titleString

Extract the title. Use guid if title is not present and guid is.

Returns:

  • (String)

Raises:



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