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) ⇒ ItemParser

Returns a new instance of ItemParser.

Parameters:

  • data (Hash)

    Parsed Rdf feed item.



13
14
15
# File 'lib/yarss/rdf/item_parser.rb', line 13

def initialize(data)
  self.data = data
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

Instance Method Details

#descriptionString

Extract the description.

Returns:

  • (String)


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

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

#idString

Extract the ID.

Returns:

  • (String)

Raises:



38
39
40
41
42
43
44
45
46
# File 'lib/yarss/rdf/item_parser.rb', line 38

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:



80
81
82
83
84
# File 'lib/yarss/rdf/item_parser.rb', line 80

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.



23
24
25
26
27
28
29
30
31
# File 'lib/yarss/rdf/item_parser.rb', line 23

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

#titleString

Extract the title.

Returns:

  • (String)

Raises:



53
54
55
56
57
# File 'lib/yarss/rdf/item_parser.rb', line 53

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

#updatedDateTime

Extract the updated date.

Returns:

  • (DateTime)

Raises:



64
65
66
67
68
69
70
71
72
73
# File 'lib/yarss/rdf/item_parser.rb', line 64

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