Skip to content

Commit

Permalink
FEATURE: screened IP address range can be entered like 192.* instead …
Browse files Browse the repository at this point in the history
…of 192.*.*.*
  • Loading branch information
nlalonde committed Sep 24, 2014
1 parent b156748 commit d96acde
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
7 changes: 5 additions & 2 deletions app/models/screened_ip_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ def ip_address=(val)
if num_wildcards == 0
write_attribute(:ip_address, val)
else
v = val.gsub(/\/.*/, '')
v = val.gsub(/\/.*/, '') # strip ranges like "/16" from the end if present
if v[v.index('*')..-1] =~ /[^\.\*]/
self.errors.add(:ip_address, :invalid)
return
end
write_attribute(:ip_address, "#{v.gsub('*', '0')}/#{32 - (num_wildcards * 8)}")
parts = v.split('.')
(4 - parts.size).times { parts << '*' } # support strings like 192.*
v = parts.join('.')
write_attribute(:ip_address, "#{v.gsub('*', '0')}/#{32 - (v.count('*') * 8)}")
end

# this gets even messier, Ruby 1.9.2 raised a different exception to Ruby 2.0.0
Expand Down
2 changes: 2 additions & 0 deletions spec/models/screened_ip_address_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ def test_bad_value(arg)
test_good_value("123.12.*.*", "123.12.0.0/16")
test_good_value("123.12.1.*", "123.12.1.0/24")
test_good_value("123.12.*.*/16", "123.12.0.0/16")
test_good_value("123.12.*", "123.12.0.0/16")
test_good_value("123.*", "123.0.0.0/8")
end

it "handles bad input" do
Expand Down

0 comments on commit d96acde

Please sign in to comment.