Class: Yarss::Rdf::FeedParser

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ FeedParser

Returns a new instance of FeedParser.

Parameters:

  • data (Hash)

    Parsed Rdf feed.



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

def initialize(data)
  self.data = data
end

Instance Attribute Details

#dataHash

Parsed Rdf feed.

Returns:

  • (Hash)


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

def data
  @data
end

Instance Method Details

#channelHash

Extract the channel data.

Returns:

  • (Hash)

Raises:



59
60
61
62
63
# File 'lib/yarss/rdf/feed_parser.rb', line 59

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

#descriptionString

Extract the description.

Returns:

  • (String)


90
91
92
93
94
# File 'lib/yarss/rdf/feed_parser.rb', line 90

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

#feedHash

Extract the feed data.

Returns:

  • (Hash)

Raises:



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/yarss/rdf/feed_parser.rb', line 42

def feed
  @feed ||= begin
    if data['RDF']
      data['RDF']
    else
      data.fetch('rdf:RDF')
    end
  end
rescue KeyError => e
  raise ParseError, e
end

#itemsArray<Item>

Extract and parse the items.

Returns:

Raises:



101
102
103
104
105
106
107
# File 'lib/yarss/rdf/feed_parser.rb', line 101

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:



81
82
83
84
85
# File 'lib/yarss/rdf/feed_parser.rb', line 81

def link
  Attribute.link_value(channel.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/rdf/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:



70
71
72
73
74
# File 'lib/yarss/rdf/feed_parser.rb', line 70

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