Class: Yarss::Rss::FeedParser

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

Overview

Extract title, link, description and items from a parsed RSS feed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ FeedParser

Returns a new instance of FeedParser.

Parameters:

  • data (Hash)

    Parsed RSS feed.



18
19
20
# File 'lib/yarss/rss/feed_parser.rb', line 18

def initialize(data)
  self.data = data
end

Instance Attribute Details

#dataHash

Parsed RSS feed.

Returns:

  • (Hash)


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

def data
  @data
end

Instance Method Details

#descriptionString

Extract the description.

Returns:

  • (String)


73
74
75
76
77
# File 'lib/yarss/rss/feed_parser.rb', line 73

def description
  Attribute.value(feed.fetch('description'))
rescue KeyError => e
  raise ParseError, e
end

#feedHash

Extract the channel data.

Returns:

  • (Hash)

Raises:



42
43
44
45
46
# File 'lib/yarss/rss/feed_parser.rb', line 42

def feed
  @feed ||= data.fetch('rss').fetch('channel')
rescue KeyError => e
  raise ParseError, e
end

#itemsArray<Item>

Extract and parse the items.

Returns:

Raises:



84
85
86
87
88
89
90
# File 'lib/yarss/rss/feed_parser.rb', line 84

def items
  items = feed.fetch('item')
  items = [items] unless items.is_a?(Array)
  items.map { |d| ItemParser.new(d).parse }
rescue KeyError => e
  raise ParseError, e
end

Extract the link.

Returns:

  • (String)

Raises:



64
65
66
67
68
# File 'lib/yarss/rss/feed_parser.rb', line 64

def link
  Attribute.link_value(feed.fetch('link'))
rescue KeyError => e
  raise ParseError, e
end

#parseFeed

Parse out the feed title, link, description and items and wrap them in a data object.

Returns:

Raises:

  • (ParseError)

    If a required field is not found.



28
29
30
31
32
33
34
35
# File 'lib/yarss/rss/feed_parser.rb', line 28

def parse
  Feed.new(
    title:       title,
    link:        link,
    description: description,
    items:       items
  )
end

#titleString

Extract the title.

Returns:

  • (String)

Raises:



53
54
55
56
57
# File 'lib/yarss/rss/feed_parser.rb', line 53

def title
  Attribute.value(feed.fetch('title'))
rescue KeyError => e
  raise ParseError, e
end