begin require 'builder' rescue LoadError require 'rubygems' require 'builder' end begin require 'activesupport' rescue LoadError require 'rubygems' require 'activesupport' end module Atomic module Atompub class Collection include Parser attr_accessor :href, :title, :accepts, :categories 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 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 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 def to_hash { :href => self.href, :title => self.title, :accepts => self.accepts, :categories => self.categories } end 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 end class Workspace include Parser attr_accessor :title, :collections def initialize(params = {}) params.symbolize_keys! params.assert_valid_keys(:title, :collections) self.title = params[:title] self.collections = params[:collections] || [] end def handle_open_element(node, reader) progressed = false case [node.depth, node.uri, node.name] when [0, NS_APP, 'workspace'] when [1, NS_ATOM, 'title'] when [1, NS_APP, 'collection'] self.collections << Collection.new.deserialize(reader) progressed = true else puts("Workspace ==> Unexpected Open Element - [#{node.depth}] #{node.name} {#{node.uri}} #{node.attributes.inspect}") end return progressed end def handle_close_element(node) case [node.depth, node.uri, node.name] when [0, NS_APP, 'workspace'] when [1, NS_ATOM, 'title'] self.title = node.text when [1, NS_APP, 'collection'] else puts("Workspace ==> Unexpected Close Element - [#{node.depth}] #{node.name} {#{node.uri}} #{node.attributes.inspect} #{node.text}") end end def to_hash { :title => self.title, :collections => self.collections.collect{ |collection| collection.to_hash } } end 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(:workspace, nsdec) do xml.atom(:title, self.title) self.collections.each do |collection| collection.to_xml(false, xml) end end xml.target! end end class Service include Parser attr_accessor :workspaces def initialize(params = {}) params.symbolize_keys! params.assert_valid_keys(:workspaces) self.workspaces = params[:workspaces] || [] end def handle_open_element(node, reader) progressed = false case [node.depth, node.uri, node.name] when [0, NS_APP, 'service'] when [1, NS_APP, 'workspace'] self.workspaces << Workspace.new.deserialize(reader) progressed = true else puts("Service ==> Unexpected Open Element - [#{node.depth}] #{node.name} {#{node.uri}} #{node.attributes.inspect}") end return progressed end def handle_close_element(node) case [node.depth, node.uri, node.name] when [0, NS_APP, 'service'] when [1, NS_APP, 'workspace'] else puts("Service ==> Unexpected Close Element - [#{node.depth}] #{node.name} {#{node.uri}} #{node.attributes.inspect} #{node.text}") end end def to_hash { :workspaces => self.workspaces.collect { |workspace| workspace.to_hash } } end 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(:service, nsdec) do self.workspaces.each do |workspace| workspace.to_xml(false, xml) end end xml.target! end end end end