IZ RS getting remote messages (not yet done)

This commit is contained in:
Raphael Sofaer 2010-06-15 18:41:54 -07:00
parent 4da68fc4b1
commit c4b931c309
9 changed files with 92 additions and 12 deletions

View file

@ -1,9 +1,17 @@
class StatusMessagesController < ApplicationController class StatusMessagesController < ApplicationController
before_filter :authenticate_user! #before_filter :authenticate_user!
include StatusMessagesHelper
def index def index
@status_messages = StatusMessage.all @status_messages = StatusMessage.all
@friends = Friend.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 end
def create def create

View file

@ -1,4 +1,5 @@
module StatusMessagesHelper module StatusMessagesHelper
def my_latest_message def my_latest_message
message = StatusMessage.my_newest message = StatusMessage.my_newest
unless message.nil? unless message.nil?
@ -7,4 +8,15 @@ module StatusMessagesHelper
return "No message to display." return "No message to display."
end end
end end
class StatusMessages
include ROXML
def initialize(messages=[])
@statusmessages = messages
end
xml_accessor :statusmessages, :as => [StatusMessage]
attr_accessor :statusmessages
end
end end

View file

@ -17,4 +17,4 @@ class Bookmark
def set_default_owner def set_default_owner
self.owner ||= User.first.email self.owner ||= User.first.email
end end
end end

View file

@ -1,9 +1,13 @@
class Friend class Friend
include Mongoid::Document include Mongoid::Document
include ROXML
xml_accessor :username
xml_accessor :url
field :username field :username
field :url field :url
validates_presence_of :username, :url
end validates_presence_of :username, :url
end

View file

@ -2,7 +2,8 @@ class StatusMessage
include Mongoid::Document include Mongoid::Document
include Mongoid::Timestamps include Mongoid::Timestamps
include ROXML include ROXML
include StatusMessagesHelper
xml_accessor :message xml_accessor :message
xml_accessor :owner xml_accessor :owner
@ -21,10 +22,15 @@ class StatusMessage
def self.my_newest def self.my_newest
StatusMessage.newest(User.first.email) StatusMessage.newest(User.first.email)
end end
def self.retrieve_from_friend(friend)
StatusMessages.from_xml `curl #{friend.url}status_messages.xml --user a@a.com:aaaaaa`
end
protected protected
def set_default_owner def set_default_owner
self.owner ||= User.first.email self.owner ||= User.first.email
end end
end end

View file

@ -47,10 +47,21 @@ describe StatusMessagesController do
response.should render_template(:show) response.should render_template(:show)
end 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" request.env["HTTP_ACCEPT"] = "application/xml"
message = StatusMessage.first message = StatusMessage.first
get :show, :id => message.id get :show, :id => message.id
response.body.include?(message.to_xml.to_s).should be true response.body.include?(message.to_xml.to_s).should be true
end 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 end

View file

@ -9,7 +9,6 @@ describe Bookmark do
end end
it "should add an owner if none is present" do 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") Factory.create(:user, :email => "bob@aol.com")
n = Factory.create(:bookmark) n = Factory.create(:bookmark)
n.owner.should == "bob@aol.com" n.owner.should == "bob@aol.com"

View file

@ -8,4 +8,23 @@ describe Friend do
n.url = "http://max.com/" n.url = "http://max.com/"
n.valid?.should be true n.valid?.should be true
end 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 end

View file

@ -46,6 +46,27 @@ describe StatusMessage do
parsed = StatusMessage.from_xml(@xml) parsed = StatusMessage.from_xml(@xml)
parsed.message.should == "I hate WALRUSES!" parsed.message.should == "I hate WALRUSES!"
parsed.owner.should == "Bob" 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 end
end end