Class: FactoryBurgers::Middleware::Static

Inherits:
Object
  • Object
show all
Defined in:
lib/factory_burgers/middleware/static.rb

Overview

Serve static assets build for the UI

Instance Method Summary collapse

Instance Method Details

#asset_pathObject



31
32
33
# File 'lib/factory_burgers/middleware/static.rb', line 31

def asset_path
  @asset_path ||= FactoryBurgers.root.join("assets/")
end

#call(env) ⇒ Object



5
6
7
8
9
# File 'lib/factory_burgers/middleware/static.rb', line 5

def call(env)
  return slashpath_redirect(env["REQUEST_PATH"]) if slashpath_redirect?(env)

  rack_static.call(env)
end

#rack_staticObject



23
24
25
# File 'lib/factory_burgers/middleware/static.rb', line 23

def rack_static
  Rack::Static.new(nil, static_options)
end

#slashpath_redirect(path) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/factory_burgers/middleware/static.rb', line 15

def slashpath_redirect(path)
  return [
    302,
    {'Location' => "#{path}/", 'Content-Type' => 'text/html', 'Content-Length' => '0'},
    [],
  ]
end

#slashpath_redirect?(env) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/factory_burgers/middleware/static.rb', line 11

def slashpath_redirect?(env)
  env["REQUEST_PATH"] == env["SCRIPT_NAME"] && env["REQUEST_PATH"][-1] != "/"
end

#static_optionsObject



27
28
29
# File 'lib/factory_burgers/middleware/static.rb', line 27

def static_options
  {urls: [""], root: asset_path, index: 'index.html'}
end