Class: Ape::Categories

Inherits:
Object
  • Object
show all
Defined in:
lib/ape/categories.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#fixedObject (readonly)

Returns the value of attribute fixed.



10
11
12
# File 'lib/ape/categories.rb', line 10

def fixed
  @fixed
end

Class Method Details

.add_cats(entry, collection, authent, reporter = nil) ⇒ Object

Decorate an entry which is about to be posted to a collection with some

categories.  For each fixed categories element, pick one of the categories
and add that.  If there are no categories elements at all, or if there's
at least one with fixed="no", also add a syntho-cat that we make up.
Return the list of categories that we added.


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ape/categories.rb', line 50

def Categories.add_cats(entry, collection, authent, reporter = nil)

  added = []
  c = from_collection(collection, authent)
  if c.empty?
    add_syntho = true
  else
    add_syntho = false

    # for each <app:categories>
    c.each do |cats|
      
      default_scheme = cats.attributes['scheme']

      # if it's fixed, pick the first one
      if cats.attributes['fixed'] == "yes"
        cat_list = REXML::XPath.match(cats, './atom:category', Names::XmlNamespaces)
        if cat_list

          # for each <app:category> take the first one whose attribute "term" is not empty
          cat_list.each do |cat|
            if cat.attributes['term'].empty?
              reporter.warning(self, 'A mangled category is present in your categories list') if reporter
            else
              scheme = cat.attributes['scheme']
              if !scheme
                scheme = default_scheme
              end              
              added << entry.add_category(cat.attributes['term'], scheme)
              break
            end
          end
        end
      else
        add_syntho = true
      end

    end
  end

  if add_syntho
    added << entry.add_category('simians', 'http://tbray.org/cat-test')
  end
  added
end

.from_collection(collection, authent, reporter = nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ape/categories.rb', line 12

def Categories.from_collection(collection, authent, reporter = nil)

  # "catses" because if cats is short for categories, then catses 
  #  suggests multiple <app:categories> elements
  catses = collection.catses

  catses.collect! do |cats|
    if cats.attribute(:href)
      getter = Getter.new(cats.attribute(:href).value, authent)
      if getter.last_error # wonky URI
        reporter.error(self, getter.last_error) if reporter
        nil
      end

      if !getter.get('application/atomcat+xml')
        reporter.error(self, "Can't fetch categories doc " + 
          "'#{cats.attribute(:href).value}': getter.last_error") if reporter
        nil
      end

      reporter.warning(self, getter.last_error) if reporter && getter.last_error
      REXML::Document.new(getter.body).root
    else
      # no href attribute
      cats
    end
  end
  catses.compact
end