Class: Opal::SimpleServer
- Inherits:
-
Object
- Object
- Opal::SimpleServer
- Defined in:
- opal/lib/opal/simple_server.rb
Overview
Opal::SimpleServer is a very basic Rack server for Opal assets, it relies on Opal::Builder and Ruby corelib/stdlib. It's meant to be used just for local development.
For a more complete implementation see opal-sprockets (Rubygems) or opal-webpack (NPM).
Examples:
(CLI)
rackup -ropal -ropal/simple_server -b 'Opal.append_path("app"); run Opal::SimpleServer.new'
Constant Summary
- NotFound =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#index_path ⇒ Object
Returns the value of attribute index_path.
-
#main ⇒ Object
Returns the value of attribute main.
Instance Method Summary collapse
- #append_path(path) ⇒ Object deprecated Deprecated.
- #cache_invalidator ⇒ Object
- #call(env) ⇒ Object
- #call_index ⇒ Object
- #call_js(path) ⇒ Object
- #fetch_asset(path) ⇒ Object
-
#initialize(options = {}) {|_self| ... } ⇒ SimpleServer
constructor
A new instance of SimpleServer.
- #javascript_include_tag(path) ⇒ Object
Constructor Details
#initialize(options = {}) {|_self| ... } ⇒ SimpleServer
Returns a new instance of SimpleServer
Yields:
- (_self)
Yield Parameters:
-
_self
(Opal::SimpleServer )
—
the object that the method was called on
20 21 22 23 24 25 26
# File 'opal/lib/opal/simple_server.rb', line 20 def initialize(options = {}) @prefix = options.fetch(:prefix, 'assets') @main = options.fetch(:main, 'application') @index_path = nil yield self if block_given? freeze end
Instance Attribute Details
#index_path ⇒ Object
Returns the value of attribute index_path
28 29 30
# File 'opal/lib/opal/simple_server.rb', line 28 def index_path @index_path end
#main ⇒ Object
Returns the value of attribute main
28 29 30
# File 'opal/lib/opal/simple_server.rb', line 28 def main @main end
Instance Method Details
#append_path(path) ⇒ Object
Deprecated.
It's here for compatibility with Opal::Sprockets::Server
32 33 34 35
# File 'opal/lib/opal/simple_server.rb', line 32 def append_path(path) Opal .deprecation "`#{self.class}#append_path` is deprecated, please use `Opal.append_path(path)` instead (called from: #{caller(1, 1).first})" Opal .append_path path end
#cache_invalidator ⇒ Object
71 72 73
# File 'opal/lib/opal/simple_server.rb', line 71 def cache_invalidator "?#{Time.now.to_i}" end
#call(env) ⇒ Object
37 38 39 40 41 42 43 44 45 46
# File 'opal/lib/opal/simple_server.rb', line 37 def call(env) case env['PATH_INFO'] when %r{\A/#{@prefix}/(.*)\.js\z} path, _cache_invalidator = Regexp.last_match(1).split('?', 2) call_js(path) else call_index end rescue NotFound => error [404, {}, [error.to_s]] end
#call_index ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
# File 'opal/lib/opal/simple_server.rb', line 75 def call_index if @index_path contents = File.read(@index_path) html = ERB .new(contents).result binding else html = <<-HTML <!doctype html> <html> <head> <meta charset="utf8"> #{javascript_include_tag(main)} </head> <body></body> </html> HTML end [200, { 'Content-Type' => 'text/html' }, [html]] end
#call_js(path) ⇒ Object
48 49 50 51 52 53 54 55
# File 'opal/lib/opal/simple_server.rb', line 48 def call_js(path) asset = fetch_asset(path) [ 200, { 'Content-Type' => 'application/javascript' }, [asset[:data], "\n", asset[:map].to_data_uri_comment], ] end
#fetch_asset(path) ⇒ Object
#javascript_include_tag(path) ⇒ Object
67 68 69
# File 'opal/lib/opal/simple_server.rb', line 67 def javascript_include_tag(path) %{<script src="/#{@prefix}/#{path}.js#{cache_invalidator}"></script>} end