Class: Raval::ListFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/raval/list_formatter.rb

Overview

converts a list of objects that describe the contents of a directory and formats them to be valid responses to the LIST or NLST FTP commands.

Instance Method Summary collapse

Constructor Details

#initialize(files) ⇒ ListFormatter

Returns a new instance of ListFormatter.



7
8
9
# File 'lib/raval/list_formatter.rb', line 7

def initialize(files)
  @files = check_duck_type(files)
end

Instance Method Details

#detailedObject

response to the LIST command



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/raval/list_formatter.rb', line 19

def detailed
  now = Time.now
  @files.map { |item|
    directory   = item.directory ? 'd' : '-'
    permissions = item.permissions || 'rwxrwxrwx'
    owner       = item.owner || 'owner'
    group       = item.group || 'group'
    size        = (item.size || 0).to_s.rjust(12)
    time        = (item.time || now).strftime("%b %d %H:%M")
    name        = item.name || "UNKNOWN"

    "#{directory}#{permissions} 1 #{owner}  #{group} #{size} #{time} #{name}"
  }
end

#shortObject

response to the NLST command



13
14
15
# File 'lib/raval/list_formatter.rb', line 13

def short
  @files.map(&:name)
end