Class: QuickAndRuby::S3::WebIndexRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/quick_and_ruby/s3/web_index_renderer.rb

Constant Summary collapse

DEFAULT_TEMPLATE =
"<!DOCTYPE html>\n<html>\n<head>\n    <title><%= path %></title>\n    <style>\n        body { font-family: Arial, sans-serif; margin: 20px; }\n        h1 { color: #333; }\n        table { border-collapse: collapse; width: 100%; margin-top: 20px; }\n        th, td { padding: 8px 12px; text-align: left; border-bottom: 1px solid #ddd; }\n        th { background-color: #f4f4f4; font-weight: bold; }\n        tr:hover { background-color: #f9f9f9; }\n        a { color: #0066cc; text-decoration: none; }\n        a:hover { text-decoration: underline; }\n        .size { font-family: monospace; }\n        .date { color: #666; }\n        .dir { font-weight: bold; color: #228822; }\n    </style>\n</head>\n<body>\n    <h1><%= path %></h1>\n\n    <% unless path == '/' %>\n    <p><a class=\"dir\" href=\"../index.html\">../</a></p>\n    <% end %>\n\n    <% if files.empty? %>\n    <p>No files found.</p>\n    <% else %>\n    <table>\n        <tr>\n            <th>Filename</th>\n            <th>Last Modified</th>\n            <th>Size</th>\n        </tr>\n        <% files.each do |file| %>\n        <tr>\n            <% if file[:is_directory] %>\n            <td class=\"dir\"><a href=\"<%= file[:filename_relative_path] %>index.html\"><%= file[:filename] %></a></td>\n            <td class=\"date\"></td>\n            <td class=\"size\"></td>\n            <% else %>\n            <td><a href=\"<%= file[:filename_relative_path] %>\"><%= file[:filename] %></a></td>\n            <td class=\"date\"><%= format_date(file[:last_modified]) %></td>\n            <td class=\"size\"><%= format_size(file[:size]) %></td>\n            <% end %>\n        </tr>\n        <% end %>\n    </table>\n    <p><%= files.size %> item(s)</p>\n    <% end %>\n</body>\n</html>\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template = DEFAULT_TEMPLATE) ⇒ WebIndexRenderer

Returns a new instance of WebIndexRenderer.



65
66
67
# File 'lib/quick_and_ruby/s3/web_index_renderer.rb', line 65

def initialize(template = DEFAULT_TEMPLATE)
  @template = ERB.new(template)
end

Instance Attribute Details

#templateObject (readonly)

Returns the value of attribute template.



63
64
65
# File 'lib/quick_and_ruby/s3/web_index_renderer.rb', line 63

def template
  @template
end

Instance Method Details

#format_date(date) ⇒ Object



73
74
75
# File 'lib/quick_and_ruby/s3/web_index_renderer.rb', line 73

def format_date(date)
  date.strftime('%Y-%m-%d %H:%M')
end

#format_size(bytes) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/quick_and_ruby/s3/web_index_renderer.rb', line 77

def format_size(bytes)
  return '0 B' if bytes.zero?

  units = %w[B KB MB GB TB]
  size = bytes.to_f
  unit = 0

  while size >= 1024 && unit < units.size - 1
    size /= 1024
    unit += 1
  end

  format("%.2f #{units[unit]}", size)
end

#generate(path:, files:) ⇒ Object



69
70
71
# File 'lib/quick_and_ruby/s3/web_index_renderer.rb', line 69

def generate(path:, files:)
  template.result(binding)
end