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

#contentString

Extract the content.

Returns:

  • (String)


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

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:



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

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

Extract the link.

Returns:

  • (String)

Raises:



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

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

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

#titleString

Extract the title.

Returns:

  • (String)

Raises:



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

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

#updatedDateTime

Extract the updated date.

Returns:

  • (DateTime)

Raises:



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

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