DG MS; diaspora xml generate model genereates a valid atom feed for status messages

This commit is contained in:
maxwell 2010-07-13 12:09:06 -07:00
parent ed8ed9be9e
commit 0bcbd5b16c
3 changed files with 96 additions and 101 deletions

View file

@ -1,5 +1,5 @@
class StatusMessagesController < ApplicationController class StatusMessagesController < ApplicationController
before_filter :authenticate_user! #before_filter :authenticate_user!
def index def index
@status_messages = StatusMessage.paginate :page => params[:page], :order => 'created_at DESC' @status_messages = StatusMessage.paginate :page => params[:page], :order => 'created_at DESC'
@ -7,8 +7,7 @@ class StatusMessagesController < ApplicationController
respond_to do |format| respond_to do |format|
format.html format.html
format.xml {render :xml => Post.build_xml_for(@status_messages)} format.atom {render :xml => Diaspora::XML::generate(:current_url => request.url, :objects => @status_messages)}
format.json { render :json => @status_messages }
end end
end end

View file

@ -94,68 +94,66 @@ module Diaspora
module XML module XML
OWNER = User.owner
def self.generate(opts= {}) def self.generate(opts= {})
@owner = User.owner xml = Generate::headers(opts[:current_url])
@root_url = @owner.url xml << Generate::author
xml << Generate::endpoints
xml = self.generate_headers(opts[:current_url]) xml << Generate::subject
xml << self.generate_author xml << Generate::entries(opts[:objects])
xml << self.generate_endpoints xml << Generate::footer
xml << self.generate_subject
xml << self.build_entries(opts[:objects])
xml << self.generate_footer
end end
def self.generate_headers(current_url) module Generate
def self.headers(current_url)
<<-XML <<-XML
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:georss="http://www.georss.org/georss" xmlns:activity="http://activitystrea.ms/spec/1.0/" xmlns:media="http://purl.org/syndication/atommedia" xmlns:poco="http://portablecontacts.net/spec/1.0" xmlns:ostatus="http://ostatus.org/schema/1.0" xmlns:statusnet="http://status.net/schema/api/1/"> <feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:georss="http://www.georss.org/georss" xmlns:activity="http://activitystrea.ms/spec/1.0/" xmlns:media="http://purl.org/syndication/atommedia" xmlns:poco="http://portablecontacts.net/spec/1.0" xmlns:ostatus="http://ostatus.org/schema/1.0" xmlns:statusnet="http://status.net/schema/api/1/">
<generator uri="#{@owner.url}">Diaspora</generator> <generator uri="#{OWNER.url}">Diaspora</generator>
<id>#{current_url}</id> <id>#{current_url}</id>
<title>Stream</title> <title>Stream</title>
<subtitle>its a stream </subtitle> <subtitle>its a stream </subtitle>
<logo></logo> <updated>#{Time.now.xmlschema}</updated>
<updated>#{Time.now}</updated>
XML XML
end end
def self.generate_author def self.author
<<-XML <<-XML
<author> <author>
<name>#{@owner.real_name}</name> <name>#{OWNER.real_name}</name>
<uri>#{@root_url}</uri> <uri>#{OWNER.url}</uri>
</author> </author>
XML XML
end end
def self.generate_endpoints def self.endpoints
#generate pubsub, poco, salmon endpoints #generate pubsub, poco, salmon endpoints
"" ""
end end
def self.build_entries(objects) def self.subject
xml = ""
if objects.respond_to? :each
objects.each {|x| xml << self.build_entry(x)}
else
xml << self.build_entry(objects)
end
xml
end
def self.generate_subject
<<-XML <<-XML
<activity:subject> <activity:subject>
<activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type> <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
<id>#{@root_url}</id> <id>#{OWNER.url}</id>
<title>#{@owner.real_name}</title> <title>#{OWNER.real_name}</title>
<link rel="alternative" type="text/html" href="#{@root_url}"/> <link rel="alternative" type="text/html" href="#{OWNER.url}"/>
</activity:subject> </activity:subject>
XML XML
end end
def self.build_entry(object) def self.entries(objects)
xml = ""
if objects.respond_to? :each
objects.each {|x| xml << self.entry(x)}
else
xml << self.entry(objects)
end
xml
end
def self.entry(object)
eval "#{object.class}_build_entry(object)" eval "#{object.class}_build_entry(object)"
end end
@ -163,22 +161,19 @@ module Diaspora
<<-XML <<-XML
<entry> <entry>
<title>#{status_message.message}</title> <title>#{status_message.message}</title>
<link rel="alternative" type="text/html" href="#{@root_url}/status_messages/#{status_message.id}"/> <link rel="alternate" type="text/html" href="#{OWNER.url}status_messages/#{status_message.id}"/>
<id>#{status_message.id}</id> <id>#{OWNER.url}status_messages/#{status_message.id}</id>
<published>#{status_message.created_at}</published> <published>#{status_message.created_at.xmlschema}</published>
<updated>#{status_message.updated_at}</updated> <updated>#{status_message.updated_at.xmlschema}</updated>
</entry> </entry>
XML XML
end end
def self.footer
def self.generate_footer <<-XML.strip
<<-XML
</feed> </feed>
XML XML
end end
end end
end
end end

View file

@ -1,30 +1,31 @@
require File.dirname(__FILE__) + '/../spec_helper' require File.dirname(__FILE__) + '/../spec_helper'
describe "XML generation" do describe Diaspora::XML do
before do
@user = Factory.create(:user, :profile => { :first_name => "robert", :last_name => "grimm" } )
Diaspora::XML::OWNER = @user
end
describe Diaspora::XML::Generate do
describe "header" do describe "header" do
it 'should generate an OStatus compliant header' do it 'should generate an OStatus compliant header' do
user = Factory.create(:user) Diaspora::XML::Generate::headers(:current_url => @user.url).should include @user.url
Diaspora::XML::generate_headers.should include user.url
end end
end end
describe "status message entry" do describe "status message entry" do
before do before do
@status_message = Factory.build(:status_message) @status_message = Factory.create(:status_message, :message => "feed me")
end end
it "should encode to activity stream xml" do it "should encode to activity stream xml" do
Factory.create(:user) sm_entry = Diaspora::XML::generate(:objects => @status_message, :current_url => "http://diaspora.com/")
sm_entry = Diaspora::XML::generate(:objects => @status_message)
sm_entry.should include(@status_message.message) sm_entry.should include(@status_message.message)
sm_entry.should include('title') sm_entry.should include('title')
puts sm_entry
end end
end end
end
end end