From 1118b56bb1ae06eab45cda8adf2453dc5d504bdf Mon Sep 17 00:00:00 2001 From: maxwell Date: Thu, 22 Jul 2010 16:55:28 -0700 Subject: [PATCH] DG MS we now fully parse an ostatus response --- lib/common.rb | 84 +++++++++++++++++++++++++++++++- lib/message_handler.rb | 2 +- spec/fixtures/ostatus_update.xml | 2 + spec/lib/message_handler_spec.rb | 4 +- spec/lib/parser_spec.rb | 62 +++++++++++++++++++++++ 5 files changed, 149 insertions(+), 5 deletions(-) create mode 100644 spec/fixtures/ostatus_update.xml diff --git a/lib/common.rb b/lib/common.rb index 461f050f7..f838d97bd 100644 --- a/lib/common.rb +++ b/lib/common.rb @@ -4,10 +4,90 @@ module Diaspora Nokogiri::HTML(xml).xpath('//link[@rel="hub"]').first.attribute("href").value end - def self.parse_sender(xml) - puts "you just won the game" + def self.process(xml) + doc = Nokogiri::HTML(xml) + parse_author(doc) + puts "" + parse_entry(doc) end + def parse_author(doc) + doc = Nokogiri::HTML(doc) if doc.is_a? String + + + service = parse_service(doc) + feed_url = parse_feed_url(doc) + avatar_thumbnail = parse_avatar_thumbnail(doc) + username = parse_username(doc) + profile_url = parse_profile_url(doc) + + + + puts "the sender:" + puts service + puts feed_url + puts avatar_thumbnail + puts username + puts profile_url + end + + def parse_entry(doc) + doc = Nokogiri::HTML(doc) if doc.is_a? String + + message = parse_message(doc) + permalink = parse_permalink(doc) + published_at = parse_published_at(doc) + updated_at = parse_updated_at(doc) + + + puts "the message" + puts message + puts permalink + puts published_at + puts updated_at + end + + + ##author### + def self.parse_service(doc) + doc.xpath('//generator').inner_html + end + + def self.parse_feed_url(doc) + doc.xpath('//id').first.inner_html + end + + def self.parse_avatar_thumbnail(doc) + doc.xpath('//logo').first.inner_html + end + + def self.parse_username(doc) + doc.xpath('//author/name').first.inner_html + end + + def self.parse_profile_url(doc) + doc.xpath('//author/uri').first.inner_html + end + + + #entry## + def self.parse_message(doc) + doc.xpath('//entry/title').first.inner_html + end + + def self.parse_permalink(doc) + doc.xpath('//entry/id').first.inner_html + end + + def self.parse_published_at(doc) + doc.xpath('//entry/published').first.inner_html + end + + def self.parse_updated_at(doc) + doc.xpath('//entry/updated').first.inner_html + end + + def self.parse_objects(xml) end diff --git a/lib/message_handler.rb b/lib/message_handler.rb index 16de0a0e1..b1e84524c 100644 --- a/lib/message_handler.rb +++ b/lib/message_handler.rb @@ -34,7 +34,7 @@ class MessageHandler def process_ostatus_subscription(query_object, http) hub = Diaspora::OStatusParser::find_hub(http.response) add_hub_subscription_request(hub, query_object.destination) - Diaspora::OStatusParser::parse_sender(http.response) + Diaspora::OStatusParser::process(http.response) end def process diff --git a/spec/fixtures/ostatus_update.xml b/spec/fixtures/ostatus_update.xml new file mode 100644 index 000000000..342ec80c2 --- /dev/null +++ b/spec/fixtures/ostatus_update.xml @@ -0,0 +1,2 @@ +"\n\n StatusNet\n http://identi.ca/api/statuses/user_timeline/217769.atom\n danielgrippi timeline\n Updates from danielgrippi on Identi.ca!\n http://theme.status.net/0.9.3/identica/default-avatar-profile.png\n 2010-07-22T22:15:31+00:00\n\n danielgrippi\n http://identi.ca/user/217769\n\n \n \n \n \n \n \n\n http://activitystrea.ms/schema/1.0/person\n http://identi.ca/user/217769\n Daniel Grippi\n \n \n \n \n 0 0\ndanielgrippi\nDaniel Grippi\nhey there kids!\n\n earth\n\n\n homepage\n http://danielgrippi.com/\n true\n\n\n\n SOAP!\n \n http://identi.ca/notice/43074747\n 2010-07-22T22:15:31+00:00\n 2010-07-22T22:15:31+00:00\n \n \n SOAP!\n 37.7912 -122.401\n\n\n" + diff --git a/spec/lib/message_handler_spec.rb b/spec/lib/message_handler_spec.rb index 0d9a56f4d..d71dc2142 100644 --- a/spec/lib/message_handler_spec.rb +++ b/spec/lib/message_handler_spec.rb @@ -194,10 +194,10 @@ describe MessageHandler do Diaspora::OStatusParser.stub!(:find_hub).and_return("http://hub.google.com") MessageHandler.stub!(:add_hub_subscription_request).and_return(true) - Diaspora::OStatusParser.stub!(:parse_sender) + Diaspora::OStatusParser.stub!(:process) Diaspora::OStatusParser.should_receive(:find_hub) @handler.should_receive(:add_hub_subscription_request) - Diaspora::OStatusParser.should_receive(:parse_sender) + Diaspora::OStatusParser.should_receive(:process) g = mock("Message") g.stub!(:destination).and_return("google") diff --git a/spec/lib/parser_spec.rb b/spec/lib/parser_spec.rb index 46bb23aab..be2d4c2c2 100644 --- a/spec/lib/parser_spec.rb +++ b/spec/lib/parser_spec.rb @@ -68,6 +68,68 @@ describe "parser in application helper" do Diaspora::OStatusParser::find_hub(xml).should == 'http://identi.ca/main/push/hub' end + + describe 'subscriber info' do + before do + #load file + xml_path = File.dirname(__FILE__) + '/../fixtures/ostatus_update.xml' + @xml = File.open(xml_path).read + @xml = Nokogiri::HTML(@xml) + end + + + it 'should parse the users service' do + Diaspora::OStatusParser::parse_service(@xml).should == 'StatusNet' + end + + it 'should parse the feed_url' do + Diaspora::OStatusParser::parse_feed_url(@xml).should == 'http://identi.ca/api/statuses/user_timeline/217769.atom' + end + + it 'should parse the avatar thumbnail' do + Diaspora::OStatusParser::parse_avatar_thumbnail(@xml).should == 'http://theme.status.net/0.9.3/identica/default-avatar-profile.png' + end + + it 'should parse the username' do + Diaspora::OStatusParser::parse_username(@xml).should == 'danielgrippi' + end + + it 'should parse the profile_url' do + Diaspora::OStatusParser::parse_profile_url(@xml).should == 'http://identi.ca/user/217769' + end + + end + + describe 'entry' do + before do + #load file + xml_path = File.dirname(__FILE__) + '/../fixtures/ostatus_update.xml' + @xml = File.open(xml_path).read + @xml = Nokogiri::HTML(@xml) + end + + it 'should parse the message' do + Diaspora::OStatusParser::parse_message(@xml).should == 'SOAP!' + end + + it 'should parse the permalink' do + Diaspora::OStatusParser::parse_permalink(@xml).should == 'http://identi.ca/notice/43074747' + end + + it 'should parse published at date' do + Diaspora::OStatusParser::parse_published_at(@xml).should == '2010-07-22T22:15:31+00:00' + + end + + it 'should parse the updated at date' do + Diaspora::OStatusParser::parse_updated_at(@xml).should == '2010-07-22T22:15:31+00:00' + end + end + + + + + end describe "parsing compliant XML object" do