lib/atomic/entry.rb in exempla-atomic-0.0.6 vs lib/atomic/entry.rb in exempla-atomic-0.0.7

- old
+ new

@@ -1,26 +1,20 @@ -require 'rubygems' -require 'nokogiri' 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) -# 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 @@ -49,48 +43,48 @@ 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("================================================================") - + params.symbolize_keys! + params.assert_valid_keys(:title, :id, :categories, :created_at, :updated_at, :content) @title = params[:title] @id = params[:id] - @categories = params[:categories] - @content = params[:content] + @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, - :content => @content, :created_at => @created_at, - :updated_at => @updated_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