Class: PortMap::NginxConf

Inherits:
Object
  • Object
show all
Defined in:
lib/port_map/nginx_conf.rb

Constant Summary collapse

DOMAIN =
'dev'.freeze
PORT_PLACEHOLDER =
'$PORT'.freeze
PORT_MAP_CONF_FILENAME =
'.port_map.conf'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port, name, domain = DOMAIN, existing_conf_content = '') ⇒ NginxConf

Returns a new instance of NginxConf.



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/port_map/nginx_conf.rb', line 22

def initialize(port, name, domain = DOMAIN, existing_conf_content = '')
  @name = name
  @port = port
  @domain = domain

  if existing_conf_content.empty?
    @output = default_conf_content
  else
    @output = existing_conf_content
  end
end

Instance Attribute Details

#domainObject (readonly)

Returns the value of attribute domain.



7
8
9
# File 'lib/port_map/nginx_conf.rb', line 7

def domain
  @domain
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/port_map/nginx_conf.rb', line 7

def name
  @name
end

#outputObject (readonly)

Returns the value of attribute output.



7
8
9
# File 'lib/port_map/nginx_conf.rb', line 7

def output
  @output
end

#portObject (readonly)

Returns the value of attribute port.



7
8
9
# File 'lib/port_map/nginx_conf.rb', line 7

def port
  @port
end

Class Method Details

.from_file(port, file) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/port_map/nginx_conf.rb', line 12

def self.from_file(port, file)
  file_content = file.read

  raise "File #{file} does not contain special $PORT placeholder" unless file_content.include?(PORT_PLACEHOLDER)
  file_content = file_content.sub(PORT_PLACEHOLDER, port)

  name, domain = file_content.match(/server_name\s+(.+)\.(.+?);/).captures
  new(port, name, domain, file_content)
end

Instance Method Details

#default_conf_contentObject



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/port_map/nginx_conf.rb', line 34

def default_conf_content
  %(
    server {
      listen       80;
      server_name  #{@name}.#{DOMAIN};

      location / {
          proxy_pass http://127.0.0.1:#{@port};
      }
    }
  )
end

#filenameObject



51
52
53
# File 'lib/port_map/nginx_conf.rb', line 51

def filename
  Nginx.servers_directory + File::Separator + @name + '.port_map.conf'
end

#saveObject



55
56
57
# File 'lib/port_map/nginx_conf.rb', line 55

def save
  File.open(filename, 'w+') { |f| f.write(@output) }
end

#server_nameObject



47
48
49
# File 'lib/port_map/nginx_conf.rb', line 47

def server_name
  "#{@name}.#{@domain}"
end