diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 4580f9cd3..397d48999 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 c6241931f..61f4697f7 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 061a2094b..69b60fd36 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 0b20b21ab..b6ad08b93 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 04c1846b4..978a5c83e 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 = " + \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 = " + \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 end diff --git a/spec/models/bookmark_spec.rb b/spec/models/bookmark_spec.rb index 4cb8b5fc6..f46b05dea 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 "Reddit" - message.to_xml.to_s.should include "http://reddit.com" + message.to_xml.to_s.should include "http://reddit.com/" end it 'should marshal serialized XML to object' do - xml = "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 7106e16ee..4ccf0ee7c 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)