lib/atomic/entry.rb in exempla-atomic-0.0.3 vs lib/atomic/entry.rb in exempla-atomic-0.0.4
- old
+ new
@@ -6,47 +6,39 @@
class Entry
class << self
- def parse(xml)
+ def parse(data)
+ entry = new
+ doc = data.kind_of?(Nokogiri::XML::Element) ? data : Nokogiri.XML(data)
- doc = Nokogiri.XML(xml)
+ entry_node = doc.xpath('//atom:entry', NAMESPACES).first
-# puts("================================================================")
-# puts("XML Document")
-# puts("================================================================")
-# puts(doc)
-# puts("================================================================")
+ 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
- entry_xml = doc.xpath('//atom:entry', NAMESPACES).first
+ content_node = entry_node.xpath('atom:content', 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
+ if (content_node['type'] == 'application/xml')
+ entry.content = {}
+ announcement_node = content_node.xpath('cirrus:announcement', NAMESPACES).first
+ unless announcement_node.nil?
+ entry.content[:message] = announcement_node.xpath('cirrus:message', NAMESPACES).first.text
+ entry.content[:starts_at] = announcement_node.xpath('cirrus:starts-at', NAMESPACES).first.text
+ entry.content[:ends_at] = announcement_node.xpath('cirrus:ends-at', NAMESPACES).first.text
end
else
- attributes[:content] = content_xml.inner_html
+ entry.content = content_node.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']}
+ entry.categories = []
+ entry_node.xpath('atom:category', NAMESPACES).each do |category_node|
+ entry.categories << {:term => category_node['term'], :scheme => category_node['scheme']}
end
- new(attributes)
+ entry
end
end
attr_accessor :id, :title, :categories, :content, :created_at, :updated_at