class Portfinder::Parser

Argument parser

Constants

Type

Parser match type

Public Class Methods

new() click to toggle source
  # File lib/portfinder/parser.rb
7 def initialize; end

Public Instance Methods

parse_dir_path(_target, _verify = false) click to toggle source
   # File lib/portfinder/parser.rb
45 def parse_dir_path _target, _verify = false
46   raise NotImplementedError, "Implementation pending"
47 end
parse_file_path(_target, _verify = false) click to toggle source
   # File lib/portfinder/parser.rb
41 def parse_file_path _target, _verify = false
42   raise NotImplementedError, "Implementation pending"
43 end
parse_hosts(target) click to toggle source

:reek: FeatureEnvy

   # File lib/portfinder/parser.rb
10 def parse_hosts target
11   type = select_type target, IP4_TYPES
12   return unless type
13 
14   case type.name
15   when :ip
16     type.match[:ip]
17   when :range
18     ip4_range type.match[:start], type.match[:limit].to_i
19   when :selection
20     target.split ","
21   when :network
22     ip4_network_hosts type.match[:network], type.match[:net_bits].to_i
23   end
24 end
parse_ports(target) click to toggle source

:reek: FeatureEnvy

   # File lib/portfinder/parser.rb
27 def parse_ports target
28   type = select_type target, PORT_TYPES
29   return unless type
30 
31   case type.name
32   when :port
33     type.match[:port].to_i
34   when :range
35     type.match[:start].to_i..type.match[:end].to_i
36   when :selection
37     target.split(",").map(&:to_i)
38   end
39 end

Private Instance Methods

ip4_network_hosts(network, net_bits) click to toggle source
   # File lib/portfinder/parser.rb
68 def ip4_network_hosts network, net_bits
69   ip_count = 2**(32 - net_bits)
70   valid_host_count = ip_count < 2 ? 0 : ip_count - 2
71   range = IPAddr.new("#{network}/#{net_bits}").to_range
72   exceptions = [range.first, range.last]
73   Enumerator.new valid_host_count do |host|
74     range.each do |addr|
75       host << addr.to_s unless exceptions.include?(addr)
76     end
77   end
78 end
ip4_range(start, limit) click to toggle source
   # File lib/portfinder/parser.rb
51 def ip4_range start, limit
52   start_offset = start.split(".").last.to_i
53   host_count = (limit - start_offset) + 1
54   host_count_in_range = (1..254).cover? host_count
55   last_host_in_range = (2..254).cover? limit
56 
57   return unless host_count_in_range && last_host_in_range
58 
59   Enumerator.new host_count do |host|
60     IPAddr.new(
61       start + "/24"
62     ).to_range.each_with_index do |addr, index|
63       host << addr.to_s if (start_offset..limit).cover? index
64     end
65   end
66 end
select_type(target, types) click to toggle source
   # File lib/portfinder/parser.rb
80 def select_type target, types
81   selection_type = nil
82 
83   types.each_pair do |type, matcher|
84     match = matcher.match(target)
85     selection_type = Type.new(type, match) if match
86   end
87 
88   selection_type
89 end