Class: Exiftool

Inherits:
Object
  • Object
show all
Defined in:
lib/exiftool.rb,
lib/exiftool/parser.rb,
lib/exiftool/result.rb,
lib/exiftool/version.rb

Defined Under Namespace

Classes: ExiftoolNotInstalled, NoSuchFile, NotAFile, Parser, Result

Constant Summary collapse

VERSION =
Gem::Version.new('0.2.0')

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filenames, exiftool_opts = '') ⇒ Exiftool

Returns a new instance of Exiftool.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/exiftool.rb', line 24

def initialize(filenames, exiftool_opts = '')
  @file2result = {}
  filenames = [filenames] if filenames.is_a?(String)
  unless filenames.empty?
    escaped_filenames = filenames.collect do |f|
      Shellwords.escape(self.class.expand_path(f.to_s))
    end.join(" ")
    # I'd like to use -dateformat, but it doesn't support timezone offsets properly,
    # nor sub-second timestamps.
    cmd = "exiftool #{exiftool_opts} -j -coordFormat \"%.8f\" #{escaped_filenames} 2> /dev/null"
    json = `#{cmd}`
    raise ExiftoolNotInstalled if json == ""
    JSON.parse(json).each do |raw|
      result = Result.new(raw)
      @file2result[result.source_file] = result
    end
  end
end

Class Method Details

.exiftool_installed?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/exiftool.rb', line 10

def self.exiftool_installed?
  exiftool_version > 0
end

.exiftool_versionObject



14
15
16
# File 'lib/exiftool.rb', line 14

def self.exiftool_version
  @@exiftool_version ||= `exiftool -ver 2> /dev/null`.to_f
end

.expand_path(filename) ⇒ Object

Raises:



18
19
20
21
22
# File 'lib/exiftool.rb', line 18

def self.expand_path(filename)
  raise(NoSuchFile, filename) unless File.exist?(filename)
  raise(NotAFile, filename) unless File.file?(filename)
  File.expand_path(filename)
end

Instance Method Details

#errors?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/exiftool.rb', line 63

def errors?
  @file2result.values.any? { |ea| ea.errors? }
end

#files_with_resultsObject



47
48
49
# File 'lib/exiftool.rb', line 47

def files_with_results
  @file2result.values.collect { |r| r.source_file unless r.errors? }.compact
end

#result_for(filename) ⇒ Object



43
44
45
# File 'lib/exiftool.rb', line 43

def result_for(filename)
  @file2result[self.class.expand_path(filename)]
end

#symbol_display_hashObject



59
60
61
# File 'lib/exiftool.rb', line 59

def symbol_display_hash
  first.symbol_display_hash
end

#to_display_hashObject



55
56
57
# File 'lib/exiftool.rb', line 55

def to_display_hash
  first.to_display_hash
end

#to_hashObject



51
52
53
# File 'lib/exiftool.rb', line 51

def to_hash
  first.to_hash
end