Class: NeocitiesRed::Services::FileList

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

Instance Method Summary collapse

Constructor Details

#initialize(client, path, detail) ⇒ FileList

Returns a new instance of FileList.



10
11
12
13
14
15
# File 'lib/neocities_red/services/file_list.rb', line 10

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

Instance Method Details

#listObject



17
18
19
20
21
22
23
# File 'lib/neocities_red/services/file_list.rb', line 17

def list
  resp = @client.list(@path)

  display_error_and_exit(resp) if resp[:result] == "error"

  resp[:files]
end

#showObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/neocities_red/services/file_list.rb', line 25

def show
  resp = @client.list(@path)

  display_error_and_exit(resp) if resp[:result] == "error"

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

    resp[: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

    puts TTY::Table.new(out)
  end

  resp[:files].map do |file|
    @pastel.send(file[:is_directory] ? :blue : :green).bold(file[:path])
  end

  resp[:files]
end