Class: AtomFeed::AtomFeed

Inherits:
Object show all
Defined in:
lib/atom_feed/atom_feed.rb

Overview

AtomFeed::AtomFeed is the central place for working with feeds in the Atom format.

Opening an atom feed from the network or a file system is done like this:

feed = AtomFeed::AtomFeed.open(open("http://example.com/atom.xml"))

If you have a file you should do:

f = File.open("feed.xml")
feed = AtomFeed::AtomFeed.open(f)
f.close

If you have an XML string you can do:

feed = AtomFeed::AtomFeed.open("<feed ...")

One can open and parse the feed like so:

AtomFeed::AtomFeed.open(...) do |feed|
  puts feed.title
  feed.entries do |entry|
    puts entry.title
  end
end

You can access OpenSearch extensions by using AtomFeed.open_search. Access to other embedded XML types are available by using AtomFeed.doc+ directly. It's a Nokogiri::XML instance.

AtomFeed uses Nokogiri for parsing.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc) ⇒ AtomFeed

Returns a new instance of AtomFeed.



51
52
53
# File 'lib/atom_feed/atom_feed.rb', line 51

def initialize(doc)
  @doc = doc
end

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



49
50
51
# File 'lib/atom_feed/atom_feed.rb', line 49

def doc
  @doc
end

Class Method Details

.open(string_or_io, url = nil, encoding = nil) {|feed| ... } ⇒ Object

Yields:

  • (feed)


55
56
57
58
59
60
# File 'lib/atom_feed/atom_feed.rb', line 55

def self.open(string_or_io, url = nil, encoding = nil)
  doc = Nokogiri::XML(string_or_io, url, encoding)
  feed = AtomFeed.new(doc)
  yield feed if block_given?
  feed
end

Instance Method Details

#authorsObject

Array of Authors (optional).



78
79
80
81
# File 'lib/atom_feed/atom_feed.rb', line 78

def authors
  nodes = @doc.xpath("atom:feed/atom:author", ::AtomFeed::NS) || []
  nodes.map { |node| AtomPerson.new(node) }
end

#categoriesObject

Array of feed categories (optional).



96
97
98
99
# File 'lib/atom_feed/atom_feed.rb', line 96

def categories
  nodes = @doc.xpath("atom:feed/atom:category", ::AtomFeed::NS) || []
  nodes.map { |node| AtomCategory.new(node) }
end

#contributorsObject

Array of contributors (optional).



102
103
104
105
# File 'lib/atom_feed/atom_feed.rb', line 102

def contributors
  nodes = @doc.xpath("atom:feed/atom:contributor", ::AtomFeed::NS) || []
  nodes.map { |node| AtomPerson.new(node) }
end

#entriesObject

Array of feed entries (optional).



90
91
92
93
# File 'lib/atom_feed/atom_feed.rb', line 90

def entries
  nodes = @doc.xpath("atom:feed/atom:entry", ::AtomFeed::NS) || []
  nodes.map { |node| AtomFeedEntry.new(node) }
end

#generatorObject

Generator (optional).



108
109
110
111
112
# File 'lib/atom_feed/atom_feed.rb', line 108

def generator
  node = @doc.at_xpath("atom:feed/atom:generator", ::AtomFeed::NS)
  return nil unless node
  AtomGenerator.new(node)
end

#iconObject

Icon (optional).



115
116
117
# File 'lib/atom_feed/atom_feed.rb', line 115

def icon
  @doc.at_xpath("atom:feed/atom:icon", ::AtomFeed::NS).try(:content)
end

#idObject

Feed id (required).



63
64
65
# File 'lib/atom_feed/atom_feed.rb', line 63

def id
  @doc.at_xpath("atom:feed/atom:id", ::AtomFeed::NS).content
end

Array of links (optional).



84
85
86
87
# File 'lib/atom_feed/atom_feed.rb', line 84

def links
  nodes = @doc.xpath("atom:feed/atom:link", ::AtomFeed::NS) || []
  nodes.map { |node| AtomLink.new(node) }
end

#logoObject

Logo (optional).



120
121
122
# File 'lib/atom_feed/atom_feed.rb', line 120

def 
  @doc.at_xpath("atom:feed/atom:logo", ::AtomFeed::NS).try(:content)
end

#open_searchObject

Open Search extensions (optional)



139
140
141
# File 'lib/atom_feed/atom_feed.rb', line 139

def open_search
  @open_search ||= OpenSearch.new(@doc)
end

#rightsObject

rights (optional)



125
126
127
128
129
# File 'lib/atom_feed/atom_feed.rb', line 125

def rights
  node = @doc.at_xpath("atom:feed/atom:rights", ::AtomFeed::NS)
  return nil unless node
  AtomText.new(node)
end

#subtitleObject

subtitle (optional)



132
133
134
135
136
# File 'lib/atom_feed/atom_feed.rb', line 132

def subtitle
  node = @doc.at_xpath("atom:feed/atom:subtitle", ::AtomFeed::NS)
  return nil unless node
  AtomText.new(node)
end

#titleObject

Feed title (required).



68
69
70
# File 'lib/atom_feed/atom_feed.rb', line 68

def title
  @doc.at_xpath("atom:feed/atom:title", ::AtomFeed::NS).content
end

#updatedObject

Feed update date (required).



73
74
75
# File 'lib/atom_feed/atom_feed.rb', line 73

def updated
  Time.parse @doc.at_xpath("atom:feed/atom:updated", ::AtomFeed::NS).content
end