class StaticViewer

Constants

FILE_TYPE

Public Class Methods

new(app) click to toggle source
# File lib/db/static_viewer.rb, line 12
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/db/static_viewer.rb, line 16
def call(env)
  if is_public?(env)
    req = Rack::Request.new(env)
    res = Rack::Response.new

    file_path = "#{req.path}"
    build_response(res, file_path)
  else
    @app.call(env)
  end
end

Private Instance Methods

build_response(res, file_path) click to toggle source
# File lib/db/static_viewer.rb, line 29
def build_response(res, file_path)
  type = find_content_type(file_path)

  if type && Pathname(file_path).exist?
    res.write(File.open(file_path, 'r') { |f| (f.read) })
    res['Content-Type'] = find_content_type(file_path)
    res.finish
  else
    type ? res.write("Could not find file.") : res.write("Unsupported file type")
    res.status = 404
    res['Content-Type'] = 'text/html'
    res.finish
  end
end
find_content_type(path) click to toggle source
# File lib/db/static_viewer.rb, line 48
def find_content_type(path)
  type = path.match(/\.(\w*)/)[1]
  FILE_TYPE[type]
end
is_public?(env) click to toggle source
# File lib/db/static_viewer.rb, line 44
def is_public?(env)
  !!(Regexp.new("/public/*") =~ env["PATH_INFO"])
end