Class: Atom::Entry

Inherits:
Element show all
Defined in:
lib/atom/entry.rb,
lib/atom/yaml.rb

Overview

An individual entry in a feed. As an Atom::Element, it can be manipulated using accessors for each of its child elements. You should be able to set them using an instance of any class that makes sense

Entries have the following children:

id

a universally unique IRI which permanently identifies the entry

title

a human-readable title (Atom::Text)

content

contains or links to the content of an entry (Atom::Content)

rights

information about rights held in and over an entry (Atom::Text)

source

the source feed’s metadata (unimplemented)

published

a Time “early in the life cycle of an entry”

updated

the most recent Time an entry was modified in a way the publisher considers significant

summary

a summary, abstract or excerpt of an entry (Atom::Text)

There are also categories, links, authors and contributors, each of which is an Array of its respective type and can be used thusly:

author = entry.authors.new
author.name = "Captain Kangaroo"

Instance Attribute Summary

Attributes inherited from Element

#base, #extensions

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Element

#[], #[]=, attrb, attrs, define_accessor, element, elements, inherited, #local_name, required, #taguri, #to_element, #to_s, #to_xml, #to_yaml_properties

Constructor Details

#initialize {|_self| ... } ⇒ Entry

:nodoc:

Yields:

  • (_self)

Yield Parameters:

  • _self (Atom::Entry)

    the object that the method was called on



52
53
54
55
56
57
# File 'lib/atom/entry.rb', line 52

def initialize # :nodoc:
  super "entry"

  # XXX I don't think I've ever actually used this
  yield self if block_given?
end

Class Method Details

.from_yaml(yaml) ⇒ Object

parses an Atom::Entry from YAML



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/atom/yaml.rb', line 78

def self.from_yaml yaml
  hash = if yaml.kind_of?(Hash); yaml else YAML.load(yaml); end

  entry = Atom::Entry.new

  ["id", "published", "updated", "title", "summary", "draft"].each do |name|
    entry.send("#{name}=".to_sym, hash[name])
  end

  elem_constructs = {"authors" => entry.authors,
                     "contributors" => entry.contributors,
                     "links" => entry.links,
                     "categories" => entry.categories}

  elem_constructs.each do |type,ary|
    hash[type] ||= []

    hash[type].each do |yelem|
      elem = ary.new

      elem.class.attrs.each do |attrb,req|
        elem[attrb.to_s] = yelem[attrb.to_s]
      end

      elem.class.elements.each do |name,kind,req|
        elem.send("#{name}=".to_sym, yelem[name.to_s])
      end
    end
  end

  entry.tag_with hash["tags"]

  entry.content = hash["content"]
  entry.content["type"] = hash["type"] if hash["type"]

  entry
end

.parse(xml, base = "") ⇒ Object

parses XML into an Atom::Entry

base is the absolute URI the document was fetched from (if there is one)



63
64
65
66
67
68
69
70
71
# File 'lib/atom/entry.rb', line 63

def self.parse xml, base = ""
  if xml.respond_to? :to_atom_entry
    xml.to_atom_entry(base)
  elsif xml.respond_to? :read
    self.parse(xml.read)
  else
    REXML::Document.new(xml.to_s).to_atom_entry(base)
  end
end

Instance Method Details

#draftObject



107
108
109
110
111
# File 'lib/atom/entry.rb', line 107

def draft
  elem = REXML::XPath.first(extensions, "app:control/app:draft", {"app" => PP_NS})

  elem and elem.text == "yes"
end

#draft=(is_draft) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/atom/entry.rb', line 113

def draft= is_draft
  nses = {"app" => PP_NS}
  draft_e = REXML::XPath.first(extensions, "app:control/app:draft", nses)
  control_e = REXML::XPath.first(extensions, "app:control", nses)

  if is_draft and not draft
    unless draft_e
      unless control_e
        control_e = REXML::Element.new("control")
        control_e.add_namespace PP_NS

        extensions << control_e
      end

      draft_e = REXML::Element.new("draft")
      control_e << draft_e
    end

    draft_e.text = "yes"
  elsif not is_draft and draft
    draft_e.remove
    control_e.remove if control_e.elements.empty?
  end

  is_draft
end

#edit_urlObject

the @href of an entry’s link



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/atom/entry.rb', line 95

def edit_url
  begin
    edit_link = self.links.find do |link|
      link["rel"] == "edit"
    end

    edit_link["href"]
  rescue
    nil
  end
end

#inspectObject

:nodoc:



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

def inspect # :nodoc:
  "#<Atom::Entry id:'#{self.id}'>"
end

#tag_with(tags) ⇒ Object

categorize the entry with each of an array or a space-separated

string


86
87
88
89
90
91
92
# File 'lib/atom/entry.rb', line 86

def tag_with tags
  return unless tags

  (tags.is_a?(String) ? tags.split : tags).each do |tag|
    categories.new["term"] = tag
  end
end

#to_yaml(opts = {}) ⇒ Object

:nodoc:



66
67
68
69
70
71
72
73
74
75
# File 'lib/atom/yaml.rb', line 66

def to_yaml( opts = {} ) # :nodoc:
  YAML::quick_emit( object_id, opts ) do |out|
    out.map( taguri, to_yaml_style ) do |map|
      self.to_yaml_properties.each do |m|
        map.add( m[1..-1], instance_variable_get( m ) )
      end
      map.add("draft", true) if self.draft
    end
  end
end

#to_yaml_typeObject

:nodoc:



62
63
64
# File 'lib/atom/yaml.rb', line 62

def to_yaml_type # :nodoc:
  '!necronomicorp.com,2006/entry'
end

#updated!Object

declare that this entry has updated.

(note that this is different from Atom::Feed#update!)



80
81
82
# File 'lib/atom/entry.rb', line 80

def updated!
  self.updated = Time.now
end