Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove asset from range #156

Closed
wants to merge 10 commits into from
58 changes: 46 additions & 12 deletions lib/nexpose/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,50 @@ def add_ip(ip)
#
# @param [String] ip IP address of an asset.
def remove_ip(ip)
@assets = assets.reject { |asset| asset == IPRange.new(ip) }
ip = IPRange.new(ip)
@assets.each do |asset_range|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No reason to use @assets instead of the assets getter method.

next if asset_range.is_a?(Nexpose::HostName)
if asset_range.include?(ip)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about an early return (next if like above) here since there is no alternative?

asset = split_ip_range(asset_range, ip)
@assets.delete(asset_range)
@assets.push(asset)
@assets.flatten!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of calling Array#flatten!, you can use Array#+ which keeps things one-dimensional:

assets += Array(asset)

end
end
end


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra blank line detected.

def split_ip_range(ip_range, split_ip)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assignment Branch Condition size for split_ip_range is too high. [25.16/15]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assignment Branch Condition size for split_ip_range is too high. [25.16/15]

split_ip = IPAddr.new(split_ip.from)
start_ip, end_ip = IPAddr.new(ip_range.from), IPAddr.new(ip_range.to)
all_ip_range = (start_ip..end_ip)

case split_ip
when all_ip_range.min
new_start = IPAddr.new(start_ip.to_i + 1, start_ip.family).to_s
asset = Nexpose::IPRange.new(new_start, ip_range.to)
when all_ip_range.max
new_end = IPAddr.new(end_ip.to_i - 1, end_ip.family).to_s
asset = Nexpose::IPRange.new(ip_range.from, new_end)
else
asset = ip_range_split_calc(start_ip, end_ip, split_ip)
end
return asset

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant return detected.

end


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra blank line detected.

def ip_range_split_calc(start_ip, end_ip, split_ip)
low_split = IPAddr.new(split_ip.to_i - 1, start_ip.family).to_s

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary spacing detected.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary spacing detected.

high_split = IPAddr.new(split_ip.to_i + 1, end_ip.family).to_s

low_range = Nexpose::IPRange.new(start_ip.to_s, low_split)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary spacing detected.

high_range = Nexpose::IPRange.new(high_split, end_ip.to_s)

return [ low_range, high_range ]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant return detected.
Space inside square brackets detected.

end


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra blank line detected.


Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra blank line detected.

# Adds assets to this site by IP address range.
#
# @param [String] from Beginning IP address of a range.
Expand Down Expand Up @@ -725,17 +766,10 @@ def eql?(other)

def include?(single_ip)
return false unless single_ip.respond_to? :from
from = IPAddr.new(@from)
to = @to.nil? ? from : IPAddr.new(@to)
other = IPAddr.new(single_ip)

if other < from
false
elsif to < other
false
else
true
end
from = IPAddr.new(@from).to_i
to = @to.nil? ? from : IPAddr.new(@to).to_i
other = IPAddr.new(single_ip.from).to_i
(from..to).include?(other)
end

def hash
Expand Down
2 changes: 2 additions & 0 deletions nexpose.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ Gem::Specification.new do |s|
s.add_development_dependency('rubocop', '~> 0.29.0')
s.add_development_dependency('webmock', '~> 1.20.4')
s.add_development_dependency('vcr', '~> 2.9.3')
s.add_development_dependency('pry')
s.add_development_dependency('pry-byebug')
end