require 'time' require 'activesupport' module Atomic class Entry include Parser attr_accessor :id, :title, :categories, :created_at, :updated_at, :content, :links, :author, :contributors 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] || {} @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, :links => @links, :author => @author } end end end