IZ, RS; Moved XML parser to lib
This commit is contained in:
parent
762cbcb0f2
commit
25198a7e86
3 changed files with 50 additions and 57 deletions
|
|
@ -1,4 +1,6 @@
|
||||||
module ApplicationHelper
|
module ApplicationHelper
|
||||||
|
require 'lib/common'
|
||||||
|
include Diaspora::XMLParser
|
||||||
def object_path(object)
|
def object_path(object)
|
||||||
eval("#{object.class.to_s.underscore}_path(object)")
|
eval("#{object.class.to_s.underscore}_path(object)")
|
||||||
end
|
end
|
||||||
|
|
@ -7,46 +9,6 @@ module ApplicationHelper
|
||||||
object.attributes.keys
|
object.attributes.keys
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_sender_id_from_xml(xml)
|
|
||||||
doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks }
|
|
||||||
doc.xpath("/XML/head/sender/email").text.to_s
|
|
||||||
end
|
|
||||||
|
|
||||||
def parse_sender_object_from_xml(xml)
|
|
||||||
sender_id = parse_sender_id_from_xml(xml)
|
|
||||||
Friend.where(:email => sender_id).first
|
|
||||||
end
|
|
||||||
|
|
||||||
def parse_body_contents_from_xml(xml)
|
|
||||||
doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks }
|
|
||||||
doc.xpath("/XML/posts/post")
|
|
||||||
end
|
|
||||||
|
|
||||||
def parse_objects_from_xml(xml)
|
|
||||||
objects = []
|
|
||||||
sender = parse_sender_object_from_xml(xml)
|
|
||||||
body = parse_body_contents_from_xml(xml)
|
|
||||||
body.children.each do |post|
|
|
||||||
begin
|
|
||||||
object = post.name.camelize.constantize.from_xml post.to_s
|
|
||||||
object.person = sender if object.is_a? Post
|
|
||||||
objects << object
|
|
||||||
rescue
|
|
||||||
puts "Not a real type: #{object.to_s}"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
objects
|
|
||||||
end
|
|
||||||
|
|
||||||
def store_objects_from_xml(xml)
|
|
||||||
objects = parse_objects_from_xml(xml)
|
|
||||||
|
|
||||||
objects.each do |p|
|
|
||||||
p.save if p.respond_to?(:person) && !(p.person.nil?) #WTF
|
|
||||||
#p.save if p.respond_to?(:person) && !(p.person == nil) #WTF
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def mine?(post)
|
def mine?(post)
|
||||||
post.person == User.first
|
post.person == User.first
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,47 @@
|
||||||
module Diaspora
|
module Diaspora
|
||||||
|
module XMLParser
|
||||||
|
def parse_sender_id_from_xml(xml)
|
||||||
|
doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks }
|
||||||
|
doc.xpath("/XML/head/sender/email").text.to_s
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse_sender_object_from_xml(xml)
|
||||||
|
sender_id = parse_sender_id_from_xml(xml)
|
||||||
|
Friend.where(:email => sender_id).first
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse_body_contents_from_xml(xml)
|
||||||
|
doc = Nokogiri::XML(xml) { |cfg| cfg.noblanks }
|
||||||
|
doc.xpath("/XML/posts/post")
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse_objects_from_xml(xml)
|
||||||
|
objects = []
|
||||||
|
sender = parse_sender_object_from_xml(xml)
|
||||||
|
body = parse_body_contents_from_xml(xml)
|
||||||
|
body.children.each do |post|
|
||||||
|
begin
|
||||||
|
object = post.name.camelize.constantize.from_xml post.to_s
|
||||||
|
object.person = sender if object.is_a? Post
|
||||||
|
objects << object
|
||||||
|
rescue
|
||||||
|
puts "Not a real type: #{object.to_s}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
objects
|
||||||
|
end
|
||||||
|
|
||||||
|
def store_objects_from_xml(xml)
|
||||||
|
objects = parse_objects_from_xml(xml)
|
||||||
|
|
||||||
|
objects.each do |p|
|
||||||
|
p.save if p.respond_to?(:person) && !(p.person.nil?) #WTF
|
||||||
|
#p.save if p.respond_to?(:person) && !(p.person == nil) #WTF
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
end
|
||||||
module Webhooks
|
module Webhooks
|
||||||
def self.included(klass)
|
def self.included(klass)
|
||||||
klass.class_eval do
|
klass.class_eval do
|
||||||
|
|
|
||||||
|
|
@ -15,19 +15,7 @@ describe "parser in application helper" do
|
||||||
store_objects_from_xml(xml)
|
store_objects_from_xml(xml)
|
||||||
StatusMessage.count.should == 0
|
StatusMessage.count.should == 0
|
||||||
end
|
end
|
||||||
it 'should discard posts where it does not know the type' do
|
|
||||||
xml = "<XML>
|
|
||||||
<head>
|
|
||||||
<sender>
|
|
||||||
<email>#{Friend.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>"
|
|
||||||
store_objects_from_xml(xml)
|
|
||||||
Post.count.should == 2
|
|
||||||
Post.first.person.email.should == Friend.first.email
|
|
||||||
end
|
|
||||||
it "should reject xml with no sender" do
|
it "should reject xml with no sender" do
|
||||||
xml = "<XML>
|
xml = "<XML>
|
||||||
<head>
|
<head>
|
||||||
|
|
@ -40,6 +28,7 @@ describe "parser in application helper" do
|
||||||
Post.count.should == 0
|
Post.count.should == 0
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should reject xml with a sender not in the database" do
|
it "should reject xml with a sender not in the database" do
|
||||||
xml = "<XML>
|
xml = "<XML>
|
||||||
<head>
|
<head>
|
||||||
|
|
@ -54,6 +43,7 @@ describe "parser in application helper" do
|
||||||
store_objects_from_xml(xml)
|
store_objects_from_xml(xml)
|
||||||
Post.count.should == 0
|
Post.count.should == 0
|
||||||
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>
|
xml = "<XML>
|
||||||
<head>
|
<head>
|
||||||
|
|
@ -62,13 +52,11 @@ describe "parser in application helper" do
|
||||||
</sender>
|
</sender>
|
||||||
</head>
|
</head>
|
||||||
<posts>
|
<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><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>
|
|
||||||
</posts></XML>"
|
</posts></XML>"
|
||||||
|
|
||||||
store_objects_from_xml(xml)
|
store_objects_from_xml(xml)
|
||||||
Post.count.should == 2
|
Post.count.should == 0
|
||||||
Post.first.person.email.should == Friend.first.email
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue