Class: NeocitiesRed::Services::File::List

Inherits:
Object
  • Object
show all
Defined in:
lib/neocities_red/services/file/list.rb

Overview

Lists files on the remote Neocities site.

Supports both a simple list mode and a detailed table mode with file size, SHA1 hash, and last-updated timestamp.

Examples:

Simple listing

list = NeocitiesRed::Services::File::List.new(client, nil, false, display: display)
files = list.show

Detailed table

list = NeocitiesRed::Services::File::List.new(client, nil, true, display: display)
list.show  # renders TTY::Table

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(client, path, detail, display: NeocitiesRed::CliDisplay.new) ⇒ List

Returns a new instance of List.

Parameters:

  • client (NeocitiesRed::Client)

    authenticated API client

  • path (String, nil)

    remote directory path to list (nil for root)

  • detail (Boolean)

    when true, renders a detailed table with size, hash, and timestamp columns

  • display (NeocitiesRed::CliDisplay) (defaults to: NeocitiesRed::CliDisplay.new)

    output helper



31
32
33
34
35
36
37
# File 'lib/neocities_red/services/file/list.rb', line 31

def initialize(client, path, detail, display: NeocitiesRed::CliDisplay.new)
  @client = client
  @path = path
  @detail = detail || false
  @display = display
  @pastel = Pastel.new(eachline: "\n")
end

Instance Method Details

#listArray<Hash>

Returns the raw list of remote files.

Returns:

  • (Array<Hash>)

    array of file hashes with :path, :is_directory, :size, :sha1_hash, and :updated_at keys

Raises:



44
45
46
# File 'lib/neocities_red/services/file/list.rb', line 44

def list
  files_from_response
end

#showArray<Hash>

Returns the file list, optionally rendered as a detailed table.

In detail mode, displays a colored table with Path, Size, SHA1 Hash, and Updated columns. Directories are shown in blue, files in green.

Returns:

  • (Array<Hash>)

    the file list hashes

Raises:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/neocities_red/services/file/list.rb', line 55

def show
  files = files_from_response

  return files unless @detail

  out = [
    [@pastel.bold("Path"), @pastel.bold("Size"), @pastel.bold("sha1_Hash"),
     @pastel.bold("Updated")]
  ]

  files.each do |file|
    out.push([
               @pastel.send(file[:is_directory] ? :blue : :green).bold(file[:path]),
               file[:size] || "",
               file[:sha1_hash],
               Time.parse(file[:updated_at]).localtime
             ])
  end

  @display.display_list_table(TTY::Table.new(out))

  files
end