discourse/lib/freedom_patches/ip_addr.rb
Jarek Radosz 694b5f108b
DEV: Fix various rubocop lints (#24749)
These (21 + 3 from previous PRs) are soon to be enabled in rubocop-discourse:

Capybara/VisibilityMatcher
Lint/DeprecatedOpenSSLConstant
Lint/DisjunctiveAssignmentInConstructor
Lint/EmptyConditionalBody
Lint/EmptyEnsure
Lint/LiteralInInterpolation
Lint/NonLocalExitFromIterator
Lint/ParenthesesAsGroupedExpression
Lint/RedundantCopDisableDirective
Lint/RedundantRequireStatement
Lint/RedundantSafeNavigation
Lint/RedundantStringCoercion
Lint/RedundantWithIndex
Lint/RedundantWithObject
Lint/SafeNavigationChain
Lint/SafeNavigationConsistency
Lint/SelfAssignment
Lint/UnreachableCode
Lint/UselessMethodDefinition
Lint/Void

Previous PRs:
Lint/ShadowedArgument
Lint/DuplicateMethods
Lint/BooleanSymbol
RSpec/SpecFilePathSuffix
2023-12-06 23:25:00 +01:00

36 lines
680 B
Ruby

# frozen_string_literal: true
class IPAddr
def self.handle_wildcards(val)
return if val.blank?
num_wildcards = val.count("*")
return val if num_wildcards == 0
# strip ranges like "/16" from the end if present
v = val.gsub(%r{/.*}, "")
return if v[v.index("*")..-1] =~ /[^\.\*]/
parts = v.split(".")
(4 - parts.size).times { parts << "*" } # support strings like 192.*
v = parts.join(".")
"#{v.tr("*", "0")}/#{32 - (v.count("*") * 8)}"
end
def to_cidr_s
if @addr
mask = @mask_addr.to_s(2).count("1")
if mask == 32
to_s
else
"#{self}/#{mask}"
end
else
nil
end
end
end