lib/atomic/service.rb in exempla-atomic-0.0.8 vs lib/atomic/service.rb in exempla-atomic-0.0.11

- old
+ new

@@ -2,81 +2,139 @@ require 'nokogiri' require 'time' module Atomic - class Workspace - class << self - def parse(data) - workspace = new - doc = data.kind_of?(Nokogiri::XML::Element) ? data : Nokogiri.XML(data) - workspace_node = doc.xpath('//app:workspace', NAMESPACES).first - workspace.title = workspace_node.xpath('atom:title', NAMESPACES).first.text - workspace_node.xpath('app:collection', NAMESPACES).each do |collection_node| - workspace.collections << Collection.parse(collection_node) - end - workspace - end - end - attr_accessor :title, :collections + class Collection + + include Parser + attr_accessor :href, :title, :content_types, :categories + def initialize(params = {}) + @href = params[:href] @title = params[:title] - @collections = params[:collections] || [] + @content_types = params[:content_types] || [] + @categories = params[:categories] || [] end - def to_hash - { - :title => @title, - :collections => @collections.collect{ |collection| collection.to_hash } - } + + 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 - end - class Collection - class << self - def parse(data) - collection = new - collection_node = data.kind_of?(Nokogiri::XML::Element) ? data : Nokogiri.XML(data).xpath('//app:collection', NAMESPACES).first -# puts "*********" -# puts collection_node -# puts "*********" - collection.href = collection_node['href'] - collection.title = collection_node.xpath('atom:title', NAMESPACES).first.text - collection + 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 - attr_accessor :href, :title - def initialize(params = {}) - @href = params[:href] - @title = params[:title] - end + def to_hash { :href => @href, :title => @title } end + end - class Service - - class << self + class Workspace - def parse(data) - service = new - doc = data.kind_of?(Nokogiri::XML::Element) ? data : Nokogiri.XML(data) - service_node = doc.xpath('//app:service', NAMESPACES).first - service_node.xpath('app:workspace', NAMESPACES).each do |workspace_node| - service.workspaces << Workspace.parse(workspace_node) - end - service + 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