Class: Middleman::PreviewServer::ServerInformation

Inherits:
Object
  • Object
show all
Defined in:
middleman-core/lib/middleman-core/preview_server/server_information.rb

Overview

This class holds all information which the preview server needs to setup a listener

  • server name
  • bind address
  • port

Furthermore it probes for a free tcp port, if the default one 4567 is not available.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ ServerInformation

Returns a new instance of ServerInformation



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'middleman-core/lib/middleman-core/preview_server/server_information.rb', line 25

def initialize(opts={})
  @resolver     = opts.fetch(:resolver, DnsResolver.new)
  @validator    = opts.fetch(:validator, ServerInformationValidator.new)
  @network_interface_inventory = opts.fetch(:network_interface_inventory, NetworkInterfaceInventory.new)
  @tcp_port_prober = opts.fetch(:tcp_port_prober, TcpPortProber.new)

  @informations = []
  @informations << AllInterfaces
  @informations << AllIpv4Interfaces
  @informations << AllIpv6Interfaces
  @informations << ServerNameIsIpInformation
  @informations << ServerNameInformation
  @informations << BindAddressInformation
  @informations << BindAddressAndServerNameInformation
  @informations << DefaultInformation
end

Instance Attribute Details

#https=(value) ⇒ Object (writeonly)

Sets the attribute https

Parameters:

  • value

    the value to set the attribute https to.



23
24
25
# File 'middleman-core/lib/middleman-core/preview_server/server_information.rb', line 23

def https=(value)
  @https = value
end

Instance Method Details

#bind_addressString

The bind address of server

Returns:

  • (String)

    The bind address of the server



118
119
120
# File 'middleman-core/lib/middleman-core/preview_server/server_information.rb', line 118

def bind_address
  information.bind_address
end

#handlerString

Make information of internal server class avaible to make debugging easier. This can be used to log the class which was used to determine the preview server settings

Returns:

  • (String)

    The name of the class



83
84
85
# File 'middleman-core/lib/middleman-core/preview_server/server_information.rb', line 83

def handler
  information.class.to_s
end

#https?Boolean

Is https enabled?

Returns:

  • (Boolean)


148
149
150
# File 'middleman-core/lib/middleman-core/preview_server/server_information.rb', line 148

def https?
  @https == true
end

#informationObject

The information

Is cached



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'middleman-core/lib/middleman-core/preview_server/server_information.rb', line 45

def information
  return @information if @information

  # The `DefaultInformation`-class always returns `true`, so there's
  # always a klass available and find will never return nil
  listener_klass = informations.find { |l| l.matches? bind_address: @bind_address, server_name: @server_name }
  @information = listener_klass.new(bind_address: @bind_address, server_name: @server_name)

  @information.show_me_network_interfaces(network_interface_inventory)
  @information.resolve_me(resolver)
  @information.port = tcp_port_prober.port(@port)
  @information.validate_me(validator)

  @information
end

#listenersArray

A list of listeners

Returns:

  • (Array)

    A list of bind address where the



143
144
145
# File 'middleman-core/lib/middleman-core/preview_server/server_information.rb', line 143

def listeners
  information.listeners
end

#portInteger

The port on which the server should listen

Returns:

  • (Integer)

    The port number



126
127
128
# File 'middleman-core/lib/middleman-core/preview_server/server_information.rb', line 126

def port
  information.port
end

#reasonString

The reason why the information is NOT valid

Returns:

  • (String)

    The reason why the information is not valid



102
103
104
# File 'middleman-core/lib/middleman-core/preview_server/server_information.rb', line 102

def reason
  information.reason
end

#server_nameString

The server name

Returns:

  • (String)

    The name of the server



110
111
112
# File 'middleman-core/lib/middleman-core/preview_server/server_information.rb', line 110

def server_name
  information.server_name
end

#site_addressesArray

A list of site addresses

Returns:

  • (Array)

    A list of addresses which can be used to access the middleman preview server



135
136
137
# File 'middleman-core/lib/middleman-core/preview_server/server_information.rb', line 135

def site_addresses
  information.site_addresses
end

#use(config) ⇒ Object

Use a middleman configuration to get information

Parameters:

  • config (#[])

    The middleman config



65
66
67
68
69
70
71
72
73
74
75
# File 'middleman-core/lib/middleman-core/preview_server/server_information.rb', line 65

def use(config)
  @bind_address = config[:bind_address]
  @port         = config[:port]
  @server_name  = config[:server_name]
  @https        = config[:https]

  config[:bind_address] = bind_address
  config[:port]         = port
  config[:server_name]  = server_name
  config[:https]        = https?
end

#valid?TrueClass, FalseClass

Is the server information valid?

This is used to output a helpful error message, which can be stored in #reason.

Returns:

  • (TrueClass, FalseClass)

    The result



94
95
96
# File 'middleman-core/lib/middleman-core/preview_server/server_information.rb', line 94

def valid?
  information.valid?
end