add exceptions for salmon stuff and move all to one file
This commit is contained in:
parent
7b4753148b
commit
778a782c76
4 changed files with 59 additions and 22 deletions
|
|
@ -5,4 +5,5 @@ module DiasporaFederation
|
|||
end
|
||||
end
|
||||
|
||||
require "diaspora_federation/salmon/exceptions"
|
||||
require "diaspora_federation/salmon/xml_payload"
|
||||
|
|
|
|||
50
lib/diaspora_federation/salmon/exceptions.rb
Normal file
50
lib/diaspora_federation/salmon/exceptions.rb
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
module DiasporaFederation
|
||||
module Salmon
|
||||
# Raised, if the element containing the Magic Envelope is missing from the XML
|
||||
class MissingMagicEnvelope < RuntimeError
|
||||
end
|
||||
|
||||
# Raised, if the element containing the author is empty.
|
||||
class MissingAuthor < RuntimeError
|
||||
end
|
||||
|
||||
# Raised, if the element containing the header is missing from the XML
|
||||
class MissingHeader < RuntimeError
|
||||
end
|
||||
|
||||
# Raised if the decrypted header has an unexpected XML structure
|
||||
class InvalidHeader < RuntimeError
|
||||
end
|
||||
|
||||
# Raised, if the Magic Envelope XML structure is malformed.
|
||||
class InvalidEnvelope < RuntimeError
|
||||
end
|
||||
|
||||
# Raised, if the calculated signature doesn't match the one contained in the
|
||||
# Magic Envelope.
|
||||
class InvalidSignature < RuntimeError
|
||||
end
|
||||
|
||||
# Raised, if the parsed Magic Envelope specifies an unhandled algorithm.
|
||||
class InvalidAlgorithm < RuntimeError
|
||||
end
|
||||
|
||||
# Raised, if the parsed Magic Envelope specifies an unhandled encoding.
|
||||
class InvalidEncoding < RuntimeError
|
||||
end
|
||||
|
||||
# Raised, if the XML structure of the parsed document doesn't resemble the
|
||||
# expected structure.
|
||||
class InvalidStructure < RuntimeError
|
||||
end
|
||||
|
||||
# Raised, if the entity name in the XML is invalid
|
||||
class InvalidEntityName < RuntimeError
|
||||
end
|
||||
|
||||
# Raised, if the entity contained within the XML cannot be mapped to a
|
||||
# defined {Entity} subclass.
|
||||
class UnknownEntity < RuntimeError
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -44,11 +44,11 @@ module DiasporaFederation
|
|||
# XML can't be found
|
||||
def self.unpack(xml)
|
||||
raise ArgumentError, "only Nokogiri::XML::Element allowed" unless xml.instance_of?(Nokogiri::XML::Element)
|
||||
raise InvalidStructure unless wrap_valid?(xml)
|
||||
raise Salmon::InvalidStructure unless wrap_valid?(xml)
|
||||
|
||||
data = xml.at_xpath("post/*[1]")
|
||||
klass_name = entity_class_name(data.name)
|
||||
raise UnknownEntity, "'#{klass_name}' not found" unless Entities.const_defined?(klass_name)
|
||||
raise Salmon::UnknownEntity, "'#{klass_name}' not found" unless Entities.const_defined?(klass_name)
|
||||
|
||||
klass = Entities.const_get(klass_name)
|
||||
populate_entity(klass, data)
|
||||
|
|
@ -70,7 +70,7 @@ module DiasporaFederation
|
|||
# @return [String] "CamelCase" class name
|
||||
def self.entity_class_name(term)
|
||||
term.to_s.tap do |string|
|
||||
raise InvalidEntityName, "'#{string}' is invalid" unless string =~ /^[a-z]*(_[a-z]*)*$/
|
||||
raise Salmon::InvalidEntityName, "'#{string}' is invalid" unless string =~ /^[a-z]*(_[a-z]*)*$/
|
||||
string.sub!(/^[a-z]/, &:upcase)
|
||||
string.gsub!(/_([a-z])/) { Regexp.last_match[1].upcase }
|
||||
end
|
||||
|
|
@ -126,20 +126,6 @@ module DiasporaFederation
|
|||
n.map {|child| populate_entity(type.first, child) }
|
||||
end
|
||||
private_class_method :parse_array_from_node
|
||||
|
||||
# Raised, if the XML structure of the parsed document doesn't resemble the
|
||||
# expected structure.
|
||||
class InvalidStructure < RuntimeError
|
||||
end
|
||||
|
||||
# Raised, if the entity name in the XML is invalid
|
||||
class InvalidEntityName < RuntimeError
|
||||
end
|
||||
|
||||
# Raised, if the entity contained within the XML cannot be mapped to a
|
||||
# defined {Entity} subclass.
|
||||
class UnknownEntity < RuntimeError
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -77,7 +77,7 @@ XML
|
|||
XML
|
||||
expect {
|
||||
Salmon::XmlPayload.unpack(Nokogiri::XML::Document.parse(xml).root)
|
||||
}.to raise_error Salmon::XmlPayload::UnknownEntity, "'UnknownEntity' not found"
|
||||
}.to raise_error Salmon::UnknownEntity, "'UnknownEntity' not found"
|
||||
end
|
||||
|
||||
it "raises an error when the entity is not found" do
|
||||
|
|
@ -88,7 +88,7 @@ XML
|
|||
XML
|
||||
expect {
|
||||
Salmon::XmlPayload.unpack(Nokogiri::XML::Document.parse(xml).root)
|
||||
}.to raise_error Salmon::XmlPayload::InvalidStructure
|
||||
}.to raise_error Salmon::InvalidStructure
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -136,19 +136,19 @@ XML
|
|||
it "raises an error when the entity name contains special characters" do
|
||||
expect {
|
||||
Salmon::XmlPayload.send(:entity_class_name, "te.st-enti/ty")
|
||||
}.to raise_error Salmon::XmlPayload::InvalidEntityName, "'te.st-enti/ty' is invalid"
|
||||
}.to raise_error Salmon::InvalidEntityName, "'te.st-enti/ty' is invalid"
|
||||
end
|
||||
|
||||
it "raises an error when the entity name contains upper case letters" do
|
||||
expect {
|
||||
Salmon::XmlPayload.send(:entity_class_name, "TestEntity")
|
||||
}.to raise_error Salmon::XmlPayload::InvalidEntityName, "'TestEntity' is invalid"
|
||||
}.to raise_error Salmon::InvalidEntityName, "'TestEntity' is invalid"
|
||||
end
|
||||
|
||||
it "raises an error when the entity name contains numbers" do
|
||||
expect {
|
||||
Salmon::XmlPayload.send(:entity_class_name, "te5t_ent1ty_w1th_number5")
|
||||
}.to raise_error Salmon::XmlPayload::InvalidEntityName, "'te5t_ent1ty_w1th_number5' is invalid"
|
||||
}.to raise_error Salmon::InvalidEntityName, "'te5t_ent1ty_w1th_number5' is invalid"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue