diff --git a/app/controllers/status_messages_controller.rb b/app/controllers/status_messages_controller.rb index 7c65ed3c7b5090e55336a71e5af005f7cf39457b..beae57255092bd10c7f667c5d567e54060a5c6c4 100644 --- a/app/controllers/status_messages_controller.rb +++ b/app/controllers/status_messages_controller.rb @@ -1,9 +1,17 @@ class StatusMessagesController < ApplicationController - before_filter :authenticate_user! - + #before_filter :authenticate_user! + include StatusMessagesHelper + def index @status_messages = StatusMessage.all @friends = Friend.all + + respond_to do |format| + format.html + format.xml {render :xml => StatusMessages.new(@status_messages).to_xml } + format.json { render :json => @status_messages } + end + end def create diff --git a/app/helpers/status_messages_helper.rb b/app/helpers/status_messages_helper.rb index 147486830111dd1b662dcdab9de859c97b519b10..71a0b26ea6fd584605501d33d6db5d0d1a15c6a3 100644 --- a/app/helpers/status_messages_helper.rb +++ b/app/helpers/status_messages_helper.rb @@ -1,4 +1,5 @@ module StatusMessagesHelper + def my_latest_message message = StatusMessage.my_newest unless message.nil? @@ -7,4 +8,15 @@ module StatusMessagesHelper return "No message to display." end end + + class StatusMessages + include ROXML + + def initialize(messages=[]) + @statusmessages = messages + end + + xml_accessor :statusmessages, :as => [StatusMessage] + attr_accessor :statusmessages + end end diff --git a/app/models/bookmark.rb b/app/models/bookmark.rb index 3480c72fb93a78be4645660a94b2eb992b5684d0..fc62d72250638f48cf02e5bbd8e9a058508942af 100644 --- a/app/models/bookmark.rb +++ b/app/models/bookmark.rb @@ -17,4 +17,4 @@ class Bookmark def set_default_owner self.owner ||= User.first.email end -end \ No newline at end of file +end diff --git a/app/models/friend.rb b/app/models/friend.rb index d6cd9e5e4fc635579f752f91f2dbc4bc72fdd87d..061a2094b8a6e4a51acb4eac028186ce45494bec 100644 --- a/app/models/friend.rb +++ b/app/models/friend.rb @@ -1,9 +1,13 @@ class Friend include Mongoid::Document - + include ROXML + + xml_accessor :username + xml_accessor :url + field :username field :url - - validates_presence_of :username, :url -end \ No newline at end of file + validates_presence_of :username, :url + +end diff --git a/app/models/status_message.rb b/app/models/status_message.rb index 7d78ce8054e100ffc51d11e54a080f532f2d8f35..9ed8b1f95eb4579054b1745d0baecdcb439c297a 100644 --- a/app/models/status_message.rb +++ b/app/models/status_message.rb @@ -2,7 +2,8 @@ class StatusMessage include Mongoid::Document include Mongoid::Timestamps include ROXML - + include StatusMessagesHelper + xml_accessor :message xml_accessor :owner @@ -21,10 +22,15 @@ class StatusMessage def self.my_newest StatusMessage.newest(User.first.email) end - + + def self.retrieve_from_friend(friend) + StatusMessages.from_xml `curl #{friend.url}status_messages.xml --user a@a.com:aaaaaa` + end protected def set_default_owner - self.owner ||= User.first.email + self.owner ||= User.first.email end + end + diff --git a/spec/controllers/status_messages_controller_spec.rb b/spec/controllers/status_messages_controller_spec.rb index 3468b9d364cbde4b30d9253c7d9d9247266ae31a..ecea61257dcea452a96e584403ca7dee458b86d1 100644 --- a/spec/controllers/status_messages_controller_spec.rb +++ b/spec/controllers/status_messages_controller_spec.rb @@ -47,10 +47,21 @@ describe StatusMessagesController do response.should render_template(:show) end - it "should return xml on the show type if the meme type exsits" do + it "should return xml on show type if the MIME type exists" do request.env["HTTP_ACCEPT"] = "application/xml" message = StatusMessage.first get :show, :id => message.id response.body.include?(message.to_xml.to_s).should be true end + + it "should return xml on index if the MIME type exists" do + Factory.create(:status_message) + + request.env["HTTP_ACCEPT"] = "application/xml" + get :index + StatusMessage.all.each do |message| + response.body.include?(message.message).should be true + response.body.include?(message.owner).should be true + end + end end diff --git a/spec/models/bookmark_spec.rb b/spec/models/bookmark_spec.rb index ac65dc5e4bde59feb007ba2b7a570b6e86415e5f..99cbbd2a53995c1e838ce486ec419e074237a54d 100644 --- a/spec/models/bookmark_spec.rb +++ b/spec/models/bookmark_spec.rb @@ -9,7 +9,6 @@ describe Bookmark do end it "should add an owner if none is present" do - #User.create(:email => "bob@aol.com", :password => "big bux") Factory.create(:user, :email => "bob@aol.com") n = Factory.create(:bookmark) n.owner.should == "bob@aol.com" diff --git a/spec/models/friend_spec.rb b/spec/models/friend_spec.rb index b76371aaa01cbe5789b4f38e0099b59518899ff7..57417e9b05f47474feb72e6d0a51ae9a89fff9ef 100644 --- a/spec/models/friend_spec.rb +++ b/spec/models/friend_spec.rb @@ -8,4 +8,23 @@ describe Friend do n.url = "http://max.com/" n.valid?.should be true end + + describe "XML" do + before do + @f = Factory.build(:friend) + @xml = "<friend>\n <username>#{@f.username}</username>\n <url>#{@f.url}</url>\n</friend>" + end + + it 'should serialize to XML' do + @f.to_xml.to_s.should == @xml + end + + it 'should marshal serialized XML to object' do + parsed = Friend.from_xml(@xml) + parsed.username.should == @f.username + parsed.url.should == @f.url + parsed.valid?.should be_true + end + end + end diff --git a/spec/models/status_message_spec.rb b/spec/models/status_message_spec.rb index f2f293cd967f5824b97f051fca21150e2b079625..fd82f8075d7dafe6bd298bbedde4169cd1d59a62 100644 --- a/spec/models/status_message_spec.rb +++ b/spec/models/status_message_spec.rb @@ -46,6 +46,27 @@ describe StatusMessage do parsed = StatusMessage.from_xml(@xml) parsed.message.should == "I hate WALRUSES!" parsed.owner.should == "Bob" + parsed.valid?.should be_true + end + end + + describe "retrieving" do + before do + @remote = Factory.create(:friend, :url => "http://localhost:1254/") + @remote_xml = (0..5).collect{Factory.create(:status_message).to_xml} + StatusMessages = StatusMessagesHelper::StatusMessages + #@remote_messages = (0..5).collect {|a| Factory.build(:status_message)} + #stub with response of @remote_msg.xml + end + it "should marshal xml and serialize it" do + messages = StatusMessages.new(@remote_xml) + unreadable_xml = messages.to_xml.to_s.sub("\t\t","\t") + unreadable_xml.include?(remote_xml.first.to_s).should be true + end + it "marshal retrieved xml" do + StatusMessage.retrieve_from_friend(@remote).to_xml.should == StatusMessages.from_xml(@remote_xml).to_xml + + # .from_xml == @remote_messages end end end