DG MS; resolved unknown types going to receive handler. only post objects are inputted

This commit is contained in:
maxwell 2010-06-18 16:47:34 -07:00
parent c430aaff18
commit 34df8fa946
7 changed files with 153 additions and 7 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -8,4 +8,5 @@ describe DashboardController do
get :index
response.should render_template(:index)
end
end

View file

@ -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

View file

@ -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

View file

@ -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)