Class: Cubic::Static

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

Overview

Replaces the Rack::Static module as was unreliable for this application.

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Static

Returns a new instance of Static.



17
18
19
20
21
22
23
# File 'lib/cubic/middleware/static.rb', line 17

def initialize(app, options = {})
  @app  = app
  @url  = options[:url]
  root  = options[:root] || Dir.pwd

  @file = Rack::File.new(root)
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/cubic/middleware/static.rb', line 9

def call(env)
  if valid_route(env['PATH_INFO'])
    @file.call(env)
  else
    @app.call(env)
  end
end

#valid_route(path) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/cubic/middleware/static.rb', line 25

def valid_route(path)
  if @url.is_a? Array
    ar = @url.map { |u| path.include?(u) ? true : false }
    ar.include? true
  else
    path.include?(@url) ? true : false
  end
end