class Explorer::Hostmap

Attributes

mappings[R]

Public Class Methods

new() click to toggle source
# File lib/explorer/hostmap.rb, line 9
def initialize
  @mappings = {}
end

Public Instance Methods

add(domain, host, port) click to toggle source
# File lib/explorer/hostmap.rb, line 13
def add domain, host, port
  @mappings[domain] = { host: host, port: port }
end
load(file) click to toggle source
# 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) click to toggle source
# File lib/explorer/hostmap.rb, line 17
def remove domain
  @mappings.delete domain
end
resolve(domain) click to toggle source
# 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) click to toggle source
# 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