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

- old
+ new

@@ -3,88 +3,100 @@ module Atomic class Entry - attr_accessor :id, :title, :categories, :created_at, :updated_at, :content + include Parser - class << self + attr_accessor :id, :title, :categories, :created_at, :updated_at, :content, :links, :author, :contributors - def parse(data) - entry = new - doc = data.kind_of?(Nokogiri::XML::Element) ? data : Nokogiri.XML(data, nil, nil, Nokogiri::XML::PARSE_RECOVER + Nokogiri::XML::PARSE_NOBLANKS) - - entry_node = doc.xpath('//atom:entry', NAMESPACES).first - - entry.id = entry_node.xpath('atom:id', NAMESPACES).first.text - entry.title = entry_node.xpath('atom:title', NAMESPACES).first.text - entry.created_at = entry_node.xpath('atom:published', NAMESPACES).first.text - entry.updated_at = entry_node.xpath('atom:updated', NAMESPACES).first.text - - content_node = entry_node.xpath('atom:content', NAMESPACES).first - - entry.content = {:type => content_node['type']} - if (content_node['type'] == 'application/xml') - content_hash = {} - case content_node.children.first.name - when 'announcement' - announcement_node = content_node.xpath('cirrus:announcement', NAMESPACES).first - unless announcement_node.nil? - content_hash[:message] = announcement_node.xpath('cirrus:message', NAMESPACES).first.text - content_hash[:starts_at] = announcement_node.xpath('cirrus:starts-at', NAMESPACES).first.text - content_hash[:ends_at] = announcement_node.xpath('cirrus:ends-at', NAMESPACES).first.text - end - else - throw("Unknown Content Type: #{content_node.children.first.name}") - end - entry.content[:data] = {content_node.children.first.name.to_sym => content_hash} - else - entry.content[:data] = content_node.inner_html - end - entry.categories = [] - entry_node.xpath('atom:category', NAMESPACES).each do |category_node| - entry.categories << {:term => category_node['term'], :scheme => category_node['scheme']} - end - - entry - end - - end - def initialize(params = {}) params.symbolize_keys! params.assert_valid_keys(:title, :id, :categories, :created_at, :updated_at, :content) @title = params[:title] @id = params[:id] @categories = params[:categories] || [] @created_at = params[:created_at].nil? ? Time.now : Time.parse(params[:created_at]) @updated_at = params[:updated_at].nil? ? @created_at : Time.parse(params[:updated_at]) - @content = params[:content] + @content = params[:content] || {} + @links = params[:links] || [] + @author = params[:author] + @contributors = params[:contributors] || [] end + def handle_open_element(node, reader) + progressed = false + case [node.depth, node.uri, node.name] + when [0, NS_ATOM, 'entry'] + when [1, NS_ATOM, 'title'] + when [1, NS_ATOM, 'id'] + when [1, NS_ATOM, 'published'] + when [1, NS_ATOM, 'updated'] + when [1, NS_ATOM, 'category'] + when [1, NS_ATOM, 'author'] + author = Person.new + author.deserialize(reader) + self.author = author + progressed = true + when [1, NS_ATOM, 'contributor'] + contributor = Person.new + contributor.deserialize(reader) + self.contributors << contributor + progressed = true + when [1, NS_ATOM, 'content'] + if node.attributes['type'] == 'application/xml' + @processing_xml_content = true + end + when [1, NS_ATOM, 'link'] + else + if @processing_xml_content + extension_class = Extensions::MAP[[node.uri, node.name]] + unless extension_class.nil? + extension_class.new(self).deserialize(reader) + progressed = true + end + else + puts("Entry ==> Unexpected Open Element - [#{node.depth}] #{node.name} #{node.uri} #{node.attributes.inspect}") + end + end + return progressed + end + + def handle_close_element(node) + case [node.depth, node.uri, node.name] + when [0, NS_ATOM, 'entry'] + when [1, NS_ATOM, 'title'] + @title = node.text + when [1, NS_ATOM, 'id'] + @id = node.text + when [1, NS_ATOM, 'published'] + @created_at = Time.parse(node.text) + when [1, NS_ATOM, 'updated'] + @updated_at = Time.parse(node.text) + when [1, NS_ATOM, 'category'] + @categories << {:scheme => node.attributes['scheme'], :term => node.attributes['term']} + when [1, NS_ATOM, 'author'] + when [1, NS_ATOM, 'content'] + @processing_xml_content = false + when [1, NS_ATOM, 'link'] + @links << {:href => node.attributes['href'], :rel => node.attributes['rel'], :type => node.attributes['type']} + else + puts("Entry ==> Unexpected Close Element - [#{node.depth}] #{node.name} #{node.uri} #{node.attributes.inspect} #{node.text}") unless @processing_xml_content + end + end + def to_hash { :id => @id, :title => @title, :categories => @categories, :created_at => @created_at, :updated_at => @updated_at, - :content => @content + :content => @content, + :links => @links, + :author => @author } end - -# def method_missing(method_symbol, *arguments) -# method_name = method_symbol.to_s -# case method_name[-1..-1] -# when "=" -# @attributes[method_name[0..-2]] = arguments.first -# when "?" -# @attributes[method_name[0..-2]] == true -# else -# # Returns nil on failure so forms will work -# @attributes.has_key?(method_name) ? @attributes[method_name] : nil -# end -# end end end