Merge branch 'master' of github.com:diaspora/diaspora_rails

This commit is contained in:
maxwell 2010-06-23 01:26:06 -07:00
commit 663b99924f
16 changed files with 125 additions and 65 deletions

View file

@ -7,25 +7,42 @@ module ApplicationHelper
object.attributes.keys object.attributes.keys
end end
def store_posts_from_xml(xml) def parse_sender_id_from_xml(xml)
doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks } doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks }
doc.xpath("/XML/head/sender/email").text.to_s
#i need to check some sort of metadata field end
doc.xpath("/XML/posts/post").each do |post| #this is the post wrapper def parse_sender_object_from_xml(xml)
post.children.each do|type| #now the text of post itself is the type sender_id = parse_sender_id_from_xml(xml)
#type object to xml is the the thing we want to from_xml Person.where(:email => sender_id).first
check_and_save_post(type)
end
end
end end
def check_and_save_post(type) def parse_body_contents_from_xml(xml)
begin doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks }
object = type.name.camelize.constantize.from_xml type.to_s doc.xpath("/XML/posts/post")
object.save if object.is_a? Post end
rescue
puts "Not of type post" 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
end end

View file

@ -1,16 +1,10 @@
class Friend class Friend < Person
include Mongoid::Document
include ROXML
xml_accessor :username
xml_accessor :url xml_accessor :url
xml_accessor :real_name
field :username
field :url field :url
field :real_name
validates_presence_of :username, :url, :real_name validates_presence_of :url
validates_format_of :url, :with => validates_format_of :url, :with =>
/^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix

15
app/models/person.rb Normal file
View file

@ -0,0 +1,15 @@
class Person
include Mongoid::Document
include ROXML
xml_accessor :email
xml_accessor :real_name
field :email
field :real_name
has_many_related :posts
validates_presence_of :email, :real_name
end

View file

@ -17,6 +17,10 @@ class Post
field :source field :source
field :snippet field :snippet
belongs_to_related :person
before_create :set_defaults before_create :set_defaults
after_save :send_to_view after_save :send_to_view
@ -50,6 +54,7 @@ class Post
self.owner ||= user_email self.owner ||= user_email
self.source ||= user_email self.source ||= user_email
self.snippet ||= user_email self.snippet ||= user_email
self.person ||= User.first
end end
end end

View file

@ -1,13 +1,8 @@
class User class User < Person
include Mongoid::Document
# Include default devise modules. Others available are: # Include default devise modules. Others available are:
# :token_authenticatable, :confirmable, :lockable and :timeoutable # :token_authenticatable, :confirmable, :lockable and :timeoutable
devise :database_authenticatable, :registerable, devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable :recoverable, :rememberable, :trackable, :validatable
field :real_name
validates_presence_of :real_name
end end

View file

@ -2,11 +2,13 @@
%table %table
%tr %tr
%th username %th real name
%th email
%th url %th url
- for friend in @friends - for friend in @friends
%tr %tr
%td= friend.username %td= friend.real_name
%td= friend.email
%td= friend.url %td= friend.url
%td= link_to 'Show', friend %td= link_to 'Show', friend
%td= link_to 'Destroy', friend, :confirm => 'Are you sure?', :method => :delete %td= link_to 'Destroy', friend, :confirm => 'Are you sure?', :method => :delete

View file

@ -3,9 +3,13 @@
= form_for @friend do |f| = form_for @friend do |f|
= f.error_messages = f.error_messages
%p %p
= f.label :username = f.label :real_name
%br %br
= f.text_field :username = f.text_field :real_name
%p
= f.label :email
%br
= f.text_field :email
%p %p
= f.label :url = f.label :url
%br %br

View file

@ -1,8 +1,11 @@
- title "Friend" - title "Friend"
%p %p
%strong Username: %strong Real Name:
= @friend.username = @friend.real_name
%p
%strong Email:
= @friend.email
%p %p
%strong Url: %strong Url:
= @friend.url = @friend.url

View file

@ -1,6 +1,6 @@
%li.message{:class => ("mine" if mine?(post))} %li.message{:class => ("mine" if mine?(post))}
%span.from %span.from
= link_to post.owner, "#" = link_to post.person.real_name, "#"
= post.message = post.message
%div.time %div.time
= "#{time_ago_in_words(post.updated_at)} ago" = "#{time_ago_in_words(post.updated_at)} ago"

View file

@ -2,9 +2,11 @@
%table %table
%tr %tr
%th User %th Real Name
%th email
%th Password %th Password
- for user in @users - for user in @users
%tr %tr
%td= user.real_name
%td= user.email %td= user.email
%td= user.encrypted_password %td= user.encrypted_password

View file

@ -36,11 +36,9 @@ module Diaspora
"<head> "<head>
<sender> <sender>
<email>#{User.first.email}</email> <email>#{User.first.email}</email>
<url>#{User.first.email}</url>
</sender> </sender>
</head>" </head>"
end end
end end
end end
end end

View file

@ -4,7 +4,7 @@
Factory.define :friend do |f| Factory.define :friend do |f|
f.real_name 'John Doe' f.real_name 'John Doe'
f.username 'max' f.email 'max@max.com'
f.url 'http://max.com/' f.url 'http://max.com/'
end end

View file

@ -4,7 +4,8 @@ include ApplicationHelper
describe ApplicationHelper do describe ApplicationHelper do
before do before do
Factory.create(:user) @user = Factory.create(:user, :email => "bob@aol.com")
@friend =Factory.create(:friend, :email => "bill@gates.com")
end end
it "should store objects sent from xml" do it "should store objects sent from xml" do
@ -18,17 +19,25 @@ describe ApplicationHelper do
end end
it 'should discard posts where it does not know the type' do it 'should discard posts where it does not know the type' do
xml = "<XML><posts> xml = "<XML>
<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> <head>
<post><not_a_real_type></not_a_real_type></post> <sender>
<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> <email>#{User.first.email}</email>
</sender>
</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>" </posts></XML>"
store_posts_from_xml(xml) store_posts_from_xml(xml)
Post.count.should == 2 Post.count.should == 2
end end
it 'should discard types which are not of type post' do it 'should discard types which are not of type post' do
xml = "<XML><posts> xml = "<XML>
<head>
<sender>
<email>#{User.first.email}</email>
</sender>
</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><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><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> <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>
@ -38,15 +47,35 @@ describe ApplicationHelper do
end end
describe "parsing a sender" do describe "parsing compliant XML object" do
it 'should be able to parse the sender of a collection' do before do
status_messages = [] status_messages = []
10.times { status_messages << Factory.build(:status_message)} 10.times { status_messages << Factory.build(:status_message)}
xml = Post.build_xml_for(status_messages) @xml = Post.build_xml_for(status_messages)
end end
it 'should be able to verify the sender as a friend' do it 'should be able to parse the sender\'s unique id' do
pending 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 "<head>"
body.should_not include "</head>"
body.should_not include "<posts>"
body.should_not include "</posts>"
body.should include "<post>"
body.should include "</post>"
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
end end

View file

@ -29,11 +29,7 @@ describe Diaspora do
end end
it "should provide the owner's email" do it "should provide the owner's email" do
@xml.should include "<email>bob@aol.com</email>" @xml.should include "<email>#{User.first.email}</email>"
end
it "should provide the owner's url" do
pending "user does not have url field"
end end
end end

View file

@ -64,7 +64,7 @@ describe Friend do
describe "XML" do describe "XML" do
before do before do
@f = Factory.build(:friend) @f = Factory.build(:friend)
@xml = "<friend>\n <username>#{@f.username}</username>\n <url>#{@f.url}</url>\n <real_name>#{@f.real_name}</real_name>\n</friend>" @xml = "<friend>\n <url>#{@f.url}</url>\n <email>#{@f.email}</email>\n <real_name>#{@f.real_name}</real_name>\n</friend>"
end end
it 'should serialize to XML' do it 'should serialize to XML' do
@ -73,7 +73,7 @@ describe Friend do
it 'should marshal serialized XML to object' do it 'should marshal serialized XML to object' do
parsed = Friend.from_xml(@xml) parsed = Friend.from_xml(@xml)
parsed.username.should == @f.username parsed.email.should == @f.email
parsed.url.should == @f.url parsed.url.should == @f.url
parsed.valid?.should be_true parsed.valid?.should be_true
end end

View file

@ -5,14 +5,14 @@ describe Post do
Factory.create(:user, :email => "bob@aol.com") Factory.create(:user, :email => "bob@aol.com")
end end
describe 'requirements' do
end
describe 'defaults' do describe 'defaults' do
before do before do
WebSocket.stub!(:update_clients) WebSocket.stub!(:update_clients)
@post = Factory.create(:post, :owner => nil, :source => nil, :snippet => nil) @post = Factory.create(:post, :person => nil, :owner => nil, :source => nil, :snippet => nil)
end
it "should associate the owner if none is present" do
@post.person.should == User.first
end end
it "should add an owner if none is present" do it "should add an owner if none is present" do