From 8ac6bb8492c9a3b9ec5d7b5bf2b35907a1f8c332 Mon Sep 17 00:00:00 2001 From: Azul Date: Sat, 23 Sep 2017 15:41:56 +0200 Subject: style: avoid endless elsif in KeyInfo#error using an array of symbols representing the different checks now. --- lib/nickserver/hkp/key_info.rb | 49 +++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/lib/nickserver/hkp/key_info.rb b/lib/nickserver/hkp/key_info.rb index 568a798..e1a9500 100644 --- a/lib/nickserver/hkp/key_info.rb +++ b/lib/nickserver/hkp/key_info.rb @@ -21,17 +21,12 @@ module Nickserver::Hkp @uids = extract_uids(uid_lines) end + CHECKS = %i[too_short? expired? revoked? disabled? outdated?].freeze + def error - if keylen < 2048 - 'key length is too short.' - elsif expired? - 'key expired.' - elsif revoked? - 'key revoked.' - elsif disabled? - 'key disabled.' - elsif expirationdate && expirationdate < Time.now - 'key expired' + CHECKS.find do |check| + msg = check.to_s.chop.tr('_', ' ') + "key is #{msg}." if send(check) end end @@ -69,18 +64,6 @@ module Nickserver::Hkp algo == '17' end - def revoked? - flags =~ /r/ - end - - def disabled? - flags =~ /d/ - end - - def expired? - flags =~ /e/ - end - protected attr_reader :properties @@ -93,5 +76,27 @@ module Nickserver::Hkp CGI.unescape(uid.sub(/.*<(.+)>.*/, '\1')) end end + + # CHECKS + + def too_short? + keylen < 2048 + end + + def expired? + flags =~ /e/ + end + + def revoked? + flags =~ /r/ + end + + def disabled? + flags =~ /d/ + end + + def outdated? + expirationdate && expirationdate < Time.now + end end end -- cgit v1.2.3