Class: Baykit::BayServer::Util::HostMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/baykit/bayserver/util/host_matcher.rb

Constant Summary collapse

MATCH_TYPE_ALL =
1
MATCH_TYPE_EXACT =
2
MATCH_TYPE_DOMAIN =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host) ⇒ HostMatcher

Returns a new instance of HostMatcher.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/baykit/bayserver/util/host_matcher.rb', line 13

def initialize(host)
  if host == "*"
    @match_type = MATCH_TYPE_ALL
  elsif host.start_with?("*.")
    @match_type = MATCH_TYPE_DOMAIN
    @domain = host[2, -1]
  else
    @match_type = MATCH_TYPE_EXACT
    @host = host
  end
end

Instance Attribute Details

#domainObject (readonly)

Returns the value of attribute domain.



11
12
13
# File 'lib/baykit/bayserver/util/host_matcher.rb', line 11

def domain
  @domain
end

#hostObject (readonly)

Returns the value of attribute host.



10
11
12
# File 'lib/baykit/bayserver/util/host_matcher.rb', line 10

def host
  @host
end

#match_typeObject (readonly)

Returns the value of attribute match_type.



9
10
11
# File 'lib/baykit/bayserver/util/host_matcher.rb', line 9

def match_type
  @match_type
end

Instance Method Details

#match(remote_host) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/baykit/bayserver/util/host_matcher.rb', line 26

def match(remote_host)
  if @match_type == MATCH_TYPE_ALL
    # all match
    return true
  end

  if remote_host == nil
    return false
  end

  if @match_type == MATCH_TYPE_EXACT
    # exact match
    remote_host == @host
  else
    # domain match
    remote_host.end_with?(@domain)
  end
end