Class: Atom::Entry

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
SimpleExtensions, Xml::Parseable
Defined in:
lib/atom.rb,
lib/atom/pub.rb

Overview

Represents an Entry as defined by the Atom Syndication Format specification.

An Entry represents an individual entry within a Feed.

Parsing

An Entry can be parsed using the Entry.load_entry method. This method accepts a String containing a valid atom entry document, an IO object, or an URI to a valid atom entry document. For example:

# Using a File
entry = Entry.load_entry(File.open("/path/to/myfeedentry.atom"))

# Using a URL
Entry = Entry.load_entry(URI.parse("http://example.org/afeedentry.atom"))

The document must contain a stand alone entry element as described in the Atom Syndication Format.

Encoding

A Entry can be converted to XML using, the to_xml method that returns a valid atom entry document in a String.

Attributes

An entry has the following attributes:

id

A unique id for the entry.

updated

The Time the entry was updated.

published

The Time the entry was published.

title

The title of the entry.

summary

A short textual summary of the item.

authors

An array of Atom::Person objects that are authors of this entry.

contributors

An array of Atom::Person objects that are contributors to this entry.

rights

A string describing the rights associated with this entry.

links

An array of Atom:Link objects. (This is actually an Atom::Links array which is an Array with some sugar).

source

Metadata of a feed that was the source of this item, for feed aggregators, etc.

categories

Array of Atom::Categories.

content

The content of the entry. This will be one of Atom::Content::Text, Atom::Content:Html or Atom::Content::Xhtml.

References

See also www.atomenabled.org/developers/syndication/atom-format-spec.php#element.entry for more detailed definitions of the attributes.

Instance Attribute Summary

Attributes included from SimpleExtensions

#simple_extensions

Instance Method Summary collapse

Methods included from SimpleExtensions

#[]

Methods included from Xml::Parseable

#==, #current_node_is?, included, #next_node_is?, #parse, #to_xml

Constructor Details

#initialize(o = {}) {|_self| ... } ⇒ Entry

Initialize an Entry.

This will also yield itself, so an Entry can be constructed like this:

entry = Entry.new do |entry|
  entry.title = "My Cool entry"
end
o

An XML Reader or a Hash of attributes.

Yields:

  • (_self)

Yield Parameters:

  • _self (Atom::Entry)

    the object that the method was called on



538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
# File 'lib/atom.rb', line 538

def initialize(o = {})
  @links = Links.new
  @authors = []
  @contributors = []
  @categories = []
  
  case o
  when XML::Reader
    if current_node_is?(o, 'entry', Atom::NAMESPACE) || next_node_is?(o, 'entry', Atom::NAMESPACE)
      o.read
      parse(o)
    else
      raise ArgumentError, "Entry created with node other than atom:entry: #{o.name}"
    end
  when Hash
    o.each do |k,v|
      send("#{k.to_s}=", v)
    end
  end

  yield(self) if block_given?
end

Instance Method Details

#destroy!Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/atom/pub.rb', line 180

def destroy!
  if edit = edit_link
    uri = URI.parse(edit.href)
    response = nil
    Net::HTTP.start(uri.host, uri.port) do |http|
      response = http.delete(uri.path, {'Accept' => 'application/atom+xml', 'User-Agent' => "rAtom #{Atom::VERSION::STRING}"})
    end
    
    case response
    when Net::HTTPSuccess
    else
      raise Atom::Pub::ProtocolError, response
    end
  else
    raise Atom::Pub::NotSupported, "Entry does not have an edit link"
  end
end

#reload!Object

Reload the Entry by fetching the self link.



562
563
564
565
566
# File 'lib/atom.rb', line 562

def reload!
  if links.self
    Entry.load_entry(URI.parse(links.self.href))
  end
end

#save!Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/atom/pub.rb', line 162

def save!
  if edit = edit_link
    uri = URI.parse(edit.href)
    response = nil
    Net::HTTP.start(uri.host, uri.port) do |http|
      response = http.put(uri.path, self.to_xml, headers)
    end
    
    case response
    when Net::HTTPSuccess
    else
      raise Atom::Pub::ProtocolError, response
    end
  else
    raise Atom::Pub::NotSupported, "Entry does not have an edit link"
  end
end