Class: SocksHandler::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/socks_handler/rule.rb

Direct Known Subclasses

DirectAccessRule, ProxyAccessRule

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host_patterns:, host: nil, port: nil, username: nil, password: nil) ⇒ Rule

Returns a new instance of Rule.

Parameters:

  • host_patterns (Array<String, Regexp>)
  • host (String, nil) (defaults to: nil)
  • port (Integer, nil) (defaults to: nil)
  • username (String, nil) (defaults to: nil)
  • password (String, nil) (defaults to: nil)


28
29
30
31
32
33
34
# File 'lib/socks_handler/rule.rb', line 28

def initialize(host_patterns:, host: nil, port: nil, username: nil, password: nil)
  @host = host
  @port = port
  @username = username
  @password = password
  self.host_patterns = host_patterns
end

Instance Attribute Details

#host ⇒ String? (readonly)

Returns:

  • (String, nil)


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
# File 'lib/socks_handler/rule.rb', line 14

class Rule
  attr_reader :host, :port, :username, :password, :host_patterns

  # @return [Rule]
  def self.new(**kwargs)
    raise "Rule is an abstract class" if self == Rule
    super
  end

  # @param host_patterns [Array<String, Regexp>]
  # @param host [String, nil]
  # @param port [Integer, nil]
  # @param username [String, nil]
  # @param password [String, nil]
  def initialize(host_patterns:, host: nil, port: nil, username: nil, password: nil)
    @host = host
    @port = port
    @username = username
    @password = password
    self.host_patterns = host_patterns
  end

  # @return [Boolean]
  def direct
    raise NotImplementedError
  end

  # @param value [Array<String, Regexp>]
  # @return [Array<String, Regexp>]
  def host_patterns=(value)
    raise ArgumentError, "host_patterns is not an array" unless value.is_a?(Array)
    @host_patterns = value.frozen? ? value : value.dup.freeze
    @remote_host_pattern_regex = Regexp.union(convert_regexps(value))
    value
  end

  # @param remote_host [String]
  # @return [Boolean]
  def match?(remote_host)
    @remote_host_pattern_regex.match?(remote_host)
  end

  private

  # @param value [Array<String, Regexp>]
  # @return [Array<Regexp>]
  def convert_regexps(value)
    value.map do |v|
      v.is_a?(Regexp) ? v : Regexp.new("\\A#{Regexp.escape(v)}\\z")
    end
  end
end

#host_patterns ⇒ Array<String, Regexp>

Returns:

  • (Array<String, Regexp>)


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
# File 'lib/socks_handler/rule.rb', line 14

class Rule
  attr_reader :host, :port, :username, :password, :host_patterns

  # @return [Rule]
  def self.new(**kwargs)
    raise "Rule is an abstract class" if self == Rule
    super
  end

  # @param host_patterns [Array<String, Regexp>]
  # @param host [String, nil]
  # @param port [Integer, nil]
  # @param username [String, nil]
  # @param password [String, nil]
  def initialize(host_patterns:, host: nil, port: nil, username: nil, password: nil)
    @host = host
    @port = port
    @username = username
    @password = password
    self.host_patterns = host_patterns
  end

  # @return [Boolean]
  def direct
    raise NotImplementedError
  end

  # @param value [Array<String, Regexp>]
  # @return [Array<String, Regexp>]
  def host_patterns=(value)
    raise ArgumentError, "host_patterns is not an array" unless value.is_a?(Array)
    @host_patterns = value.frozen? ? value : value.dup.freeze
    @remote_host_pattern_regex = Regexp.union(convert_regexps(value))
    value
  end

  # @param remote_host [String]
  # @return [Boolean]
  def match?(remote_host)
    @remote_host_pattern_regex.match?(remote_host)
  end

  private

  # @param value [Array<String, Regexp>]
  # @return [Array<Regexp>]
  def convert_regexps(value)
    value.map do |v|
      v.is_a?(Regexp) ? v : Regexp.new("\\A#{Regexp.escape(v)}\\z")
    end
  end
end

#password ⇒ String? (readonly)

Returns:

  • (String, nil)


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
# File 'lib/socks_handler/rule.rb', line 14

class Rule
  attr_reader :host, :port, :username, :password, :host_patterns

  # @return [Rule]
  def self.new(**kwargs)
    raise "Rule is an abstract class" if self == Rule
    super
  end

  # @param host_patterns [Array<String, Regexp>]
  # @param host [String, nil]
  # @param port [Integer, nil]
  # @param username [String, nil]
  # @param password [String, nil]
  def initialize(host_patterns:, host: nil, port: nil, username: nil, password: nil)
    @host = host
    @port = port
    @username = username
    @password = password
    self.host_patterns = host_patterns
  end

  # @return [Boolean]
  def direct
    raise NotImplementedError
  end

  # @param value [Array<String, Regexp>]
  # @return [Array<String, Regexp>]
  def host_patterns=(value)
    raise ArgumentError, "host_patterns is not an array" unless value.is_a?(Array)
    @host_patterns = value.frozen? ? value : value.dup.freeze
    @remote_host_pattern_regex = Regexp.union(convert_regexps(value))
    value
  end

  # @param remote_host [String]
  # @return [Boolean]
  def match?(remote_host)
    @remote_host_pattern_regex.match?(remote_host)
  end

  private

  # @param value [Array<String, Regexp>]
  # @return [Array<Regexp>]
  def convert_regexps(value)
    value.map do |v|
      v.is_a?(Regexp) ? v : Regexp.new("\\A#{Regexp.escape(v)}\\z")
    end
  end
end

#port ⇒ Integer? (readonly)

Returns:

  • (Integer, nil)


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
# File 'lib/socks_handler/rule.rb', line 14

class Rule
  attr_reader :host, :port, :username, :password, :host_patterns

  # @return [Rule]
  def self.new(**kwargs)
    raise "Rule is an abstract class" if self == Rule
    super
  end

  # @param host_patterns [Array<String, Regexp>]
  # @param host [String, nil]
  # @param port [Integer, nil]
  # @param username [String, nil]
  # @param password [String, nil]
  def initialize(host_patterns:, host: nil, port: nil, username: nil, password: nil)
    @host = host
    @port = port
    @username = username
    @password = password
    self.host_patterns = host_patterns
  end

  # @return [Boolean]
  def direct
    raise NotImplementedError
  end

  # @param value [Array<String, Regexp>]
  # @return [Array<String, Regexp>]
  def host_patterns=(value)
    raise ArgumentError, "host_patterns is not an array" unless value.is_a?(Array)
    @host_patterns = value.frozen? ? value : value.dup.freeze
    @remote_host_pattern_regex = Regexp.union(convert_regexps(value))
    value
  end

  # @param remote_host [String]
  # @return [Boolean]
  def match?(remote_host)
    @remote_host_pattern_regex.match?(remote_host)
  end

  private

  # @param value [Array<String, Regexp>]
  # @return [Array<Regexp>]
  def convert_regexps(value)
    value.map do |v|
      v.is_a?(Regexp) ? v : Regexp.new("\\A#{Regexp.escape(v)}\\z")
    end
  end
end

#username ⇒ String? (readonly)

Returns:

  • (String, nil)


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
# File 'lib/socks_handler/rule.rb', line 14

class Rule
  attr_reader :host, :port, :username, :password, :host_patterns

  # @return [Rule]
  def self.new(**kwargs)
    raise "Rule is an abstract class" if self == Rule
    super
  end

  # @param host_patterns [Array<String, Regexp>]
  # @param host [String, nil]
  # @param port [Integer, nil]
  # @param username [String, nil]
  # @param password [String, nil]
  def initialize(host_patterns:, host: nil, port: nil, username: nil, password: nil)
    @host = host
    @port = port
    @username = username
    @password = password
    self.host_patterns = host_patterns
  end

  # @return [Boolean]
  def direct
    raise NotImplementedError
  end

  # @param value [Array<String, Regexp>]
  # @return [Array<String, Regexp>]
  def host_patterns=(value)
    raise ArgumentError, "host_patterns is not an array" unless value.is_a?(Array)
    @host_patterns = value.frozen? ? value : value.dup.freeze
    @remote_host_pattern_regex = Regexp.union(convert_regexps(value))
    value
  end

  # @param remote_host [String]
  # @return [Boolean]
  def match?(remote_host)
    @remote_host_pattern_regex.match?(remote_host)
  end

  private

  # @param value [Array<String, Regexp>]
  # @return [Array<Regexp>]
  def convert_regexps(value)
    value.map do |v|
      v.is_a?(Regexp) ? v : Regexp.new("\\A#{Regexp.escape(v)}\\z")
    end
  end
end

Class Method Details

.new(**kwargs) ⇒ Rule

Returns:



18
19
20
21
# File 'lib/socks_handler/rule.rb', line 18

def self.new(**kwargs)
  raise "Rule is an abstract class" if self == Rule
  super
end

Instance Method Details

#direct ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


37
38
39
# File 'lib/socks_handler/rule.rb', line 37

def direct
  raise NotImplementedError
end

#match?(remote_host) ⇒ Boolean

Parameters:

  • remote_host (String)

Returns:

  • (Boolean)


52
53
54
# File 'lib/socks_handler/rule.rb', line 52

def match?(remote_host)
  @remote_host_pattern_regex.match?(remote_host)
end