Class: Nexpose::IPRange
- Inherits:
-
Object
- Object
- Nexpose::IPRange
- Includes:
- Comparable
- Defined in:
- lib/nexpose/site.rb
Overview
Object that represents a single IP address or an inclusive range of IP addresses. If to is nil then the from field will be used to specify a single IP Address only.
Instance Attribute Summary collapse
-
#from ⇒ Object
Start of range *Required.
-
#to ⇒ Object
End of range *Optional (If nil then IPRange is a single IP Address).
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #as_xml ⇒ Object (also: #to_xml_elem)
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
- #include?(single_ip) ⇒ Boolean
-
#initialize(from, to = nil) ⇒ IPRange
constructor
A new instance of IPRange.
-
#size ⇒ Fixnum
Size of the IP range.
- #to_xml ⇒ Object
Constructor Details
#initialize(from, to = nil) ⇒ IPRange
Returns a new instance of IPRange.
559 560 561 562 |
# File 'lib/nexpose/site.rb', line 559 def initialize(from, to = nil) @from = from @to = to unless from == to end |
Instance Attribute Details
#from ⇒ Object
Start of range *Required
555 556 557 |
# File 'lib/nexpose/site.rb', line 555 def from @from end |
#to ⇒ Object
End of range *Optional (If nil then IPRange is a single IP Address)
557 558 559 |
# File 'lib/nexpose/site.rb', line 557 def to @to end |
Instance Method Details
#<=>(other) ⇒ Object
578 579 580 581 582 583 584 585 586 587 588 589 590 591 |
# File 'lib/nexpose/site.rb', line 578 def <=>(other) return 1 unless other.respond_to? :from from = IPAddr.new(@from) to = @to.nil? ? from : IPAddr.new(@to) cf_from = IPAddr.new(other.from) cf_to = IPAddr.new(other.to.nil? ? other.from : other.to) if cf_to < from 1 elsif to < cf_from -1 else # Overlapping 0 end end |
#==(other) ⇒ Object
593 594 595 |
# File 'lib/nexpose/site.rb', line 593 def ==(other) eql?(other) end |
#as_xml ⇒ Object Also known as: to_xml_elem
621 622 623 624 625 |
# File 'lib/nexpose/site.rb', line 621 def as_xml xml = REXML::Element.new('range') xml.add_attributes({ 'from' => @from, 'to' => @to }) xml end |
#eql?(other) ⇒ Boolean
597 598 599 600 |
# File 'lib/nexpose/site.rb', line 597 def eql?(other) return false unless other.respond_to? :from @from == other.from && @to == other.to end |
#hash ⇒ Object
617 618 619 |
# File 'lib/nexpose/site.rb', line 617 def hash to_xml.hash end |
#include?(single_ip) ⇒ Boolean
602 603 604 605 606 607 608 609 610 611 612 613 614 615 |
# File 'lib/nexpose/site.rb', line 602 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 end |
#size ⇒ Fixnum
Size of the IP range. The total number of IP addresses represented by this range.
569 570 571 572 573 574 |
# File 'lib/nexpose/site.rb', line 569 def size return 1 if @to.nil? from = IPAddr.new(@from) to = IPAddr.new(@to) (from..to).to_a.size end |
#to_xml ⇒ Object
628 629 630 |
# File 'lib/nexpose/site.rb', line 628 def to_xml as_xml.to_s end |