MS^2 Object parser can handle comments
This commit is contained in:
parent
a6c33b3e7b
commit
0d700046c4
2 changed files with 34 additions and 18 deletions
|
|
@ -22,27 +22,27 @@ module ApplicationHelper
|
||||||
doc.xpath("/XML/posts/post")
|
doc.xpath("/XML/posts/post")
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_posts_from_xml(xml)
|
def parse_objects_from_xml(xml)
|
||||||
posts = []
|
objects = []
|
||||||
sender = parse_sender_object_from_xml(xml)
|
sender = parse_sender_object_from_xml(xml)
|
||||||
body = parse_body_contents_from_xml(xml)
|
body = parse_body_contents_from_xml(xml)
|
||||||
body.children.each do |post|
|
body.children.each do |post|
|
||||||
begin
|
begin
|
||||||
object = post.name.camelize.constantize.from_xml post.to_s
|
object = post.name.camelize.constantize.from_xml post.to_s
|
||||||
object.person = sender
|
object.person = sender if object.is_a? Post
|
||||||
posts << object if object.is_a? Post
|
objects << object
|
||||||
rescue
|
rescue
|
||||||
puts "Not a real type: #{post.to_s}"
|
puts "Not a real type: #{object.to_s}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
posts
|
objects
|
||||||
end
|
end
|
||||||
|
|
||||||
def store_posts_from_xml(xml)
|
def kk(xml)
|
||||||
posts = parse_posts_from_xml(xml)
|
objects = parse_objects_from_xml(xml)
|
||||||
|
|
||||||
posts.each do |p|
|
objects.each do |p|
|
||||||
p.save unless p.person.nil?
|
p.save if p.respond_to?(:person) && p.person
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ describe "parser in application helper" do
|
||||||
status_messages = []
|
status_messages = []
|
||||||
10.times { status_messages << Factory.build(:status_message, :person => @user)}
|
10.times { status_messages << Factory.build(:status_message, :person => @user)}
|
||||||
xml = Post.build_xml_for(status_messages)
|
xml = Post.build_xml_for(status_messages)
|
||||||
store_posts_from_xml(xml)
|
store_objects_from_xml(xml)
|
||||||
StatusMessage.count.should == 0
|
StatusMessage.count.should == 0
|
||||||
end
|
end
|
||||||
it 'should discard posts where it does not know the type' do
|
it 'should discard posts where it does not know the type' do
|
||||||
|
|
@ -24,7 +24,7 @@ describe "parser in application helper" do
|
||||||
</head><posts>
|
</head><posts>
|
||||||
<post><status_message>\n <message>Here is another message</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post> <post><not_a_real_type></not_a_real_type></post> <post><status_message>\n <message>HEY DUDE</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post>
|
<post><status_message>\n <message>Here is another message</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post> <post><not_a_real_type></not_a_real_type></post> <post><status_message>\n <message>HEY DUDE</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post>
|
||||||
</posts></XML>"
|
</posts></XML>"
|
||||||
store_posts_from_xml(xml)
|
store_objects_from_xml(xml)
|
||||||
Post.count.should == 2
|
Post.count.should == 2
|
||||||
Post.first.person.email.should == Friend.first.email
|
Post.first.person.email.should == Friend.first.email
|
||||||
end
|
end
|
||||||
|
|
@ -36,7 +36,7 @@ describe "parser in application helper" do
|
||||||
<post><friend></friend></post>
|
<post><friend></friend></post>
|
||||||
<post><status_message>\n <message>HEY DUDE</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post>
|
<post><status_message>\n <message>HEY DUDE</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post>
|
||||||
</posts></XML>"
|
</posts></XML>"
|
||||||
store_posts_from_xml(xml)
|
store_objects_from_xml(xml)
|
||||||
Post.count.should == 0
|
Post.count.should == 0
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
@ -51,7 +51,7 @@ describe "parser in application helper" do
|
||||||
<post><friend></friend></post>
|
<post><friend></friend></post>
|
||||||
<post><status_message>\n <message>HEY DUDE</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post>
|
<post><status_message>\n <message>HEY DUDE</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post>
|
||||||
</posts></XML>"
|
</posts></XML>"
|
||||||
store_posts_from_xml(xml)
|
store_objects_from_xml(xml)
|
||||||
Post.count.should == 0
|
Post.count.should == 0
|
||||||
end
|
end
|
||||||
it 'should discard types which are not of type post' do
|
it 'should discard types which are not of type post' do
|
||||||
|
|
@ -60,12 +60,13 @@ describe "parser in application helper" do
|
||||||
<sender>
|
<sender>
|
||||||
<email>#{Friend.first.email}</email>
|
<email>#{Friend.first.email}</email>
|
||||||
</sender>
|
</sender>
|
||||||
</head><posts>
|
</head>
|
||||||
|
<posts>
|
||||||
<post><status_message>\n <message>Here is another message</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post>
|
<post><status_message>\n <message>Here is another message</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post>
|
||||||
<post><friend></friend></post>
|
<post><friend></friend></post>
|
||||||
<post><status_message>\n <message>HEY DUDE</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post>
|
<post><status_message>\n <message>HEY DUDE</message>\n <owner>a@a.com</owner>\n <snippet>a@a.com</snippet>\n <source>a@a.com</source>\n</status_message></post>
|
||||||
</posts></XML>"
|
</posts></XML>"
|
||||||
store_posts_from_xml(xml)
|
store_objects_from_xml(xml)
|
||||||
Post.count.should == 2
|
Post.count.should == 2
|
||||||
Post.first.person.email.should == Friend.first.email
|
Post.first.person.email.should == Friend.first.email
|
||||||
end
|
end
|
||||||
|
|
@ -94,10 +95,25 @@ describe "parser in application helper" do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should be able to extract all posts to an array' do
|
it 'should be able to extract all posts to an array' do
|
||||||
posts = parse_posts_from_xml(@xml)
|
posts = parse_objects_from_xml(@xml)
|
||||||
posts.is_a?(Array).should be true
|
posts.is_a?(Array).should be true
|
||||||
posts.count.should == 10
|
posts.count.should == 10
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'should be able to correctly handle comments' do
|
||||||
|
friend = Factory.create(:friend)
|
||||||
|
post = Factory.create(:status_message)
|
||||||
|
xml = "<XML><head><sender><email>#{Friend.first.email}</email></sender></head>
|
||||||
|
<posts>
|
||||||
|
<post><comment>\n <text>Freedom!</text>\n <person>#{friend.id}</person>\n <post_id>#{post.id}}</post_id>\n</comment></post>
|
||||||
|
</posts></XML>"
|
||||||
|
objects = parse_objects_from_xml(xml)
|
||||||
|
comment = objects.first
|
||||||
|
comment.text.should == "Freedom!"
|
||||||
|
comment.person.should == friend
|
||||||
|
comment.post.should == post
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue