Class: Explorer::Hostmap
- Inherits:
-
Object
- Object
- Explorer::Hostmap
- Includes:
- Celluloid
- Defined in:
- lib/explorer/hostmap.rb
Instance Attribute Summary collapse
-
#mappings ⇒ Object
readonly
Returns the value of attribute mappings.
Instance Method Summary collapse
- #add(domain, host, port) ⇒ Object
-
#initialize ⇒ Hostmap
constructor
A new instance of Hostmap.
- #load(file) ⇒ Object
- #remove(domain) ⇒ Object
- #resolve(domain) ⇒ Object
- #save(file) ⇒ Object
Constructor Details
#initialize ⇒ Hostmap
Returns a new instance of Hostmap.
9 10 11 |
# File 'lib/explorer/hostmap.rb', line 9 def initialize @mappings = {} end |
Instance Attribute Details
#mappings ⇒ Object (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 |