refactoring: use more tap :)
This commit is contained in:
parent
5d1f6ab220
commit
89de392fda
4 changed files with 37 additions and 46 deletions
|
|
@ -161,7 +161,7 @@ module DiasporaFederation
|
|||
def self.from_html(html_string)
|
||||
doc = parse_html_and_validate(html_string)
|
||||
|
||||
data = {
|
||||
new(
|
||||
guid: guid_from_doc(doc),
|
||||
nickname: content_from_doc(doc, :nickname),
|
||||
full_name: content_from_doc(doc, :fn),
|
||||
|
|
@ -173,12 +173,10 @@ module DiasporaFederation
|
|||
# TODO: public key is new and can be missing
|
||||
public_key: (content_from_doc(doc, :key) unless element_from_doc(doc, :key).nil?),
|
||||
|
||||
# TODO: remove me! ###################
|
||||
# TODO: remove first_name and last_name!
|
||||
first_name: content_from_doc(doc, :given_name),
|
||||
last_name: content_from_doc(doc, :family_name)
|
||||
#######################################
|
||||
}
|
||||
new(data)
|
||||
)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
|||
|
|
@ -175,10 +175,10 @@ module DiasporaFederation
|
|||
# @return [Hash] data XML data
|
||||
# @raise [InvalidData] if the given XML string is invalid or incomplete
|
||||
def self.parse_xml_and_validate(webfinger_xml)
|
||||
data = XrdDocument.xml_data(webfinger_xml)
|
||||
valid = data.key?(:subject) && data.key?(:aliases) && data.key?(:links)
|
||||
raise InvalidData, "webfinger xml is incomplete" unless valid
|
||||
data
|
||||
XrdDocument.xml_data(webfinger_xml).tap do |data|
|
||||
valid = data.key?(:subject) && data.key?(:aliases) && data.key?(:links)
|
||||
raise InvalidData, "webfinger xml is incomplete" unless valid
|
||||
end
|
||||
end
|
||||
private_class_method :parse_xml_and_validate
|
||||
|
||||
|
|
|
|||
|
|
@ -92,19 +92,18 @@ module DiasporaFederation
|
|||
# @raise [InvalidDocument] if the XRD is malformed
|
||||
def self.xml_data(xrd_doc)
|
||||
doc = parse_xrd_document(xrd_doc)
|
||||
data = {}
|
||||
|
||||
exp_elem = doc.at_xpath("xrd:XRD/xrd:Expires", NS)
|
||||
data[:expires] = DateTime.strptime(exp_elem.content, DATETIME_FORMAT) unless exp_elem.nil?
|
||||
{}.tap do |data|
|
||||
exp_elem = doc.at_xpath("xrd:XRD/xrd:Expires", NS)
|
||||
data[:expires] = DateTime.strptime(exp_elem.content, DATETIME_FORMAT) unless exp_elem.nil?
|
||||
|
||||
subj_elem = doc.at_xpath("xrd:XRD/xrd:Subject", NS)
|
||||
data[:subject] = subj_elem.content unless subj_elem.nil?
|
||||
subj_elem = doc.at_xpath("xrd:XRD/xrd:Subject", NS)
|
||||
data[:subject] = subj_elem.content unless subj_elem.nil?
|
||||
|
||||
parse_aliases_from_xml_doc(doc, data)
|
||||
parse_properties_from_xml_doc(doc, data)
|
||||
parse_links_from_xml_doc(doc, data)
|
||||
|
||||
data
|
||||
parse_aliases_from_xml_doc(doc, data)
|
||||
parse_properties_from_xml_doc(doc, data)
|
||||
parse_links_from_xml_doc(doc, data)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
|||
|
|
@ -76,16 +76,12 @@ module DiasporaFederation
|
|||
|
||||
# some of this is from Rails "Inflector.demodulize" and "Inflector.undersore"
|
||||
def self.entity_name
|
||||
word = name.dup
|
||||
i = word.rindex("::")
|
||||
word = word[(i + 2)..-1] if i
|
||||
|
||||
word.gsub!("::", "/")
|
||||
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
|
||||
word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
|
||||
word.tr!("-", "_")
|
||||
word.downcase!
|
||||
word
|
||||
name.rpartition("::").last.tap do |word|
|
||||
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
|
||||
word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
|
||||
word.tr!("-", "_")
|
||||
word.downcase!
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
|
@ -122,30 +118,28 @@ module DiasporaFederation
|
|||
# @return [Nokogiri::XML::Element] root node
|
||||
def entity_xml
|
||||
doc = Nokogiri::XML::DocumentFragment.new(Nokogiri::XML::Document.new)
|
||||
root_element = Nokogiri::XML::Element.new(self.class.entity_name, doc)
|
||||
|
||||
self.class.class_props.each do |prop_def|
|
||||
name = prop_def[:name]
|
||||
type = prop_def[:type]
|
||||
if type == String
|
||||
root_element << simple_node(doc, name)
|
||||
else
|
||||
# call #to_xml for each item and append to root
|
||||
[*send(name)].compact.each do |item|
|
||||
root_element << item.to_xml
|
||||
Nokogiri::XML::Element.new(self.class.entity_name, doc).tap do |root_element|
|
||||
self.class.class_props.each do |prop_def|
|
||||
name = prop_def[:name]
|
||||
type = prop_def[:type]
|
||||
if type == String
|
||||
root_element << simple_node(doc, name)
|
||||
else
|
||||
# call #to_xml for each item and append to root
|
||||
[*send(name)].compact.each do |item|
|
||||
root_element << item.to_xml
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
root_element
|
||||
end
|
||||
|
||||
# create simple node, fill it with text and append to root
|
||||
def simple_node(doc, name)
|
||||
node = Nokogiri::XML::Element.new(name.to_s, doc)
|
||||
data = send(name).to_s
|
||||
node.content = data unless data.empty?
|
||||
node
|
||||
Nokogiri::XML::Element.new(name.to_s, doc).tap do |node|
|
||||
data = send(name).to_s
|
||||
node.content = data unless data.empty?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue