Class: Opal::Server::Index

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

Constant Summary collapse

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.



99
100
101
102
103
# File 'lib/opal/sprockets/server.rb', line 99

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

Instance Method Details

#call(env) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/opal/sprockets/server.rb', line 105

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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/opal/sprockets/server.rb', line 114

def html
  source = if @index_path
    raise "index does not exist: #{@index_path}" unless File.exist?(@index_path)
    File.read @index_path
  elsif File.exist? 'index.html'
    File.read 'index.html'
  elsif File.exist? 'index.html.erb'
    File.read 'index.html.erb'
  else
    SOURCE
  end

  ::ERB.new(source).result binding
end

#javascript_include_tag(source) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/opal/sprockets/server.rb', line 129

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