Class: ReservedSubdomain::Literal

Inherits:
Object
  • Object
show all
Defined in:
lib/reserved_subdomain/literal.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subdomain) ⇒ Literal



32
33
34
# File 'lib/reserved_subdomain/literal.rb', line 32

def initialize(subdomain)
  @subdomain = subdomain
end

Instance Attribute Details

#subdomainObject (readonly)

Returns the value of attribute subdomain.



30
31
32
# File 'lib/reserved_subdomain/literal.rb', line 30

def subdomain
  @subdomain
end

Class Method Details

.literalsObject

If the env var is set then we pre-load all the reserved literals into a big array. This makes checking for reserved subdomains quicker but at the expense of keeping the big array in memory.

If you’re only checking for reserved subdomains when a user creates a new account then it may be better to load the literals from the file each time.



11
12
13
# File 'lib/reserved_subdomain/literal.rb', line 11

def literals
  @literals ||= ENV['RESERVED_SUBDOMAIN_PRELOAD'] ? load_literals : nil
end

.load_literalsObject



15
16
17
# File 'lib/reserved_subdomain/literal.rb', line 15

def load_literals
  literals_files.map { |filename| File.read(filename).split("\n") }.compact.flatten
end

Instance Method Details

#reserved?Boolean



36
37
38
# File 'lib/reserved_subdomain/literal.rb', line 36

def reserved?
  literals.include? subdomain
end