require 'rubygems' require 'nokogiri' require 'time' module Atomic class Entry class << self 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) # puts("================================================================") # puts("XML") # puts("================================================================") # puts(doc.inspect) # puts("================================================================") 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 => 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 # puts("================================================================") # puts("Entry") # puts("================================================================") # puts(entry.inspect) # puts("================================================================") entry end end attr_accessor :id, :title, :categories, :content, :created_at, :updated_at def initialize(params = {}) # puts("================================================================") # puts(" Params") # puts("================================================================") # puts(params.inspect) # puts("================================================================") @title = params[:title] @id = params[:id] @categories = params[:categories] @content = params[:content] @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]) end def to_hash { :id => @id, :title => @title, :categories => @categories, :content => @content, :created_at => @created_at, :updated_at => @updated_at } end end end