Class: Atomic::Atompub::Collection

Inherits:
Object
  • Object
show all
Includes:
Parser
Defined in:
lib/atomic/atompub.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Parser

#deserialize, included

Constructor Details

#initialize(params = {}) ⇒ Collection

Returns a new instance of Collection.



23
24
25
26
27
28
29
30
# File 'lib/atomic/atompub.rb', line 23

def initialize(params = {})
  params.symbolize_keys!
  params.assert_valid_keys(:href, :title, :accepts, :categories)
  self.href = params[:href]
  self.title = params[:title]
  self.accepts = params[:accepts] || []
  self.categories = params[:categories] || []
end

Instance Attribute Details

#acceptsObject

Returns the value of attribute accepts.



21
22
23
# File 'lib/atomic/atompub.rb', line 21

def accepts
  @accepts
end

#categoriesObject

Returns the value of attribute categories.



21
22
23
# File 'lib/atomic/atompub.rb', line 21

def categories
  @categories
end

#hrefObject

Returns the value of attribute href.



21
22
23
# File 'lib/atomic/atompub.rb', line 21

def href
  @href
end

#titleObject

Returns the value of attribute title.



21
22
23
# File 'lib/atomic/atompub.rb', line 21

def title
  @title
end

Instance Method Details

#handle_close_element(node) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/atomic/atompub.rb', line 47

def handle_close_element(node)
  case [node.depth, node.uri, node.name]
  when [0, NS_APP, 'collection']
    self.href = node.attributes['href']
  when [1, NS_ATOM, 'title']
    self.title = node.text
  when [1, NS_APP, 'accept']
    self.accepts << node.text
  when [1, NS_APP, 'categories']
    @processing_categories = false
  when [2, NS_APP, 'category']
    self.categories << {:scheme => node.attributes['scheme'], :term => node.attributes['term']} if @processing_categories
  else
    puts("Collection ==> Unexpected Close Element - [#{node.depth}] #{node.name} {#{node.uri}} #{node.attributes.inspect} #{node.text}")
  end
end

#handle_open_element(node, reader) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/atomic/atompub.rb', line 32

def handle_open_element(node, reader)
  progressed = false
  case [node.depth, node.uri, node.name]
  when [0, NS_APP, 'collection']
  when [1, NS_ATOM, 'title']
  when [1, NS_APP, 'accept']
  when [1, NS_APP, 'categories']
    @processing_categories = true
  when [2, NS_APP, 'category']
  else
    puts("Collection ==> Unexpected Open Element - [#{node.depth}] #{node.name} {#{node.uri}} #{node.attributes.inspect}")
  end
  return progressed
end

#to_hashObject



64
65
66
67
68
69
70
71
# File 'lib/atomic/atompub.rb', line 64

def to_hash
  {
    :href => self.href,
    :title => self.title,
    :accepts => self.accepts,
    :categories => self.categories
  }
end

#to_xml(as_document = true, target = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/atomic/atompub.rb', line 73

def to_xml(as_document=true, target=nil)
  nsdec = as_document ? {:"xmlns:atom" => NS_ATOM, :"xmlns:app" => NS_APP} : {}
  xml = Builder::XmlMarkup.new(:target => target)
  xml.instruct! if as_document
  xml.app(:collection, nsdec.merge({:href => self.href})) do
    xml.atom(:title, self.title)
    self.accepts.each do |accept|
      xml.app(:accept, accept)
    end
    xml.app(:categories) do
      self.categories.each do |category|
        xml.app(:category, category)
      end
    end unless categories.empty?
  end
  xml.target!
end