Class: Iterator

Inherits:
Object
  • Object
show all
Defined in:
lib/ddis/ip_iterate.rb

Overview

Iterate through IPs given a CIDR, start and end IP

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_ip, size) ⇒ Iterator

Returns a new instance of Iterator.



8
9
10
11
12
13
14
15
# File 'lib/ddis/ip_iterate.rb', line 8

def initialize start_ip, size
  @start_ip = start_ip
  @start_si = size

  @ip       = start_ip
  @esize    = 0

end

Instance Attribute Details

#ipObject

Returns the value of attribute ip.



6
7
8
# File 'lib/ddis/ip_iterate.rb', line 6

def ip
  @ip
end

Instance Method Details

#increment_last(ip) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ddis/ip_iterate.rb', line 17

def increment_last ip
  return nil if ip == '255.255.255.255'    # End of the line
  
  octets = ip.split "."
  last = octets[-1]
  
  return "#{increment_last octets[0..-2].join('.')}.0" if last == '255'
  last = Integer(last)
  last += 1
  return "#{octets[0..-2].join('.')}.#{last}"
end

#iterateObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ddis/ip_iterate.rb', line 29

def iterate
  @ip = increment_last @ip
  @esize += 1
  
  if @esize == @start_si
    @esize = @start_si
    @ip    = @start_ip
    return nil
  end
  
  return @ip

end