From: František Dvořák Date: Wed, 7 Sep 2016 14:57:31 +0000 (+0200) Subject: Apply more rubocop style checks. X-Git-Url: http://scientific.zcu.cz/git/?a=commitdiff_plain;h=0d1282cc34f48d1efb67edb7d40d07d13d98d859;p=now.git Apply more rubocop style checks. --- diff --git a/.rubocop.yml b/.rubocop.yml index 41683a4..c2bc902 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -12,67 +12,21 @@ AllCops: Metrics: Enabled: false -# Use the return of the conditional for variable assignment and comparison -# (wtf) -Style/ConditionalAssignment: - Enabled: false - -# Favor format over sprintf -# (easy to read) -Style/FormatString: - Enabled: false - # Do not introduce global variables # (do want) Style/GlobalVars: Enabled: false -# Extra empty line detected at class body end -# (easy to read) -Style/EmptyLinesAroundClassBody: - Enabled: false - -# Extra empty line detected at module body beginning -# Extra empty line detected at module body end -# (easy to read) -Style/EmptyLinesAroundModuleBody: - Enabled: false - -# Favor modifier if usage when having a single-line body -# (easy to read) -Style/IfUnlessModifier: - Enabled: false - # Missing space after # # (debugging) Style/LeadingCommentSpace: Enabled: false -# Do not use parentheses for method calls with no arguments -# (easy to read) -Style/MethodCallParentheses: - Enabled: false - -# Freeze mutable objects assigned to constants -# (easy to read) -Style/MutableConstant: - Enabled: false - -# Favor unless over if for negative conditions -# (easy to read) -Style/NegatedIf: - Enabled: false - # Redundant return detected # (easy to read) Style/RedundantReturn: Enabled: false -# Avoid comma after the last parameter of a method call. -# (do want) -Style/TrailingCommaInArguments: - Enabled: false - # Avoid comma after the last item of an array # (do want) Style/TrailingCommaInLiteral: diff --git a/application.rb b/application.rb index 3c03db5..b8fddbe 100644 --- a/application.rb +++ b/application.rb @@ -13,6 +13,6 @@ require './lib/api' $logger = Logger.new(STDOUT) $logger.formatter = proc do |severity, datetime, _progname, msg| date_format = datetime.strftime('%Y-%m-%dT%H:%M:%S%z') - sprintf "[#{date_format}] %5s: #{msg}\n", severity + format("[#{date_format}] %5{severity}: #{msg}\n", severity: severity) end -$config = Now::Config.new() +$config = Now::Config.new diff --git a/lib/api.rb b/lib/api.rb index 1cff446..2f7da49 100644 --- a/lib/api.rb +++ b/lib/api.rb @@ -27,7 +27,7 @@ module Now helpers do def switch_user(user) if user.nil? - nebula.switch_server() + nebula.switch_server else nebula.switch_user(user) end @@ -62,6 +62,5 @@ module Now halt e.code, e.message end end - end end diff --git a/lib/config.rb b/lib/config.rb index 86b4b3f..d34e010 100644 --- a/lib/config.rb +++ b/lib/config.rb @@ -2,12 +2,11 @@ require 'logger' require 'yaml' module Now - CONFIG_FILES = [ ::File.expand_path('~/.config/now.yml'), '/etc/now.yml', ::File.expand_path('../../etc/now.yml', __FILE__), - ] + ].freeze # Config class for NOW class Config < Hash @@ -36,6 +35,5 @@ module Now replace config end - end end diff --git a/lib/error.rb b/lib/error.rb index 4f24575..e348d31 100644 --- a/lib/error.rb +++ b/lib/error.rb @@ -1,5 +1,4 @@ module Now - # The main exception class for NOW. class NowError < StandardError attr_accessor :code @@ -7,7 +6,5 @@ module Now def initialize(code) @code = code end - end - end diff --git a/lib/nebula.rb b/lib/nebula.rb index a6a5952..4eee867 100644 --- a/lib/nebula.rb +++ b/lib/nebula.rb @@ -3,7 +3,6 @@ require 'yaml' require 'ipaddress' module Now - EXPIRE_LENGTH = 8 * 60 * 60 # NOW core class for communication with OpenNebula @@ -106,9 +105,7 @@ module Now end def check(return_code) - if !OpenNebula.is_error?(return_code) - return true - end + return true unless OpenNebula.is_error?(return_code) code = error_one2http(return_code.errno) raise NowError.new(code), return_code.message @@ -127,18 +124,14 @@ module Now raise NowError.new(422), "Missing 'IP' in the address range #{id} of network #{vn_id}" end address = IPAddress ip - if !ip.include? '/' - address.prefix = 24 - end + address.prefix = 24 unless ip.include? '/' when 'IP6', 'IP4_6' ip = ar['GLOBAL_PREFIX'] || ar['ULA_PREFIX'] if ip.nil? || ip.empty? raise NowError.new(422), "Missing 'GLOBAL_PREFIX' in the address range #{id} of network #{vn_id}" end address = IPAddress ip - if !ip.include? '/' - address.prefix = 64 - end + address.prefix = 64 unless ip.include? '/' when nil if ip.nil? || ip.empty? raise NowError.new(422), "No address range and no NETWORK_ADDRESS in the network #{vn_id}" @@ -149,9 +142,9 @@ module Now end # get the mask from NETWORK_MASK network parameter, if IP not in CIDR notation already - if !ip.include? '/' + unless ip.include? '/' if mask && !mask.empty? - if /\d+\.\d+\.\d+\.\d+/.match(mask) + if /\d+\.\d+\.\d+\.\d+/ =~ mask address.netmask = mask else address.prefix = mask.to_i @@ -166,7 +159,7 @@ module Now def parse_ranges(vn_id, vn) ar = nil vn.each('AR_POOL/AR') do |a| - if !ar.nil? + unless ar.nil? raise NowError.new(501), "Multiple address ranges found in network #{vn_id}" end ar = a @@ -180,7 +173,7 @@ module Now vn.each('CLUSTERS/ID') do |cluster_xml| id = cluster_xml.text logger.debug "[parse_cluster] cluster: #{id}" - if !cluster.nil? + unless cluster.nil? raise NowError.new(501), "Multiple clusters assigned to network #{vn_id}" end cluster = id @@ -194,13 +187,9 @@ module Now id = vn.id title = vn.name desc = vn['SUMMARY'] - if desc.nil? || desc.empty? - desc = nil - end + desc && desc.empty? && desc = nil vlan = vn['VLAN_ID'] - if vlan.nil? || vlan.empty? - vlan = nil - end + vlan && vlan.empty? && vlan = nil range = parse_ranges(id, vn) zone = parse_cluster(id, vn) @@ -212,11 +201,10 @@ module Now bridge: vn['BRIDGE'], vlan: vlan, range: range, - zone: zone, + zone: zone ) return network end - end end diff --git a/lib/server_cipher_auth.rb b/lib/server_cipher_auth.rb index ba2e796..c6a07d3 100644 --- a/lib/server_cipher_auth.rb +++ b/lib/server_cipher_auth.rb @@ -28,17 +28,17 @@ module Now ########################################################################### # Constants with paths to relevant files and defaults ########################################################################### - CIPHER = 'aes-256-cbc' + CIPHER = 'aes-256-cbc'.freeze def initialize(srv_user, srv_passwd) @srv_user = srv_user @srv_passwd = srv_passwd - if srv_passwd.nil? || srv_passwd.empty? - @key = '' - else - @key = ::Digest::SHA1.hexdigest(@srv_passwd) - end + @key = if srv_passwd.nil? || srv_passwd.empty? + '' + else + ::Digest::SHA1.hexdigest(@srv_passwd) + end @cipher = ::OpenSSL::Cipher::Cipher.new(CIPHER) end diff --git a/models/helpers/nowobject.rb b/models/helpers/nowobject.rb index 39b7a3a..9839961 100644 --- a/models/helpers/nowobject.rb +++ b/models/helpers/nowobject.rb @@ -1,8 +1,6 @@ module Now - # Generic hash class with custom accessors and helper methods class NowObject - def initialize(parameters = {}) parameters.select { |_k, v| !v.nil? }.each_pair { |k, v| instance_variable_set("@#{k}", v) } end @@ -23,7 +21,5 @@ module Now value end end - end - end diff --git a/models/network.rb b/models/network.rb index 4589556..44cf15d 100644 --- a/models/network.rb +++ b/models/network.rb @@ -1,7 +1,6 @@ require 'date' module Now - # Network object class Network < NowObject # OpenNebula ID @@ -24,7 +23,7 @@ module Now # IP address range (writer) def range=(new_value) - if !valid_range?(new_value) + unless valid_range?(new_value) raise NowError.new(500), 'Invalid range type' end @range = new_value @@ -37,7 +36,7 @@ module Now attr_accessor :zone def initialize(parameters = {}) - if !parameters.key?(:id) + unless parameters.key?(:id) raise NowError.new(500), 'ID required in network object' end if parameters.key?(:range) && !valid_range?(parameters[:range]) @@ -50,7 +49,7 @@ module Now # @return true if the model is valid def valid? return false if id.nil? - return false if !valid_range?(range) + return false unless valid_range?(range) return true end @@ -93,9 +92,7 @@ module Now h = {} [:id, :title, :description, :user, :vlan, :range, :state, :zone].each do |k| v = instance_variable_get "@#{k}" - if !v.nil? - h[k] = _to_hash(v) - end + v.nil? || h[k] = _to_hash(v) end return h @@ -107,5 +104,4 @@ module Now value.nil? || value.is_a?(Now::Range) end end - end diff --git a/models/range.rb b/models/range.rb index bf60019..20b49b3 100644 --- a/models/range.rb +++ b/models/range.rb @@ -1,16 +1,14 @@ require 'ipaddress' module Now - # Address range class Range < NowObject - # Address range in CIDR notation (reader) attr_reader :address # Address range in CIDR notation (writer) def address=(new_value) - if !valid_address?(new_value) + unless valid_address?(new_value) raise NowError.new(500), 'Internal error: Invalid IP network address' end @address = new_value @@ -20,10 +18,10 @@ module Now attr_accessor :allocation def initialize(parameters = {}) - if !parameters.key?(:address) + unless parameters.key?(:address) raise NowError.new(500), 'Internal error: IP network address required' end - if !valid_address?(parameters[:address]) + unless valid_address?(parameters[:address]) raise NowError.new(500), 'Internal error: Invalid IP network address' end super @@ -32,7 +30,7 @@ module Now # Check to see if the all the properties in the model are valid # @return true if the model is valid def valid? - return false if !valid_address?(address) + return false unless valid_address?(address) return true end @@ -67,12 +65,8 @@ module Now # @return [Hash] Returns the object in the form of hash def to_hash h = {} - if !address.nil? - h[:address] = "#{address}/#{address.prefix}" - end - if !allocation.nil? - h[:allocation] = allocation - end + address.nil? || h[:address] = "#{address}/#{address.prefix}" + allocation.nil? || h[:allocation] = allocation return h end @@ -82,7 +76,5 @@ module Now def valid_address?(value) !value.nil? && value.is_a?(IPAddress) end - end - end diff --git a/test/launch_test.rb b/test/launch_test.rb index 7dee70f..533d927 100644 --- a/test/launch_test.rb +++ b/test/launch_test.rb @@ -11,5 +11,4 @@ class LaunchTest < Minitest::Test get '/' assert_equal Now::API_VERSION, last_response.body end - end diff --git a/version.rb b/version.rb index 7f4b2c4..a133f8e 100644 --- a/version.rb +++ b/version.rb @@ -1,4 +1,4 @@ module Now - VERSION = '0.0.1' - API_VERSION = '0.0.0' + VERSION = '0.0.1'.freeze + API_VERSION = '0.0.0'.freeze end