Class: Opal::Server::Index

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

Instance Method Summary collapse

Constructor Details

#initialize(app, server) ⇒ Index

Returns a new instance of Index



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

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

Instance Method Details

#call(env) ⇒ Object



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

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



115
116
117
118
119
120
121
122
# File 'opal/lib/opal/sprockets/server.rb', line 115

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

#javascript_include_tag(name) ⇒ Object



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

def javascript_include_tag name
  sprockets = @server.sprockets
  prefix = @server.prefix
  asset = sprockets[name]
  raise "Cannot find asset: #{name}" if asset.nil?
  scripts = []

  if @server.debug
    asset.to_a.map do |dependency|
      scripts << %{<script src="#{prefix}/#{dependency.logical_path}?body=1"></script>}
    end
  else
    scripts << %{<script src="#{prefix}/#{name}.js"></script>}
  end

  scripts << %{<script>#{Opal::Processor.load_asset_code(sprockets, name)}</script>}

  scripts.join "\n"
end

#sourceObject



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

def source
  <<-HTML
    <!DOCTYPE html>
    <html>
    <head>
      <title>Opal Server</title>
    </head>
    <body>
      #{javascript_include_tag @server.main}
    </body>
    </html>
  HTML
end