diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 9ae4b3f94..9791f1b55 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -7,25 +7,42 @@ module ApplicationHelper object.attributes.keys end - def store_posts_from_xml(xml) + def parse_sender_id_from_xml(xml) doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks } - - #i need to check some sort of metadata field - - doc.xpath("/XML/posts/post").each do |post| #this is the post wrapper - post.children.each do|type| #now the text of post itself is the type - #type object to xml is the the thing we want to from_xml - check_and_save_post(type) - end - end + doc.xpath("/XML/head/sender/email").text.to_s + end + + def parse_sender_object_from_xml(xml) + sender_id = parse_sender_id_from_xml(xml) + Person.where(:email => sender_id).first end - def check_and_save_post(type) - begin - object = type.name.camelize.constantize.from_xml type.to_s - object.save if object.is_a? Post - rescue - puts "Not of type post" + def parse_body_contents_from_xml(xml) + doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks } + doc.xpath("/XML/posts/post") + end + + def parse_posts_from_xml(xml) + posts = [] + body = parse_body_contents_from_xml(xml) + body.children.each do |post| + begin + object = post.name.camelize.constantize.from_xml post.to_s + posts << object if object.is_a? Post + rescue + puts "Not a real type: #{post.to_s}" + end + end + posts + end + + def store_posts_from_xml(xml) + sender_object = parse_sender_object_from_xml(xml) + posts = parse_posts_from_xml(xml) + + posts.each do |p| + p.person = sender_object + p.save end end diff --git a/lib/common.rb b/lib/common.rb index 8e34b4591..8da6ddd31 100644 --- a/lib/common.rb +++ b/lib/common.rb @@ -36,11 +36,9 @@ module Diaspora " #{User.first.email} - #{User.first.email} " end - end end end diff --git a/spec/helpers/parser_spec.rb b/spec/helpers/parser_spec.rb index 5d742239f..d1d991a8f 100644 --- a/spec/helpers/parser_spec.rb +++ b/spec/helpers/parser_spec.rb @@ -4,7 +4,8 @@ include ApplicationHelper describe ApplicationHelper do before do - Factory.create(:user) + @user = Factory.create(:user, :email => "bob@aol.com") + @friend =Factory.create(:friend, :email => "bill@gates.com") end it "should store objects sent from xml" do @@ -18,17 +19,25 @@ describe ApplicationHelper do end it 'should discard posts where it does not know the type' do - xml = " - \n Here is another message\n a@a.com\n a@a.com\n a@a.com\n - - \n HEY DUDE\n a@a.com\n a@a.com\n a@a.com\n + xml = " + + + #{User.first.email} + + + \n Here is another message\n a@a.com\n a@a.com\n a@a.com\n \n HEY DUDE\n a@a.com\n a@a.com\n a@a.com\n " store_posts_from_xml(xml) Post.count.should == 2 end it 'should discard types which are not of type post' do - xml = " + xml = " + + + #{User.first.email} + + \n Here is another message\n a@a.com\n a@a.com\n a@a.com\n \n HEY DUDE\n a@a.com\n a@a.com\n a@a.com\n @@ -38,15 +47,35 @@ describe ApplicationHelper do end - describe "parsing a sender" do - it 'should be able to parse the sender of a collection' do - status_messages = [] - 10.times { status_messages << Factory.build(:status_message)} - xml = Post.build_xml_for(status_messages) + describe "parsing compliant XML object" do + before do + status_messages = [] + 10.times { status_messages << Factory.build(:status_message)} + @xml = Post.build_xml_for(status_messages) end - it 'should be able to verify the sender as a friend' do - pending + it 'should be able to parse the sender\'s unique id' do + parse_sender_id_from_xml(@xml).should == @user.email + end + + it 'should be able to retrieve the sender\'s local Person object' do + parse_sender_object_from_xml(@xml).should == @user + end + + it 'should be able to parse the body\'s contents' do + body = parse_body_contents_from_xml(@xml).to_s + body.should_not include "" + body.should_not include "" + body.should_not include "" + body.should_not include "" + body.should include "" + body.should include "" + end + + it 'should be able to extract all posts to an array' do + posts = parse_posts_from_xml(@xml) + posts.is_a?(Array).should be true + posts.count.should == 10 end end diff --git a/spec/lib/common_spec.rb b/spec/lib/common_spec.rb index bc5bdf41d..dc99c3aeb 100644 --- a/spec/lib/common_spec.rb +++ b/spec/lib/common_spec.rb @@ -29,11 +29,7 @@ describe Diaspora do end it "should provide the owner's email" do - @xml.should include "bob@aol.com" - end - - it "should provide the owner's url" do - pending "user does not have url field" + @xml.should include "#{User.first.email}" end end