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

#descriptionString

Extract the content.

Returns:

  • (String)


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

#guidString

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

Returns:

  • (String)

Raises:



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

Extract the link.

Returns:

  • (String)

Raises:



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

#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
# 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_dateDateTime

Extract the updated date.

Returns:

  • (DateTime)


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

#titleString

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

Returns:

  • (String)

Raises:



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