From 34df8fa946932e443ef45d1083c006ce4e7af0d5 Mon Sep 17 00:00:00 2001 From: maxwell <maxwell@joindiaspora.com> Date: Fri, 18 Jun 2010 16:47:34 -0700 Subject: [PATCH] DG MS; resolved unknown types going to receive handler. only post objects are inputted --- app/helpers/application_helper.rb | 15 +++++- app/models/bookmark.rb | 13 +++++ app/models/friend.rb | 14 +++++- spec/controllers/dashboard_controller_spec.rb | 1 + spec/helpers/parser_spec.rb | 19 +++++++ spec/models/bookmark_spec.rb | 50 +++++++++++++++++-- spec/models/friend_spec.rb | 48 +++++++++++++++++- 7 files changed, 153 insertions(+), 7 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 4580f9cd30..397d489997 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -9,12 +9,23 @@ module ApplicationHelper def store_posts_from_xml(xml) doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks } + + #i need to check some sort of metadata field + doc.xpath("//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 - object = type.name.camelize.constantize.from_xml type.to_s - object.save + check_and_save_post(type) end end 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" + end + end end diff --git a/app/models/bookmark.rb b/app/models/bookmark.rb index c6241931fc..61f4697f7f 100644 --- a/app/models/bookmark.rb +++ b/app/models/bookmark.rb @@ -9,4 +9,17 @@ class Bookmark < Post validates_presence_of :link + validates_format_of :link, :with => + /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix + + before_validation :clean_link + + protected + + def clean_link + if self.link + self.link = 'http://' + self.link unless self.link.match('http://' || 'https://') + self.link = self.link + '/' if self.link[-1,1] != '/' + end + end end diff --git a/app/models/friend.rb b/app/models/friend.rb index 061a2094b8..69b60fd361 100644 --- a/app/models/friend.rb +++ b/app/models/friend.rb @@ -9,5 +9,17 @@ class Friend field :url validates_presence_of :username, :url - + validates_format_of :url, :with => + /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix + + before_validation :clean_url + + protected + + def clean_url + if self.url + self.url = 'http://' + self.url unless self.url.match('http://' || 'https://') + self.url = self.url + '/' if self.url[-1,1] != '/' + end + end end diff --git a/spec/controllers/dashboard_controller_spec.rb b/spec/controllers/dashboard_controller_spec.rb index 0b20b21abf..b6ad08b93c 100644 --- a/spec/controllers/dashboard_controller_spec.rb +++ b/spec/controllers/dashboard_controller_spec.rb @@ -8,4 +8,5 @@ describe DashboardController do get :index response.should render_template(:index) end + end diff --git a/spec/helpers/parser_spec.rb b/spec/helpers/parser_spec.rb index 04c1846b4d..978a5c83e1 100644 --- a/spec/helpers/parser_spec.rb +++ b/spec/helpers/parser_spec.rb @@ -17,5 +17,24 @@ describe DashboardHelper do StatusMessage.count.should == 10 end + it 'should discard posts where it does not know the type' do + xml = "<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>" + store_posts_from_xml(xml) + Post.count.should == 2 + end + + it 'should discard types which are not of type post' do + xml = "<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>" + store_posts_from_xml(xml) + Post.count.should == 2 + end end diff --git a/spec/models/bookmark_spec.rb b/spec/models/bookmark_spec.rb index 4cb8b5fc62..f46b05deae 100644 --- a/spec/models/bookmark_spec.rb +++ b/spec/models/bookmark_spec.rb @@ -14,19 +14,63 @@ describe Bookmark do n.owner.should == "bob@aol.com" end + it 'should validate its link' do + bookmark = Factory.build(:bookmark) + + #links changed valid + bookmark.link = "google.com" + bookmark.valid?.should == true + bookmark.link.should == "http://google.com/" + + bookmark.link = "www.google.com" + bookmark.valid?.should == true + bookmark.link.should == "http://www.google.com/" + + bookmark.link = "google.com/" + bookmark.valid?.should == true + bookmark.link.should == "http://google.com/" + + bookmark.link = "www.google.com/" + bookmark.valid?.should == true + bookmark.link.should == "http://www.google.com/" + + bookmark.link = "http://google.com" + bookmark.valid?.should == true + bookmark.link.should == "http://google.com/" + + bookmark.link = "http://www.google.com" + bookmark.valid?.should == true + + #invalid links + bookmark.link = "zsdvzxdg" + bookmark.valid?.should == false + bookmark.link = "sdfasfa.c" + bookmark.valid?.should == false + bookmark.link = "http://.com/" + bookmark.valid?.should == false + bookmark.link = "http://www..com/" + bookmark.valid?.should == false + bookmark.link = "http:/www.asodij.com/" + bookmark.valid?.should == false + bookmark.link = "https:/www.asodij.com/" + bookmark.valid?.should == false + bookmark.link = "http:///www.asodij.com/" + bookmark.valid?.should == false + end + describe "XML" do it 'should serialize to XML' do Factory.create(:user) message = Factory.create(:bookmark, :title => "Reddit", :link => "http://reddit.com") message.to_xml.to_s.should include "<title>Reddit</title>" - message.to_xml.to_s.should include "<link>http://reddit.com</link>" + message.to_xml.to_s.should include "<link>http://reddit.com/</link>" end it 'should marshal serialized XML to object' do - xml = "<bookmark><title>Reddit</message><link>http://reddit.com</link><owner>bob@aol.com</owner></bookmark>" + xml = "<bookmark><title>Reddit</message><link>http://reddit.com/</link><owner>bob@aol.com</owner></bookmark>" parsed = Bookmark.from_xml(xml) parsed.title.should == "Reddit" - parsed.link.should == "http://reddit.com" + parsed.link.should == "http://reddit.com/" parsed.owner.should == "bob@aol.com" parsed.valid?.should be_true end diff --git a/spec/models/friend_spec.rb b/spec/models/friend_spec.rb index 7106e16eec..4ccf0ee7c4 100644 --- a/spec/models/friend_spec.rb +++ b/spec/models/friend_spec.rb @@ -1,13 +1,59 @@ require File.dirname(__FILE__) + '/../spec_helper' describe Friend do + it 'should have a diaspora username and diaspora url' do - n = Factory.build(:friend, :url => nil) + n = Factory.build(:friend, :url => "") n.valid?.should be false n.url = "http://max.com/" n.valid?.should be true end + + it 'should validate its url' do + friend = Factory.build(:friend) + + #urls changed valid + friend.url = "google.com" + friend.valid?.should == true + friend.url.should == "http://google.com/" + + friend.url = "www.google.com" + friend.valid?.should == true + friend.url.should == "http://www.google.com/" + + friend.url = "google.com/" + friend.valid?.should == true + friend.url.should == "http://google.com/" + + friend.url = "www.google.com/" + friend.valid?.should == true + friend.url.should == "http://www.google.com/" + + friend.url = "http://google.com" + friend.valid?.should == true + friend.url.should == "http://google.com/" + + friend.url = "http://www.google.com" + friend.valid?.should == true + + #invalid urls + friend.url = "zsdvzxdg" + friend.valid?.should == false + friend.url = "sdfasfa.c" + friend.valid?.should == false + friend.url = "http://.com/" + friend.valid?.should == false + friend.url = "http://www..com/" + friend.valid?.should == false + friend.url = "http:/www.asodij.com/" + friend.valid?.should == false + friend.url = "https:/www.asodij.com/" + friend.valid?.should == false + friend.url = "http:///www.asodij.com/" + friend.valid?.should == false + end + describe "XML" do before do @f = Factory.build(:friend) -- GitLab