Class: Resource
- Inherits:
-
Object
- Object
- Resource
- Defined in:
- lib/sinatra/directory_listing/resource.rb
Instance Attribute Summary collapse
-
#file ⇒ Object
Class definition for a single resource to be listed.
-
#mtime ⇒ Object
Class definition for a single resource to be listed.
-
#mtime_html ⇒ Object
Class definition for a single resource to be listed.
-
#name_html ⇒ Object
Class definition for a single resource to be listed.
-
#page ⇒ Object
Class definition for a single resource to be listed.
-
#size ⇒ Object
Class definition for a single resource to be listed.
-
#size_html ⇒ Object
Class definition for a single resource to be listed.
Class Method Summary collapse
-
.sort(resource_array, sortby, direction) ⇒ Object
Sort an array of resources by name, mtime, or size.
Instance Method Summary collapse
-
#initialize(file, page) ⇒ Resource
constructor
A new instance of Resource.
-
#set_mtime(file) ⇒ Object
Set the mtime for a file.
-
#set_name(file) ⇒ Object
Set the name of the file and its link.
-
#set_size(file) ⇒ Object
Set the size for a file.
-
#wrap ⇒ Object
Generate html for a resource.
Constructor Details
#initialize(file, page) ⇒ Resource
12 13 14 15 16 17 18 |
# File 'lib/sinatra/directory_listing/resource.rb', line 12 def initialize(file, page) @page = page @file = file @name_html = set_name(file) @mtime, @mtime_html = set_mtime(file) @size, @size_html = set_size(file) end |
Instance Attribute Details
#file ⇒ Object
Class definition for a single resource to be listed. Each resource object has accessors for its file name, regular name, size and mtime, as well as those components wrapped in html.
10 11 12 |
# File 'lib/sinatra/directory_listing/resource.rb', line 10 def file @file end |
#mtime ⇒ Object
Class definition for a single resource to be listed. Each resource object has accessors for its file name, regular name, size and mtime, as well as those components wrapped in html.
10 11 12 |
# File 'lib/sinatra/directory_listing/resource.rb', line 10 def mtime @mtime end |
#mtime_html ⇒ Object
Class definition for a single resource to be listed. Each resource object has accessors for its file name, regular name, size and mtime, as well as those components wrapped in html.
10 11 12 |
# File 'lib/sinatra/directory_listing/resource.rb', line 10 def mtime_html @mtime_html end |
#name_html ⇒ Object
Class definition for a single resource to be listed. Each resource object has accessors for its file name, regular name, size and mtime, as well as those components wrapped in html.
10 11 12 |
# File 'lib/sinatra/directory_listing/resource.rb', line 10 def name_html @name_html end |
#page ⇒ Object
Class definition for a single resource to be listed. Each resource object has accessors for its file name, regular name, size and mtime, as well as those components wrapped in html.
10 11 12 |
# File 'lib/sinatra/directory_listing/resource.rb', line 10 def page @page end |
#size ⇒ Object
Class definition for a single resource to be listed. Each resource object has accessors for its file name, regular name, size and mtime, as well as those components wrapped in html.
10 11 12 |
# File 'lib/sinatra/directory_listing/resource.rb', line 10 def size @size end |
#size_html ⇒ Object
Class definition for a single resource to be listed. Each resource object has accessors for its file name, regular name, size and mtime, as well as those components wrapped in html.
10 11 12 |
# File 'lib/sinatra/directory_listing/resource.rb', line 10 def size_html @size_html end |
Class Method Details
.sort(resource_array, sortby, direction) ⇒ Object
Sort an array of resources by name, mtime, or size. Direction should be “ascending” or “descending”
132 133 134 135 136 |
# File 'lib/sinatra/directory_listing/resource.rb', line 132 def self.sort(resource_array, sortby, direction) new_array = resource_array.sort_by {|a| a.send(sortby)} new_array.reverse! if direction == "descending" new_array end |
Instance Method Details
#set_mtime(file) ⇒ Object
Set the mtime for a file.
Returns the mtime as a Time object so it can be sorted.
25 26 27 28 29 |
# File 'lib/sinatra/directory_listing/resource.rb', line 25 def set_mtime(file) f = File.join(File.join(@page.public_folder, URI.unescape(@page.request_path)), file) html = "\t<td>#{File.mtime(f).strftime(@page.last_modified_format)}</td>" return [File.mtime(f), html] end |
#set_name(file) ⇒ Object
Set the name of the file and its link.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/sinatra/directory_listing/resource.rb', line 54 def set_name(file) ## # Make sure we're working with an unescaped file name and truncate it. # URI.unescape seems to work best to decode uris. file = URI.unescape(file) file_truncated = file.truncate(@page.filename_truncate_length, '...') ## # If the requested resource is in the root public directory, the link is # just the resource itself without the public directory path as well. requested = Pathname.new(URI.unescape(@page.request_path)).cleanpath pub_folder = Pathname.new(@page.public_folder).cleanpath if requested.eql?(pub_folder) link = file else link = File.join(@page.request_path, file) end ## # Add a class of "dir" to directories and "file" to files. html = "" if File.directory?(URI.unescape(File.join(@page.public_folder, link))) html << "\t<td class='dir'>" ## # Append the sorting information if the current directory is sorted if @page.request_params["sortby"] && @page.request_params["direction"] link << "?sortby=" + @page.request_params["sortby"] + "&direction=" + @page.request_params["direction"] end else html << "\t<td class='file'>" end ## # Append the rest of the html. # # I haven't found a URI escaping library that will handle this # gracefully, so for now, we're going to just take care of spaces and # apostrophes ourselves. link = link.gsub(" ", "%20").gsub("'", "%27") html << "<a href='#{link}'>#{file_truncated}</a></td>" return html end |
#set_size(file) ⇒ Object
Set the size for a file.
Returns the size as number.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/sinatra/directory_listing/resource.rb', line 36 def set_size(file) html = "" size = '' f = File.join(File.join(@page.public_folder, URI.unescape(@page.request_path)), file) if File.directory?(f) size = 0 html = "\t<td>-</td>" else size = File.stat(f).size converted = Filesize.from("#{File.stat(f).size} B").pretty html = "\t<td>#{converted}</td>" end return [size, html] end |
#wrap ⇒ Object
Generate html for a resource.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/sinatra/directory_listing/resource.rb', line 108 def wrap html = "" if @page.should_list_invisibles == true html << "\n\t<tr> #{@name_html} #{@mtime_html} #{@size_html} \t</tr>" else if @file[0] != "." html << "\n\t<tr> #{@name_html} #{@mtime_html} #{@size_html} </tr>" end end html end |