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



113
114
115
116
117
# File 'opal/lib/opal/sprockets/server.rb', line 113

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

Instance Method Details

#call(env) ⇒ Object



119
120
121
122
123
124
125
# File 'opal/lib/opal/sprockets/server.rb', line 119

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



128
129
130
131
132
133
134
135
# File 'opal/lib/opal/sprockets/server.rb', line 128

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



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'opal/lib/opal/sprockets/server.rb', line 137

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