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, feed_link: '') ⇒ ItemParser



21
22
23
24
# File 'lib/yarss/atom/item_parser.rb', line 21

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

Instance Attribute Details

#dataHash

Parsed Atom feed item.



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

def data
  @data
end

Feed link URL.



17
18
19
# File 'lib/yarss/atom/item_parser.rb', line 17

def feed_link
  @feed_link
end

Instance Method Details

#authorString

Extract the author.

Raises:



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

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

#contentString

Extract the content.



99
100
101
102
103
104
105
# File 'lib/yarss/atom/item_parser.rb', line 99

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

  Attribute.absolutize_urls(content, feed_link)
end

#idString

Extract the ID.

Raises:



48
49
50
51
52
# File 'lib/yarss/atom/item_parser.rb', line 48

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

Extract the link.

Raises:



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

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.

Raises:

  • (ParseError)

    If a required field is not found.



32
33
34
35
36
37
38
39
40
41
# File 'lib/yarss/atom/item_parser.rb', line 32

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

#titleString

Extract the title.

Raises:



59
60
61
62
63
# File 'lib/yarss/atom/item_parser.rb', line 59

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

#updatedDateTime

Extract the updated date.

Raises:



70
71
72
73
74
# File 'lib/yarss/atom/item_parser.rb', line 70

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