Class: Yarss::Atom::ItemParser

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

Overview

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

atomenabled.org/developers/syndication/#requiredEntryElements

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ ItemParser

Returns a new instance of ItemParser.

Parameters:

  • data (Hash)

    Parsed Atom feed item.



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

def initialize(data)
  self.data = data
end

Instance Attribute Details

#dataHash

Parsed Atom feed item.

Returns:

  • (Hash)


12
13
14
# File 'lib/yarss/atom/item_parser.rb', line 12

def data
  @data
end

Instance Method Details

#authorString

Extract the author.

Returns:

  • (String)

Raises:



85
86
87
# File 'lib/yarss/atom/item_parser.rb', line 85

def author
  Attribute.author_value(data['author'] || '')
end

#contentString

Extract the content.

Returns:

  • (String)


92
93
94
95
96
97
# File 'lib/yarss/atom/item_parser.rb', line 92

def content
  summary = Attribute.value(data['summary'] || '')
  content = Attribute.value(data['content'] || '')
  return content unless content.empty?
  summary
end

#idString

Extract the ID.

Returns:

  • (String)

Raises:



41
42
43
44
45
# File 'lib/yarss/atom/item_parser.rb', line 41

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

Extract the link.

Returns:

  • (String)

Raises:



74
75
76
77
78
# File 'lib/yarss/atom/item_parser.rb', line 74

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
34
# File 'lib/yarss/atom/item_parser.rb', line 25

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

#titleString

Extract the title.

Returns:

  • (String)

Raises:



52
53
54
55
56
# File 'lib/yarss/atom/item_parser.rb', line 52

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

#updatedDateTime

Extract the updated date.

Returns:

  • (DateTime)

Raises:



63
64
65
66
67
# File 'lib/yarss/atom/item_parser.rb', line 63

def updated
  DateTime.parse(data.fetch('updated'))
rescue KeyError, ArgumentError => e
  raise ParseError, e
end