Class: IPLogic::IP

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/iplogic/ip.rb

Constant Summary collapse

FormatError =
Class.new(ArgumentError)
MAXIP =

255.255.255.255

self.new(0xFFFFFFFF)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(int) ⇒ IP

Returns a new instance of IP.



68
69
70
# File 'lib/iplogic/ip.rb', line 68

def initialize(int)
  @int = int
end

Instance Attribute Details

#intObject (readonly) Also known as: to_i, to_int

-*- instance methods -*-



64
65
66
# File 'lib/iplogic/ip.rb', line 64

def int
  @int
end

Class Method Details

.maxObject



74
75
76
# File 'lib/iplogic/ip.rb', line 74

def self.max
  MAXIP
end

.wrap(arg) ⇒ Object

Basic monad wrapper for IP addresses.

You can pass it:

a String >> IP(‘11.22.33.44’)

> #<IP [ 11.22.33.44 ]>

an Integer >> IP(0xFFFFFFFF)

> #<IP [ 255.255.255.255 ]>

an Array of int-ish objects >> IP([‘11’, 22, 33, ‘44’])

> #<IP [ 11.22.33.44 ]>

nil >> IP(nil)

> #<IP [ 0.0.0.0 ]>

an IP >> ip = IP(‘11.22.33.44’)

> #<IP [ 11.22.33.44 ]>

>> IP(ip)

> #<IP [ 11.22.33.44 ]>

>> ip.object_id == IP(ip).object_id

> true



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/iplogic/ip.rb', line 32

def wrap(arg)
  return arg if arg.is_a? IP

  int = case arg
  when Array
    parts_to_int(arg)
  when String
    parts_to_int(arg.split('.'))
  when Fixnum
    arg
  when nil
    0
  else
    raise FormatError, "IP: Unable to parse #{arg.inspect}"
  end

  return new(int)
end

Instance Method Details

#+(int_ish) ⇒ Object



108
109
110
# File 'lib/iplogic/ip.rb', line 108

def +(int_ish)
  IP.wrap(int + int_ish.to_i)
end

#-(int_ish) ⇒ Object



117
118
119
# File 'lib/iplogic/ip.rb', line 117

def -(int_ish)
  self + (-int_ish.to_i)
end

#<=>(other) ⇒ Object



104
105
106
# File 'lib/iplogic/ip.rb', line 104

def <=>(other)
  self.int <=> other.int
end

#assert_netmask!Object

Raises:

  • (ArgumentError)


142
143
144
145
146
# File 'lib/iplogic/ip.rb', line 142

def assert_netmask!
  raise ArgumentError, <<-msg.strip unless netmask?
    #{self.inspect} is not a valid netmask.
  msg
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


98
99
100
# File 'lib/iplogic/ip.rb', line 98

def eql?(other)
  self.int == IP.wrap(other).int
end

#hashObject



94
95
96
# File 'lib/iplogic/ip.rb', line 94

def hash
  self.to_s.hash
end

#inspectObject



90
91
92
# File 'lib/iplogic/ip.rb', line 90

def inspect
  "#<IP [ #{self} ]>"
end

#max(netmask) ⇒ Object



126
127
128
# File 'lib/iplogic/ip.rb', line 126

def max(netmask)
  CIDR.wrap(self, netmask).max
end

#netmask?Boolean

Returns:

  • (Boolean)


135
136
137
138
139
140
# File 'lib/iplogic/ip.rb', line 135

def netmask?
  maxint32 = 0xFFFFFFFF
  (0..32).any? do |i|
    (int-1) + (1 << i) == maxint32
  end
end

#partsObject



78
79
80
81
82
83
# File 'lib/iplogic/ip.rb', line 78

def parts
  @parts ||= begin
    rad = int.radix(256)
    [0]*([4-rad.size,0].max) + rad
  end
end

#prefix(netmask) ⇒ Object Also known as: min



121
122
123
# File 'lib/iplogic/ip.rb', line 121

def prefix(netmask)
  CIDR.wrap(self, netmask).min
end

#rest_field(netmask) ⇒ Object Also known as: rest



130
131
132
# File 'lib/iplogic/ip.rb', line 130

def rest_field(netmask)
  CIDR.wrap(self, netmask).rest_field
end

#succObject

This allows IP “ranges” with (ip1..ip2)



113
114
115
# File 'lib/iplogic/ip.rb', line 113

def succ
  self + 1
end

#to_sObject Also known as: to_str



85
86
87
# File 'lib/iplogic/ip.rb', line 85

def to_s
  parts.join('.')
end