Class: Restapi::StaticDispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/restapi/static_dispatcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, path, baseurl) ⇒ StaticDispatcher

Dispatches the statis files. Simillar to ActionDispatch::Static, but it supports different baseurl configurations



39
40
41
42
43
# File 'lib/restapi/static_dispatcher.rb', line 39

def initialize(app, path, baseurl)
  @app = app
  @baseurl = baseurl
  @file_handler = Restapi::FileHandler.new(path)
end

Instance Method Details

#call(env) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/restapi/static_dispatcher.rb', line 45

def call(env)
  case env['REQUEST_METHOD']
  when 'GET', 'HEAD'
    path = env['PATH_INFO'].sub("#{@baseurl}/","/restapi/").chomp('/')
    path.sub!("#{ENV["RAILS_RELATIVE_URL_ROOT"]}",'')
    
    if match = @file_handler.match?(path)
      env["PATH_INFO"] = match
      return @file_handler.call(env)
    end
  end

  @app.call(env)
end