Skip to content

Commit

Permalink
Refactor file system interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-murray committed Jan 28, 2014
1 parent eba7995 commit 09a7504
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
7 changes: 5 additions & 2 deletions spec/ferver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
describe 'ferver' do
include Webrat::Matchers # allow cool html matching

DEFAULT_FERVER_DIR = '/tmp'

context 'given a request to the server root' do

Expand Down Expand Up @@ -36,7 +35,11 @@

it 'will use default directory when none specified' do

Dir.expects(:foreach).with(DEFAULT_FERVER_DIR).returns([])
# hmmm... we have to stub this call too. This knows too much about the implementation.
# this doesnt smell like the best idea. TODO
File.stubs(:expand_path).returns('/a/path/to/ferver')

Dir.expects(:foreach).with('/a/path/to/ferver').returns([])
get '/files.html'

end
Expand Down
27 changes: 19 additions & 8 deletions src/ferver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ class Ferver < Sinatra::Base

# Config

# The full path to the directory to be served
DEFAULT_FILE_SERVER_DIR_PATH = '/tmp'

@file_list = []
# By default, serve files from current location
DEFAULT_FILE_SERVER_DIR_PATH = './'

# redirect to file list
# /
Expand Down Expand Up @@ -66,7 +64,7 @@ class Ferver < Sinatra::Base

if id < @file_list.size

file = "#{get_current_ferver_path}/#{@file_list[id]}" # todo: urgghh -> move this
file = get_path_for_file(get_current_ferver_path, @file_list[id])

send_file(file, :disposition => 'attachment', :filename => File.basename(file))

Expand All @@ -90,26 +88,39 @@ class Ferver < Sinatra::Base

next if file == '.' or file == '..'

@file_list.push(file) if File.file?(file)
file_path = get_path_for_file(current_directory, file)

@file_list.push(file) if File.file?(file_path)

end

end

private

def get_path_for_file(directory, file_name)

File.join(directory, file_name)

end


def get_current_ferver_path

path = nil

if settings.respond_to?(:ferver_path) and settings.ferver_path

settings.ferver_path
path = settings.ferver_path

else

DEFAULT_FILE_SERVER_DIR_PATH
path = DEFAULT_FILE_SERVER_DIR_PATH

end

File.expand_path(path)

end


Expand Down

0 comments on commit 09a7504

Please sign in to comment.