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