Class: Opal::Server::Index

Inherits:
Object show all
Defined in:
opal/lib/opal/sprockets/server.rb

Constant Summary

SOURCE =
<<-HTML
  <!DOCTYPE html>
  <html>
  <head>
    <title>Opal Server</title>
  </head>
  <body>
    <%= javascript_include_tag @server.main %>
  </body>
  </html>
HTML

Instance Method Summary collapse

Constructor Details

#initialize(app, server) ⇒ Index

Returns a new instance of Index



135
136
137
138
139
# File 'opal/lib/opal/sprockets/server.rb', line 135

def initialize(app, server)
  @app = app
  @server = server
  @index_path = server.index_path
end

Instance Method Details

#call(env) ⇒ Object



141
142
143
144
145
146
147
# File 'opal/lib/opal/sprockets/server.rb', line 141

def call(env)
  if %w[/ /index.html].include? env['PATH_INFO']
    [200, { 'Content-Type' => 'text/html' }, [html]]
  else
    @app.call env
  end
end

#htmlObject

Returns the html content for the root path. Supports ERB



150
151
152
153
154
155
156
157
# File 'opal/lib/opal/sprockets/server.rb', line 150

def html
  if @index_path
    raise "index does not exist: #{@index_path}" unless File.exist?(@index_path)
    Tilt.new(@index_path).render(self)
  else
    ::ERB.new(SOURCE).result binding
  end
end

#javascript_include_tag(source) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'opal/lib/opal/sprockets/server.rb', line 159

def javascript_include_tag source
  if @server.debug
    assets = @server.sprockets[source].to_a

    raise "Cannot find asset: #{source}" if assets.empty?

    scripts = assets.map do |a|
      %Q{<script src="/assets/#{ a.logical_path }?body=1"></script>}
    end

    scripts.join "\n"
  else
    "<script src=\"/assets/#{source}.js\"></script>"
  end
end