Class: Shakapacker::DevServer

Inherits:
Object
  • Object
show all
Defined in:
lib/shakapacker/dev_server.rb

Constant Summary collapse

DEFAULT_ENV_PREFIX =
"SHAKAPACKER_DEV_SERVER".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ DevServer

Returns a new instance of DevServer.



10
11
12
# File 'lib/shakapacker/dev_server.rb', line 10

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/shakapacker/dev_server.rb', line 8

def config
  @config
end

Instance Method Details

#env_prefixObject



79
80
81
# File 'lib/shakapacker/dev_server.rb', line 79

def env_prefix
  config.dev_server.fetch(:env_prefix, DEFAULT_ENV_PREFIX)
end

#hmr?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/shakapacker/dev_server.rb', line 66

def hmr?
  fetch(:hmr)
end

#hostObject



25
26
27
# File 'lib/shakapacker/dev_server.rb', line 25

def host
  fetch(:host)
end

#host_with_portObject



58
59
60
# File 'lib/shakapacker/dev_server.rb', line 58

def host_with_port
  "#{host}:#{port}"
end

#inline_css?Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
# File 'lib/shakapacker/dev_server.rb', line 70

def inline_css?
  case fetch(:inline_css)
  when false, "false"
    false
  else
    true
  end
end

#portObject



29
30
31
# File 'lib/shakapacker/dev_server.rb', line 29

def port
  fetch(:port)
end

#pretty?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/shakapacker/dev_server.rb', line 62

def pretty?
  fetch(:pretty)
end

#protocolObject



52
53
54
55
56
# File 'lib/shakapacker/dev_server.rb', line 52

def protocol
  return "https" if server == "https"

  "http"
end

#running?Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
# File 'lib/shakapacker/dev_server.rb', line 14

def running?
  if config.dev_server.present?
    Socket.tcp(host, port, connect_timeout: connect_timeout).close
    true
  else
    false
  end
rescue
  false
end

#serverObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/shakapacker/dev_server.rb', line 33

def server
  server_value = fetch(:server)
  server_type = server_value.is_a?(Hash) ? server_value[:type] : server_value

  return server_type if ["http", "https"].include?(server_type)

  return "http" if server_type.nil?

  puts <<~MSG
  WARNING:
  `server: #{server_type}` is not a valid configuration in Shakapacker.
  Falling back to default `server: http`.
  MSG

  "http"
rescue KeyError
  "http"
end