Method: Exiftool#initialize

Defined in:
lib/exiftool.rb

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

Returns a new instance of Exiftool.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/exiftool.rb', line 62

def initialize(filenames, exiftool_opts = '')
  @file2result = {}
  io_input = nil
  if filenames.is_a?(IO)
    io_input = filenames
    filenames = ['-']
  end

  filenames = [filenames] if filenames.is_a?(String) || filenames.is_a?(Pathname)
  return if filenames.empty?

  expanded_filenames = filenames.map do |f|
    f == '-' ? '-' : self.class.expand_path(f.to_s)
  end
  args = [
    self.class.command,
    *Shellwords.split(exiftool_opts),
    '-j',
    '-coordFormat', '%.8f',
    *expanded_filenames
  ]

  json = ''
  begin
    Open3.popen3(*args) do |stdin, stdout, _stderr, wait_thr|
      if io_input
        IO.copy_stream(io_input, stdin)
        stdin.close
      end
      json = stdout.read.to_s.chomp
      wait_thr.value
    end
  rescue Errno::ENOENT
    json = ''
  end

  raise ExiftoolNotInstalled if json == ''

  JSON.parse(json).each do |raw|
    result = Result.new(raw)
    @file2result[result.source_file] = result
  end
end