require 'time' require 'activesupport' module Atomic class Entry attr_accessor :id, :title, :categories, :created_at, :updated_at, :content 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) 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 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] end def to_hash { :id => @id, :title => @title, :categories => @categories, :created_at => @created_at, :updated_at => @updated_at, :content => @content } 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