require 'rubygems' require 'nokogiri' require 'time' module Atomic class Entry class << self def parse(xml) namespaces = { "xmlns:atom" => "http://www.w3.org/2005/Atom", "xmlns:app" => "http://www.w3.org/2007/app", "xmlns:cirrus" => "http://www.glam.ac.uk/2009/cirrus" } doc = Nokogiri.XML(xml) # puts("================================================================") # puts("XML Document") # puts("================================================================") # puts(doc) # puts("================================================================") entry_xml = doc.xpath('//atom:entry', namespaces).first attributes = {} attributes[:id] = entry_xml.xpath('atom:id', namespaces).first.text attributes[:title] = entry_xml.xpath('atom:title', namespaces).first.text attributes[:created_at] = entry_xml.xpath('atom:published', namespaces).first.text attributes[:updated_at] = entry_xml.xpath('atom:updated', namespaces).first.text content_xml = entry_xml.xpath('atom:content', namespaces).first if (content_xml['type'] == 'application/xml') attributes[:content] = {} announcement_xml = content_xml.xpath('cirrus:announcement', namespaces).first unless announcement_xml.nil? attributes[:content][:message] = announcement_xml.xpath('cirrus:message', namespaces).first.text attributes[:content][:starts_at] = announcement_xml.xpath('cirrus:starts-at', namespaces).first.text attributes[:content][:ends_at] = announcement_xml.xpath('cirrus:ends-at', namespaces).first.text end else attributes[:content] = content_xml.inner_html end attributes[:categories] = [] entry_xml.xpath('atom:category', namespaces).each do |category_node| attributes[:categories] << {:term => category_node['term'], :scheme => category_node['scheme']} end new(attributes) 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