Class: Opal::Server::Index

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

Constant Summary collapse

SOURCE =
"<!DOCTYPE html>\n<html>\n<head>\n<title>Opal Server</title>\n</head>\n<body>\n<%= javascript_include_tag @server.main %>\n</body>\n</html>\n"

Instance Method Summary collapse

Constructor Details

#initialize(app, server) ⇒ Index


133
134
135
136
137
# File 'lib/opal/sprockets/server.rb', line 133

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

Instance Method Details

#call(env) ⇒ Object


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

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


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

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


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

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