Class: MockDnsServer::IpAddressDispenser

Inherits:
Object
  • Object
show all
Defined in:
lib/mock_dns_server/ip_address_dispenser.rb

Overview

Wraps Ruby’s IPAddr class and yields successive IP addresses. Instantiate it with a starting address (IPV4 or IPV6), and when you call .next, it will provide addresses that increment by 1 or optional step parameter, starting with your initial address.

We’ll probably want to instruct it to be more intelligent, e.g. skip .0 and .255.

Instance Method Summary collapse

Constructor Details

#initialize(initial_address = '192.168.1.1') ⇒ IpAddressDispenser

Returns a new instance of IpAddressDispenser.

Parameters:

  • initial_address (defaults to: '192.168.1.1')

    first address dispensed, and basis for subsequent ‘next’ calls



15
16
17
# File 'lib/mock_dns_server/ip_address_dispenser.rb', line 15

def initialize(initial_address = '192.168.1.1')
  @initial_address = initial_address
end

Instance Method Details

#next(step = 1) ⇒ Object

Parameters:

  • step (defaults to: 1)

    number of addresses to step in each next call, defaults to 1



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mock_dns_server/ip_address_dispenser.rb', line 21

def next(step = 1)
  step.times do
    if @address.nil?
      @address = IPAddr.new(@initial_address)
    elsif @address.to_s == '255.255.255.255'
      @address = IPAddr.new('1.1.1.1')
    else
      @address = @address.succ
    end
  end
  @address.to_s
end