MS^2 Object parser can handle comments

This commit is contained in:
maxwell 2010-06-25 22:29:08 -07:00
parent a6c33b3e7b
commit 0d700046c4
2 changed files with 34 additions and 18 deletions

View file

@ -22,27 +22,27 @@ module ApplicationHelper
doc.xpath("/XML/posts/post")
end
def parse_posts_from_xml(xml)
posts = []
def parse_objects_from_xml(xml)
objects = []
sender = parse_sender_object_from_xml(xml)
body = parse_body_contents_from_xml(xml)
body.children.each do |post|
begin
object = post.name.camelize.constantize.from_xml post.to_s
object.person = sender
posts << object if object.is_a? Post
object.person = sender if object.is_a? Post
objects << object
rescue
puts "Not a real type: #{post.to_s}"
puts "Not a real type: #{object.to_s}"
end
end
posts
objects
end
def store_posts_from_xml(xml)
posts = parse_posts_from_xml(xml)
def kk(xml)
objects = parse_objects_from_xml(xml)
posts.each do |p|
p.save unless p.person.nil?
objects.each do |p|
p.save if p.respond_to?(:person) && p.person
end
end

View file

@ -12,7 +12,7 @@ describe "parser in application helper" do
status_messages = []
10.times { status_messages << Factory.build(:status_message, :person => @user)}
xml = Post.build_xml_for(status_messages)
store_posts_from_xml(xml)
store_objects_from_xml(xml)
StatusMessage.count.should == 0
end
it 'should discard posts where it does not know the type' do
@ -24,7 +24,7 @@ describe "parser in application helper" do
</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>
</posts></XML>"
store_posts_from_xml(xml)
store_objects_from_xml(xml)
Post.count.should == 2
Post.first.person.email.should == Friend.first.email
end
@ -36,7 +36,7 @@ describe "parser in application helper" do
<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>
</posts></XML>"
store_posts_from_xml(xml)
store_objects_from_xml(xml)
Post.count.should == 0
end
@ -51,7 +51,7 @@ describe "parser in application helper" do
<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>
</posts></XML>"
store_posts_from_xml(xml)
store_objects_from_xml(xml)
Post.count.should == 0
end
it 'should discard types which are not of type post' do
@ -60,12 +60,13 @@ describe "parser in application helper" do
<sender>
<email>#{Friend.first.email}</email>
</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><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>
</posts></XML>"
store_posts_from_xml(xml)
</posts></XML>"
store_objects_from_xml(xml)
Post.count.should == 2
Post.first.person.email.should == Friend.first.email
end
@ -94,10 +95,25 @@ describe "parser in application helper" do
end
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.count.should == 10
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