Class: Kitkat::FileInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/kitkat/file_info.rb

Overview

File-level operations.

Constant Summary collapse

BLANK =
''
MIME_TYPE_SEPARATOR =
'/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, root: BLANK) ⇒ FileInfo

Returns a new instance of FileInfo.



11
12
13
14
# File 'lib/kitkat/file_info.rb', line 11

def initialize(path, root: BLANK)
  @path = path
  @root = root
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/kitkat/file_info.rb', line 9

def path
  @path
end

#rootObject (readonly)

Returns the value of attribute root.



9
10
11
# File 'lib/kitkat/file_info.rb', line 9

def root
  @root
end

Instance Method Details

#bytesizeObject



44
45
46
# File 'lib/kitkat/file_info.rb', line 44

def bytesize
  File.size(path)
end

#digestObject

Important note: Calling this on a directory will result in a blank string.



36
37
38
# File 'lib/kitkat/file_info.rb', line 36

def digest
  File.directory?(path) ? BLANK : Digest::SHA256.file(path).hexdigest
end

#full_mime_typeObject



20
21
22
23
24
25
# File 'lib/kitkat/file_info.rb', line 20

def full_mime_type
  @full_mime_type ||= IO.popen(
    ['file', '--brief', '--mime-type', path],
    in: :close, err: :close
  ) { |io| io.read.chomp }.to_s
end

#last_modified_atObject



40
41
42
# File 'lib/kitkat/file_info.rb', line 40

def last_modified_at
  File.mtime(path).utc
end

#mime_subtypeObject



31
32
33
# File 'lib/kitkat/file_info.rb', line 31

def mime_subtype
  full_mime_type.split(MIME_TYPE_SEPARATOR).last
end

#mime_typeObject



27
28
29
# File 'lib/kitkat/file_info.rb', line 27

def mime_type
  full_mime_type.split(MIME_TYPE_SEPARATOR).first
end

#relative_pathObject



16
17
18
# File 'lib/kitkat/file_info.rb', line 16

def relative_path
  path.delete_prefix(root).delete_prefix(File::SEPARATOR)
end