Module: GrapeDSL::Include::AccessControlEndpoint::Helpers

Defined in:
lib/grape-dsl/ace.rb

Class Method Summary collapse

Class Method Details

.generate_ip_regexp_collection(*args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/grape-dsl/ace.rb', line 8

def generate_ip_regexp_collection *args

  if args.empty?
    raise ArgumentError, "missing ip(s) for allowed sources"
  end

  @cached_regexp_collection ||= {}
  if @cached_regexp_collection[args].nil?
    @cached_regexp_collection= {}

    ip_regex_collection= []
    args.each do |ip_addr|

      ip_regex_builder= [[],[],[],[]]

      #ip_addr.to_s.check(/([0-9\*]{1,3}\.){3}([0-9\*]{1,3})/)#(/([0-9\*]{1,3}\.){3}(0|\*)$/)
      if (ip_addr.to_s =~ /([0-9\*]{1,3}\.){3}([0-9\*]{1,3})/).nil? ? false : true

        ip_addr_index= 0
        ip_addr.split('.').each do |ip_addr_part|

          # 0.0.0.0
          # 255.255.255.255

          if ip_addr_part.include?("*")
            ip_regex_builder[ip_addr_index]= "([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])"
          else
            ip_regex_builder[ip_addr_index].push(ip_addr_part)
          end

          # increment index
          ip_addr_index += 1

        end

      else
        next
      end

      ip_regex_builder.map! do |element|

        case true

          when element.class <= Regexp
            element.inspect[1..(element.inspect.length-2)]

          when element.class <= String
            element

          when element.class <= Array
            "(#{element.join('|')})"

          else
            element.to_s

        end

      end

      ip_regex_collection.push /#{ip_regex_builder.join('\.')}/

    end

    @cached_regexp_collection[args]= ip_regex_collection

  end

  return @cached_regexp_collection[args]

end