class ServerHere::Ls

Constants

Tmpl

Public Class Methods

new(app) click to toggle source
# File lib/server_here/ls.rb, line 7
def initialize app
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/server_here/ls.rb, line 11
def call env
  req = Rack::Request.new env
  path = req.path_info

  if not File.exists? in_pwd path
    return [404, {'Content-Type' => 'text/plain'}, ["#{path} does not exist"] ]
  end

  if File.directory? in_pwd path
    return [200, {'Content-Type' => 'text/html'}, [ls_al(path)] ]
  end

  @app.call env
end

Private Instance Methods

in_pwd(path) click to toggle source
# File lib/server_here/ls.rb, line 48
def in_pwd path
  '.' + path
end
ls_al(path) click to toggle source
# File lib/server_here/ls.rb, line 28
def ls_al path
  rel_path = relative path
  list = `ls -al #{in_pwd path}`.split("\n")[3..-1]
  parent_dir = parent rel_path
  Tmpl.result binding
end
no_deeper_than_one?(path) click to toggle source
# File lib/server_here/ls.rb, line 44
def no_deeper_than_one? path
  path.count('/') == 1
end
parent(path) click to toggle source
# File lib/server_here/ls.rb, line 39
def parent path
  return '/' if no_deeper_than_one? path
  File.join path.split('/')[0..-2]
end
relative(path) click to toggle source
# File lib/server_here/ls.rb, line 35
def relative path
  path == '/' ? '' : path#.sub(/\./, '')
end