Sha256: 5c73c6c237f0b4640a5f43ffe7c888c15aa85ded6262c143fb31ce67dcb4e790

Contents?: true

Size: 1.22 KB

Versions: 8

Compression:

Stored size: 1.22 KB

Contents

require 'net/ftp/list/parser'
require 'date'

# Parse Microsoft(NT) like FTP LIST entries.
#
# == MATCHES
#
#   06-25-2007  01:08PM       <DIR>          etc
#   06-25-07    01:08PM       <DIR>          etc
#   11-27-07    08:45PM                23437 README.TXT
class Net::FTP::List::Microsoft < Net::FTP::List::Parser
  REGEXP = %r!
    ^\s*
    ([0-9\-:\/]{5,})\s+([0-9\-:]{3,}(?:[aApP][mM])?)\s+
    (?:(<DIR>)|([0-9]+))\s+
    (\S.*)
    \s*$
  !x

  # Parse a Microsoft(NT) like FTP LIST entries.
  def self.parse(raw)
    match = REGEXP.match(raw.strip) or return false

    date_match = %r!(\d\d).(\d\d).(\d\d(?:\d\d)?)!.match(match[1])
    date_format = date_match[1].to_i > 12 ? '%d-%m-%y' : '%m-%d-%y'
    date_format.sub!(%r{%y}, '%Y') if date_match[3].length > 2

    if match[1] !~ /\-/
      date_format.gsub!(/\-/, '/') if match[1] =~ %r{/}
      date_format.gsub!(/\-/, ':') if match[1] =~ %r{:}
    end

    mtime = DateTime.strptime("#{match[1]} #{match[2]}", "#{date_format} %H:%M%p")
    is_dir = match[3] == '<DIR>'
    filesize = is_dir ? 0 : match[4].to_i

    emit_entry(
      raw,
      :dir => is_dir,
      :file => !is_dir,
      :filesize => filesize,
      :basename => match[5],
      :mtime => mtime
    )
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
net-ftp-list-3.2.11 lib/net/ftp/list/microsoft.rb
net-ftp-list-3.2.10 lib/net/ftp/list/microsoft.rb
net-ftp-list-3.2.9 lib/net/ftp/list/microsoft.rb
net-ftp-list-3.2.8 lib/net/ftp/list/microsoft.rb
net-ftp-list-3.2.7 lib/net/ftp/list/microsoft.rb
net-ftp-list-3.2.6 lib/net/ftp/list/microsoft.rb
net-ftp-list-3.2.5 lib/net/ftp/list/microsoft.rb
net-ftp-list-3.2.4 lib/net/ftp/list/microsoft.rb