class MicroExiftool::File

Public Class Methods

new(path) click to toggle source
# File lib/micro_exiftool/file.rb, line 12
def initialize(path)
  @path = path.to_s
  unless ::File.exist?(@path)
    raise NoSuchFile.new("No such file: #{@path}")
  end
end

Public Instance Methods

[](key) click to toggle source
# File lib/micro_exiftool/file.rb, line 19
def [](key)
  file_attributes[key]
end
attributes() click to toggle source
# File lib/micro_exiftool/file.rb, line 23
def attributes
  file_attributes.dup
end
update!(new_attributes) click to toggle source
# File lib/micro_exiftool/file.rb, line 27
def update!(new_attributes)
  update_json = [new_attributes.merge('SourceFile' => @path)].to_json

  run_exiftool(
    '-P',                     # dont change timestamp of file
    '-G0',                    # prefix all attributes with "EXIF:", "ITCP:" etc
    '-overwrite_original',    # dont make a backup copy
    '-j=-',                   # read new attributes from stdin as json
    stdin_data: update_json
  )

  @file_attributes = nil
  true
end

Private Instance Methods

file_attributes() click to toggle source
# File lib/micro_exiftool/file.rb, line 45
def file_attributes
  @file_attributes ||= load_attributes
end
load_attributes() click to toggle source
# File lib/micro_exiftool/file.rb, line 49
def load_attributes
  result = run_exiftool('-G0', '-j')
  JSON.parse(result).first
end
run_exiftool(*arguments) click to toggle source
# File lib/micro_exiftool/file.rb, line 54
def run_exiftool(*arguments)
  options = arguments.last.is_a?(Hash) ? arguments.pop : {}
  stdout_string, stderr_string, status = Open3.capture3('exiftool', @path, *arguments, options)
  unless status.exitstatus == 0
    raise ExifToolError.new("Error running exiftool:\n#{stderr_string}")
  end
  stdout_string
rescue Errno::ENOENT
  raise ExifToolError.new('Exiftool not installed')
end