Class: Octodown::Renderer::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/octodown/renderer/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_content, options = {}) ⇒ Server

Returns a new instance of Server.



13
14
15
16
17
18
19
20
21
# File 'lib/octodown/renderer/server.rb', line 13

def initialize(_content, options = {})
  @file = ARGF.file
  @options = options
  @path = File.dirname(File.expand_path(file.path))
  @port = options[:port]
  @websockets = []
  @already_opened = false
  @mutex = Mutex.new
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



11
12
13
# File 'lib/octodown/renderer/server.rb', line 11

def file
  @file
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/octodown/renderer/server.rb', line 11

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



11
12
13
# File 'lib/octodown/renderer/server.rb', line 11

def path
  @path
end

#portObject (readonly)

Returns the value of attribute port.



11
12
13
# File 'lib/octodown/renderer/server.rb', line 11

def port
  @port
end

Instance Method Details

#appObject

Cascade through this app and Rack::File app. If Server returns 404, Rack::File will try to serve a static file.



60
61
62
# File 'lib/octodown/renderer/server.rb', line 60

def app
  @app ||= Rack::Cascade.new([self, Rack::File.new(path)])
end

#boot_serverObject



35
36
37
38
# File 'lib/octodown/renderer/server.rb', line 35

def boot_server
  puts "[INFO] Server running on http://localhost:#{port}"
  Rack::Handler::Puma.run app, Host: 'localhost', Port: port, Silent: true
end

#call(env) ⇒ Object



54
55
56
# File 'lib/octodown/renderer/server.rb', line 54

def call(env)
  ::Faye::WebSocket.websocket?(env) ? render_ws(env) : render_http(env)
end

#maybe_launch_browserObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/octodown/renderer/server.rb', line 40

def maybe_launch_browser
  return if ENV['TEST']

  sleep 2.5

  @mutex.synchronize do
    if @already_opened == false
      @already_opened = true
      puts '[INFO] Loading preview in a new browser tab'
      Launchy.open "http://localhost:#{port}"
    end
  end
end

#presentObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/octodown/renderer/server.rb', line 23

def present
  register_listener

  Thread.new do
    Thread.abort_on_exception = true
    maybe_launch_browser
    Thread.exit
  end

  boot_server
end