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 =
<<~HTML
  <!DOCTYPE html>
  <html>
  <head>
      <title><%= path %></title>
      <style>
          body { font-family: Arial, sans-serif; margin: 20px; }
          h1 { color: #333; }
          table { border-collapse: collapse; width: 100%; margin-top: 20px; }
          th, td { padding: 8px 12px; text-align: left; border-bottom: 1px solid #ddd; }
          th { background-color: #f4f4f4; font-weight: bold; }
          tr:hover { background-color: #f9f9f9; }
          a { color: #0066cc; text-decoration: none; }
          a:hover { text-decoration: underline; }
          .size { font-family: monospace; }
          .date { color: #666; }
          .dir { font-weight: bold; color: #228822; }
      </style>
  </head>
  <body>
      <h1><%= path %></h1>

      <% unless path == '/' %>
      <p><a class="dir" href="../index.html">../</a></p>
      <% end %>

      <% if files.empty? %>
      <p>No files found.</p>
      <% else %>
      <table>
          <tr>
              <th>Filename</th>
              <th>Last Modified</th>
              <th>Size</th>
          </tr>
          <% files.each do |file| %>
          <tr>
              <% if file[:is_directory] %>
              <td class="dir"><a href="<%= file[:filename_relative_path] %>index.html"><%= file[:filename] %></a></td>
              <td class="date"></td>
              <td class="size"></td>
              <% else %>
              <td><a href="<%= file[:filename_relative_path] %>"><%= file[:filename] %></a></td>
              <td class="date"><%= format_date(file[:last_modified]) %></td>
              <td class="size"><%= format_size(file[:size]) %></td>
              <% end %>
          </tr>
          <% end %>
      </table>
      <p><%= files.size %> item(s)</p>
      <% end %>
  </body>
  </html>
HTML

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