Class: Yarss::Rdf::ItemParser

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, feed_link: '') ⇒ ItemParser

Returns a new instance of ItemParser.

Parameters:

  • data (Hash)

    Parsed Rdf feed item.

  • feed_link (String) (defaults to: '')

    Feed link URL.



19
20
21
22
# File 'lib/yarss/rdf/item_parser.rb', line 19

def initialize(data, feed_link: '')
  self.data      = data
  self.feed_link = feed_link
end

Instance Attribute Details

#dataHash

Parsed Rdf feed item.

Returns:

  • (Hash)


10
11
12
# File 'lib/yarss/rdf/item_parser.rb', line 10

def data
  @data
end

Feed link URL.

Returns:

  • (String)


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

def feed_link
  @feed_link
end

Instance Method Details

#authorString

Extract the author.

Returns:

  • (String)

Raises:



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/yarss/rdf/item_parser.rb', line 99

def author
  author = if data['dc:creator']
             data['dc:creator']
           else
             data.fetch('creator')
           end

  Attribute.value(author)
rescue KeyError => e
  raise ParseError, e
end

#descriptionString

Extract the description.

Returns:

  • (String)


114
115
116
117
118
119
# File 'lib/yarss/rdf/item_parser.rb', line 114

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

#idString

Extract the ID.

Returns:

  • (String)

Raises:



46
47
48
49
50
51
52
53
54
# File 'lib/yarss/rdf/item_parser.rb', line 46

def id
  if data['about']
    data['about']
  else
    data.fetch('rdf:about')
  end
rescue KeyError => e
  raise ParseError, e
end

Extract the link.

Returns:

  • (String)

Raises:



88
89
90
91
92
# File 'lib/yarss/rdf/item_parser.rb', line 88

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.



30
31
32
33
34
35
36
37
38
39
# File 'lib/yarss/rdf/item_parser.rb', line 30

def parse
  Item.new(
    id:         id,
    title:      title,
    updated_at: updated,
    link:       link,
    author:     author,
    content:    description
  )
end

#titleString

Extract the title.

Returns:

  • (String)

Raises:



61
62
63
64
65
# File 'lib/yarss/rdf/item_parser.rb', line 61

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

#updatedDateTime

Extract the updated date.

Returns:

  • (DateTime)

Raises:



72
73
74
75
76
77
78
79
80
81
# File 'lib/yarss/rdf/item_parser.rb', line 72

def updated
  date = if data['date']
           data['date']
         else
           data.fetch('dc:date')
         end
  DateTime.parse(date)
rescue KeyError, ArgumentError => e
  raise ParseError, e
end