Class: Explorer::Hostmap

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/explorer/hostmap.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHostmap

Returns a new instance of Hostmap.



9
10
11
# File 'lib/explorer/hostmap.rb', line 9

def initialize
  @mappings = {}
end

Instance Attribute Details

#mappingsObject (readonly)

Returns the value of attribute mappings.



7
8
9
# File 'lib/explorer/hostmap.rb', line 7

def mappings
  @mappings
end

Instance Method Details

#add(domain, host, port) ⇒ Object



13
14
15
# File 'lib/explorer/hostmap.rb', line 13

def add domain, host, port
  @mappings[domain] = { host: host, port: port }
end

#load(file) ⇒ Object



37
38
39
40
41
42
# File 'lib/explorer/hostmap.rb', line 37

def load file
  return unless File.exist? file

  yaml = YAML.load_file file
  @mappings = yaml
end

#remove(domain) ⇒ Object



17
18
19
# File 'lib/explorer/hostmap.rb', line 17

def remove domain
  @mappings.delete domain
end

#resolve(domain) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/explorer/hostmap.rb', line 21

def resolve domain
  domain = domain.gsub(/\d+.\d+.\d+.\d+.xip.io/, 'dev') #Support xip.io
  parts = domain.split '.'
  map = nil
  while !parts.empty? && map.nil? do
    map = @mappings[parts.join('.')]
    parts.shift
  end
  map
end

#save(file) ⇒ Object



32
33
34
35
# File 'lib/explorer/hostmap.rb', line 32

def save file
  Dir.mkdir File.dirname(file) unless Dir.exist?(File.dirname(file))
  File.write file, YAML.dump(mappings)
end