Class: Octodown::Renderer::Server
- Inherits:
-
Object
- Object
- Octodown::Renderer::Server
- Defined in:
- lib/octodown/renderer/server.rb
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
-
#app ⇒ Object
Cascade through this app and Rack::File app.
- #boot_server ⇒ Object
- #call(env) ⇒ Object
-
#initialize(_content, options = {}) ⇒ Server
constructor
A new instance of Server.
- #maybe_launch_browser ⇒ Object
- #present ⇒ Object
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, = {}) @file = ARGF.file @options = @path = File.dirname(File.(file.path)) @port = [:port] @websockets = [] @already_opened = false @mutex = Mutex.new end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
11 12 13 |
# File 'lib/octodown/renderer/server.rb', line 11 def file @file end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
11 12 13 |
# File 'lib/octodown/renderer/server.rb', line 11 def @options end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
11 12 13 |
# File 'lib/octodown/renderer/server.rb', line 11 def path @path end |
#port ⇒ Object (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
#app ⇒ Object
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_server ⇒ Object
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_browser ⇒ Object
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 |
#present ⇒ Object
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 |