exempla-atomic 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :patch: 2
3
+ :major: 0
4
+ :minor: 0
@@ -0,0 +1,88 @@
1
+ require 'rubygems'
2
+ require 'nokogiri'
3
+ require 'time'
4
+
5
+ module Atomic
6
+
7
+ class Entry
8
+
9
+ class << self
10
+
11
+ def parse(xml)
12
+
13
+ namespaces = { "xmlns:atom" => "http://www.w3.org/2005/Atom",
14
+ "xmlns:app" => "http://www.w3.org/2007/app",
15
+ "xmlns:cirrus" => "http://www.glam.ac.uk/2009/cirrus" }
16
+
17
+ doc = Nokogiri.XML(xml)
18
+
19
+ # puts("================================================================")
20
+ # puts("XML Document")
21
+ # puts("================================================================")
22
+ # puts(doc)
23
+ # puts("================================================================")
24
+
25
+ entry_xml = doc.xpath('//atom:entry', namespaces).first
26
+
27
+ attributes = {}
28
+ attributes[:id] = entry_xml.xpath('atom:id', namespaces).first.text
29
+ attributes[:title] = entry_xml.xpath('atom:title', namespaces).first.text
30
+ attributes[:created_at] = entry_xml.xpath('atom:published', namespaces).first.text
31
+ attributes[:updated_at] = entry_xml.xpath('atom:updated', namespaces).first.text
32
+
33
+ content_xml = entry_xml.xpath('atom:content', namespaces).first
34
+
35
+ if (content_xml['type'] == 'application/xml')
36
+ attributes[:content] = {}
37
+
38
+ announcement_xml = content_xml.xpath('cirrus:announcement', namespaces).first
39
+ unless announcement_xml.nil?
40
+ attributes[:content][:message] = announcement_xml.xpath('cirrus:message', namespaces).first.text
41
+ attributes[:content][:starts_at] = announcement_xml.xpath('cirrus:starts-at', namespaces).first.text
42
+ attributes[:content][:ends_at] = announcement_xml.xpath('cirrus:ends-at', namespaces).first.text
43
+ end
44
+ else
45
+ attributes[:content] = content_xml.inner_html
46
+ end
47
+ attributes[:categories] = []
48
+ entry_xml.xpath('atom:category', namespaces).each do |category_node|
49
+ attributes[:categories] << {:term => category_node['term'], :scheme => category_node['scheme']}
50
+ end
51
+
52
+ new(attributes)
53
+ end
54
+
55
+ end
56
+
57
+ attr_accessor :id, :title, :categories, :content, :created_at, :updated_at
58
+
59
+ def initialize(params = {})
60
+
61
+ # puts("================================================================")
62
+ # puts(" Params")
63
+ # puts("================================================================")
64
+ # puts(params.inspect)
65
+ # puts("================================================================")
66
+
67
+ @title = params[:title]
68
+ @id = params[:id]
69
+ @categories = params[:categories]
70
+ @content = params[:content]
71
+ @created_at = params[:created_at].nil? ? Time.now : Time.parse(params[:created_at])
72
+ @updated_at = params[:updated_at].nil? ? @created_at : Time.parse(params[:updated_at])
73
+ end
74
+
75
+ def to_hash
76
+ {
77
+ :id => @id,
78
+ :title => @title,
79
+ :categories => @categories,
80
+ :content => @content,
81
+ :created_at => @created_at,
82
+ :updated_at => @updated_at
83
+ }
84
+ end
85
+
86
+ end
87
+
88
+ end
data/lib/atomic.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + '/atomic/entry'
@@ -0,0 +1,129 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+ #require 'time'
3
+
4
+ describe Atomic::Entry do
5
+
6
+ def valid_attributes
7
+ {
8
+ :id => "123abc",
9
+ :title => "Atom Entry Title",
10
+ :categories => [
11
+ {:term => 'treforest', :scheme => 'http://cirrusstage.glam.ac.uk/schemes/locations'},
12
+ {:term => 'hesas', :scheme => 'http://cirrusstage.glam.ac.uk/schemes/categories'}
13
+ ],
14
+ :content => {
15
+ :message=>"Test Announcement Message",
16
+ :starts_at=>"2009-02-10T08:00:00Z",
17
+ :ends_at=>"2009-02-10T17:00:00Z"
18
+ }
19
+ }
20
+ end
21
+
22
+ before :each do
23
+ @xml = File.read(File.join(File.dirname(__FILE__), '..', 'fixtures', 'valid_atom_entry.xml'))
24
+ end
25
+
26
+ describe "Class Methods" do
27
+
28
+ describe "#parse" do
29
+
30
+ before :each do
31
+ @entry = Atomic::Entry.parse(@xml)
32
+ end
33
+
34
+ it "should return a new Atomic::Entry" do
35
+ @entry.should be_an(Atomic::Entry)
36
+ end
37
+
38
+ it "should decode a valid Atom Entry xml document" do
39
+ @entry.title.should == 'Test Announcement'
40
+ @entry.id.should == 'tag:example.org,2003:3.2397'
41
+ @entry.categories.size.should == 2
42
+ @entry.content.should_not be_nil
43
+ end
44
+
45
+ end
46
+
47
+ describe "#new" do
48
+
49
+ it "should return a new Atomic::Entry" do
50
+ entry = Atomic::Entry.new(valid_attributes)
51
+ entry.should be_an(Atomic::Entry)
52
+ end
53
+
54
+ it "should set created_at and updated_at to current time if not supplied" do
55
+ now = Time.now
56
+ Time.stub!(:now).and_return(now)
57
+ entry = Atomic::Entry.new(valid_attributes)
58
+ entry.created_at.should be_a(Time)
59
+ entry.created_at.should == now
60
+ entry.updated_at.should be_a(Time)
61
+ entry.updated_at.should == now
62
+ end
63
+
64
+ it "should set created_at to supplied time" do
65
+ now = Time.now
66
+ Time.stub!(:now).and_return(now)
67
+ entry = Atomic::Entry.new(valid_attributes.merge(:created_at => '2009-02-10T08:00:00Z'))
68
+ entry.created_at.should == Time.parse('2009-02-10T08:00:00Z')
69
+ entry.created_at.should be_a(Time)
70
+ end
71
+
72
+ it "should set updated_at to supplied time" do
73
+ now = Time.now
74
+ Time.stub!(:now).and_return(now)
75
+ entry = Atomic::Entry.new(valid_attributes.merge(:updated_at => '2009-02-10T08:00:00Z'))
76
+ entry.updated_at.should == Time.parse('2009-02-10T08:00:00Z')
77
+ entry.updated_at.should be_a(Time)
78
+ end
79
+
80
+ end
81
+
82
+ end
83
+
84
+ describe "#to_hash" do
85
+
86
+ before :each do
87
+ @now = Time.now
88
+ Time.stub!(:now).and_return(@now)
89
+ @entry = Atomic::Entry.new(valid_attributes)
90
+ end
91
+
92
+ it "should return a hash" do
93
+ @entry.to_hash.should be_a(Hash)
94
+ end
95
+
96
+ it "should include the id" do
97
+ @entry.to_hash.should have_key(:id)
98
+ @entry.to_hash[:id].should == valid_attributes[:id]
99
+ end
100
+
101
+ it "should include the title" do
102
+ @entry.to_hash.should have_key(:title)
103
+ @entry.to_hash[:title].should == valid_attributes[:title]
104
+ end
105
+
106
+ it "should include the categories" do
107
+ @entry.to_hash.should have_key(:categories)
108
+ @entry.to_hash[:categories].should == valid_attributes[:categories]
109
+ end
110
+
111
+ it "should include the content" do
112
+ @entry.to_hash.should have_key(:content)
113
+ @entry.to_hash[:content].should == valid_attributes[:content]
114
+ end
115
+
116
+ it "should include the created_at time" do
117
+ @entry.to_hash.should have_key(:created_at)
118
+ @entry.to_hash[:created_at].should == @now
119
+ end
120
+
121
+ it "should include the updated_at time" do
122
+ @entry.to_hash.should have_key(:updated_at)
123
+ @entry.to_hash[:updated_at].should == @now
124
+ end
125
+
126
+ end
127
+
128
+ end
129
+
@@ -0,0 +1,21 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <entry xmlns="http://www.w3.org/2005/Atom" xmlns:cirrus="http://www.glam.ac.uk/2009/cirrus">
3
+ <title>Test Announcement</title>
4
+ <id>tag:example.org,2003:3.2397</id>
5
+ <updated>2009-02-10T08:00:00Z</updated>
6
+ <published>2009-02-10T08:00:00Z</published>
7
+ <author>
8
+ <name>Peter Portal</name>
9
+ <uri>http://www.glam.ac.uk/</uri>
10
+ <email>peterportal@glam.ac.uk</email>
11
+ </author>
12
+ <category term="trefforest" scheme="http://cirrusstage.glam.ac.uk/schemes/locations"/>
13
+ <category term="gbs" scheme="http://cirrusstage.glam.ac.uk/schemes/faculties"/>
14
+ <content type="application/xml">
15
+ <cirrus:announcement>
16
+ <cirrus:message>Test Announcement Message</cirrus:message>
17
+ <cirrus:starts-at type="datetime">2009-02-10T08:00:00Z</cirrus:starts-at>
18
+ <cirrus:ends-at type="datetime">2009-02-10T17:00:00Z</cirrus:ends-at>
19
+ </cirrus:announcement>
20
+ </content>
21
+ </entry>
data/spec/spec.opts ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format specdoc
3
+ --loadby mtime
@@ -0,0 +1,9 @@
1
+ require 'spec'
2
+
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+
5
+ require 'atomic'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: exempla-atomic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Darrin Wortlehock
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-11 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: TODO
17
+ email: darrin@exempla.co.uk
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - VERSION.yml
26
+ - lib/atomic.rb
27
+ - lib/atomic
28
+ - lib/atomic/entry.rb
29
+ - spec/spec.opts
30
+ - spec/fixtures
31
+ - spec/fixtures/valid_atom_entry.xml
32
+ - spec/spec_helper.rb
33
+ - spec/atomic
34
+ - spec/atomic/entry_spec.rb
35
+ has_rdoc: true
36
+ homepage: http://github.com/exempla/atomic
37
+ post_install_message:
38
+ rdoc_options:
39
+ - --inline-source
40
+ - --charset=UTF-8
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.2.0
59
+ signing_key:
60
+ specification_version: 2
61
+ summary: A simple Atom and AtomPub parsing library
62
+ test_files: []
63
+