require 'rubygems' require 'nokogiri' require 'time' module Atomic class Collection include Parser attr_accessor :href, :title, :content_types, :categories def initialize(params = {}) @href = params[:href] @title = params[:title] @content_types = params[:content_types] || [] @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.content_types << 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 => @href, :title => @title } end end class Workspace include Parser attr_accessor :title, :collections def initialize(params = {}) @title = params[:title] @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'] collection = Collection.new collection.deserialize(reader) self.collections << collection 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 => @title, :collections => @collections.collect{ |collection| collection.to_hash } } end end class Service include Parser attr_accessor :workspaces def initialize(params = {}) @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'] workspace = Workspace.new workspace.deserialize(reader) self.workspaces << workspace 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 => @workspaces.collect { |workspace| workspace.to_hash } } end end end