Class: Coppertone::Mechanism::IPMechanism

Inherits:
Coppertone::Mechanism show all
Defined in:
lib/coppertone/mechanism/ip_mechanism.rb

Overview

Implements the ip4 mechanism.

Direct Known Subclasses

IP4, IP6

Constant Summary collapse

LEADING_ZEROES_IN_CIDR_REGEXP =
%r{\/0\d}
IP_PARSE_ERROR =

Hack for JRuby - remove when JRuby moves to 2.0.x

if RUBY_VERSION < '2.0'
  ArgumentError
else
  IPAddr::Error
end

Instance Attribute Summary collapse

Attributes inherited from Coppertone::Mechanism

#arguments

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Coppertone::Mechanism

build, class_builder, #context_dependent?, dns_lookup_term?, #dns_lookup_term?, #includes_ptr?, register, #to_s

Constructor Details

#initialize(attributes) ⇒ IPMechanism

Returns a new instance of IPMechanism.



10
11
12
13
14
15
16
17
# File 'lib/coppertone/mechanism/ip_mechanism.rb', line 10

def initialize(attributes)
  super(attributes)
  unless attributes.blank?
    attributes = attributes[1..-1] if attributes[0] == ':'
    @netblock, @cidr_length = parse_netblock(attributes)
  end
  raise Coppertone::InvalidMechanismError if @netblock.nil?
end

Instance Attribute Details

#cidr_lengthObject (readonly)

Returns the value of attribute cidr_length.



5
6
7
# File 'lib/coppertone/mechanism/ip_mechanism.rb', line 5

def cidr_length
  @cidr_length
end

#netblockObject (readonly)

Returns the value of attribute netblock.



5
6
7
# File 'lib/coppertone/mechanism/ip_mechanism.rb', line 5

def netblock
  @netblock
end

Class Method Details

.create(attributes) ⇒ Object



6
7
8
# File 'lib/coppertone/mechanism/ip_mechanism.rb', line 6

def self.create(attributes)
  new(attributes)
end

Instance Method Details

#==(other) ⇒ Object



55
56
57
58
# File 'lib/coppertone/mechanism/ip_mechanism.rb', line 55

def ==(other)
  return false unless other.instance_of? self.class
  netblock == other.netblock
end

#default_cidr(network) ⇒ Object



44
45
46
# File 'lib/coppertone/mechanism/ip_mechanism.rb', line 44

def default_cidr(network)
  network.ipv6? ? 128 : 32
end

#match?(macro_context, _request_context) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
# File 'lib/coppertone/mechanism/ip_mechanism.rb', line 48

def match?(macro_context, _request_context)
  ip = ip_for_match(macro_context)
  return false unless ip
  return false unless ip.ipv4? == @netblock.ipv4?
  @netblock.include?(ip)
end

#parse_netblock(ip_as_s) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/coppertone/mechanism/ip_mechanism.rb', line 32

def parse_netblock(ip_as_s)
  validate_no_leading_zeroes_in_cidr(ip_as_s)
  addr, cidr_length_as_s, dual = ip_as_s.split('/')
  return [nil, nil] if dual
  network = IPAddr.new(addr)
  network = network.mask(cidr_length_as_s.to_i) unless cidr_length_as_s.blank?
  cidr_length = cidr_length_as_s.blank? ? default_cidr(network) : cidr_length_as_s.to_i
  [network, cidr_length]
rescue IP_PARSE_ERROR
  [nil, nil]
end

#validate_no_leading_zeroes_in_cidr(ip_as_s) ⇒ Object



20
21
22
23
# File 'lib/coppertone/mechanism/ip_mechanism.rb', line 20

def validate_no_leading_zeroes_in_cidr(ip_as_s)
  return unless LEADING_ZEROES_IN_CIDR_REGEXP.match(ip_as_s)
  raise Coppertone::InvalidMechanismError
end