Skip to content

Commit

Permalink
Move html into inline template
Browse files Browse the repository at this point in the history
  • Loading branch information
rob-murray committed Jan 31, 2014
1 parent 09a7504 commit bf34aba
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
2 changes: 1 addition & 1 deletion spec/ferver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
get '/files.html'
expect(last_response).to be_ok

expect(last_response.body).not_to have_selector("a")
expect(last_response.body).to have_selector("li", :count => 0)

expect(last_response.body).to contain(/0 files/)

Expand Down
59 changes: 49 additions & 10 deletions src/ferver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
class Ferver < Sinatra::Base

# Config
set :inline_templates, true
set :app_file, __FILE__

# By default, serve files from current location
DEFAULT_FILE_SERVER_DIR_PATH = './'
Expand All @@ -31,16 +33,11 @@ class Ferver < Sinatra::Base
# list files
# /files.html
get '/files.html' do

content = "<html><body><h3>Files served:</h3><p><ul>"

@file_list.each_with_index do |file, index|

content += "<li><a href=""/files/#{index}"">#{file}</a></li>"

end
@file_count = @file_list.size
@ferver_path = get_current_ferver_path

content += "</ul></p><p>#{@file_list.size} files served from: #{get_current_ferver_path}</p></body></html>"
erb :file_list_view

end

Expand Down Expand Up @@ -77,7 +74,9 @@ class Ferver < Sinatra::Base
end


# before each block
# Find all files in `Ferver` directory.
# Called before each response.
#
before do

@file_list = []
Expand All @@ -98,13 +97,19 @@ class Ferver < Sinatra::Base

private

# Return an absolute path to a `file_name` in the `directory`
#
#
def get_path_for_file(directory, file_name)

File.join(directory, file_name)

end


# Return the absolute path to the directory Ferver is serving files from.
# This can be specified in Sinatra configuration;
# i.e. `Ferver.set :ferver_path, ferver_path` or the default if nil
#
def get_current_ferver_path

path = nil
Expand All @@ -125,3 +130,37 @@ def get_current_ferver_path


end

__END__

@@file_list_view
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Ferver File List</title>
</head>
<body>
<h3>Files served:</h3>
<ul>
<% @file_list.each_with_index do |file_name, index| %>

<li><a href="/files/<%= index %>"><%= file_name %></a></li>

<% end %>

</ul>

<p><%= @file_count %> files served from: <%= @ferver_path %></p>

<hr>

<p>Served by: <a href="https://github.com/rob-murray/ferver" title="Ferver: A simple Ruby app serving files over HTTP">Ferver</a></p>

</body>
</html>

<html>
<body>


0 comments on commit bf34aba

Please sign in to comment.