Class: SubdomainParser

Inherits:
Object
  • Object
show all
Defined in:
lib/sensible_logging/helpers/subdomain_parser.rb

Overview

Returns subdomains from a full qualified domain name

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tld_length: 1) ⇒ SubdomainParser

Returns a new instance of SubdomainParser.



7
8
9
# File 'lib/sensible_logging/helpers/subdomain_parser.rb', line 7

def initialize(tld_length: 1)
  @tld_length = tld_length
end

Instance Attribute Details

#tld_lengthObject (readonly)

Returns the value of attribute tld_length.



5
6
7
# File 'lib/sensible_logging/helpers/subdomain_parser.rb', line 5

def tld_length
  @tld_length
end

Instance Method Details

#parse(host) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/sensible_logging/helpers/subdomain_parser.rb', line 11

def parse(host)
  domain_parts = host.split('.')

  return domain_parts[0] if domain_parts.size == 1

  main_domain_length = tld_length + 1
  subdomain_length = domain_parts.size - main_domain_length

  subdomain_parts = domain_parts[0...subdomain_length]

  return nil if subdomain_parts.empty?

  subdomain_parts.join('.')
end