make some methods less complex and adjust rubocop-rules

This commit is contained in:
Benjamin Neff 2015-06-20 19:12:18 +02:00
parent c950e7a94b
commit 5ac7a14b1e
3 changed files with 42 additions and 26 deletions

View file

@ -18,9 +18,6 @@ Metrics/MethodLength:
Metrics/ClassLength:
Max: 1500
Metrics/ModuleLength:
Max: 1500
# No space makes the method definition shorter and differentiates
# from a regular assignment.
Style/SpaceAroundEqualsInParameterDefault:
@ -142,3 +139,19 @@ Lint/Debugger:
# Reset some HoundCI changes back to Rubocop defaults
Style/DotPosition:
EnforcedStyle: leading
# diaspora_federation rules
Rails/TimeZone:
Exclude:
- "spec/**/*"
Metrics/CyclomaticComplexity:
Max: 10
Metrics/PerceivedComplexity:
Max: 10
Metrics/AbcSize:
Max: 20
Metrics/ModuleLength:
Max: 1500

View file

@ -66,12 +66,12 @@ module DiasporaFederation
return logger.warn "table for #{entity} doesn't exist, skip validation" unless entity.table_exists?
methods.each {|method|
valid = entity.respond_to?(method) ||
entity.column_names.include?(method.to_s) ||
entity.method_defined?(method)
raise ConfigurationError, "the configured class #{entity} for #{name} doesn't respond to #{method}" unless valid
}
methods.each {|method| entity_respond_to?(entity, method) }
end
def entity_respond_to?(entity, method)
valid = entity.respond_to?(method) || entity.column_names.include?(method.to_s) || entity.method_defined?(method)
raise ConfigurationError, "the configured class #{entity} for #{name} doesn't respond to #{method}" unless valid
end
end

View file

@ -107,24 +107,24 @@ module DiasporaFederation
# Create a WebFinger instance from the given XML string.
# @param [String] webfinger_xml WebFinger XML string
# @return [WebFinger] WebFinger instance
# @raise [InvalidData] if the given XML string is invalid or incomplete
def self.from_xml(webfinger_xml)
data = XrdDocument.xml_data(webfinger_xml)
raise InvalidData unless xml_data_valid?(data)
data = parse_xml_and_validate(webfinger_xml)
hcard, seed, guid, profile, updates, pubkey = parse_links(data)
hcard_url, seed_url, guid, profile_url, updates_url, pubkey = parse_links(data)
wf = allocate
wf.instance_eval {
@acct_uri = data[:subject]
@alias_url = data[:aliases].first
@hcard_url = hcard[:href]
@seed_url = seed[:href]
@profile_url = profile[:href]
@updates_url = updates[:href]
@hcard_url = hcard_url
@seed_url = seed_url
@profile_url = profile_url
@updates_url = updates_url
# TODO: change me! ##########
@guid = guid[:href]
@pubkey = pubkey[:href]
@guid = guid
@pubkey = pubkey
##############################
}
wf
@ -144,14 +144,17 @@ module DiasporaFederation
end
private_class_method :account_data_complete?
# Does some rudimentary checking on the data Hash produced from parsing the
# XML string
# @param [Hash] data XML data
# @return [Boolean] validation result
def self.xml_data_valid?(data)
data.key?(:subject) && data.key?(:aliases) && data.key?(:links)
# Parses the XML string to a Hash and does some rudimentary checking on
# the data Hash.
# @param [String] webfinger_xml WebFinger XML string
# @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)
raise InvalidData unless data.key?(:subject) && data.key?(:aliases) && data.key?(:links)
data
end
private_class_method :xml_data_valid?
private_class_method :parse_xml_and_validate
def add_links_to(doc)
doc.links << {rel: REL_HCARD,
@ -190,7 +193,7 @@ module DiasporaFederation
updates = parse_link(links, REL_UPDATES)
pubkey = parse_link(links, REL_PUBKEY)
raise InvalidData unless [hcard, seed, guid, profile, updates, pubkey].all?
[hcard, seed, guid, profile, updates, pubkey]
[hcard[:href], seed[:href], guid[:href], profile[:href], updates[:href], pubkey[:href]]
end
private_class_method :parse_links