Class: APDM::Saxo::IPTC

Inherits:
Object
  • Object
show all
Defined in:
lib/apdm/saxo/iptc.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, path, options = {}) ⇒ IPTC

Returns a new instance of IPTC.



6
7
8
9
10
11
12
# File 'lib/apdm/saxo/iptc.rb', line 6

def initialize(name, path, options = {})
  self.source = options[:source]
  self.name = name
  self.path = path
  self.file = "#{path.chomp('/')}/#{name}"
  duplicate options[:file] if options[:file]
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



5
6
7
# File 'lib/apdm/saxo/iptc.rb', line 5

def file
  @file
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/apdm/saxo/iptc.rb', line 5

def name
  @name
end

#pathObject

Returns the value of attribute path.



5
6
7
# File 'lib/apdm/saxo/iptc.rb', line 5

def path
  @path
end

#sourceObject

Returns the value of attribute source.



5
6
7
# File 'lib/apdm/saxo/iptc.rb', line 5

def source
  @source
end

Instance Method Details

#delete_metadataObject



41
42
43
44
45
46
47
48
49
# File 'lib/apdm/saxo/iptc.rb', line 41

def 
  logger.info "Deleting metadata on #{file}."
  command = "exiv2 -d a delete #{file}"
  Open3.popen3(command) do |stdin, stdout, stderr|
    if stderr.gets
      logger.warn "Could not delete metadata: #{stderr.gets}"
    end
  end
end

#downloadObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/apdm/saxo/iptc.rb', line 28

def download
  logger.info "Downloading #{source}."
  begin
    File.open(file, "wb") do |f|
      o = open(source)
      r = o.read
      f.write r
    end
  rescue
    raise $!, "Problem downloading image with file: #{file} and source: #{source}. #{$!}"
  end
end

#duplicate(original) ⇒ Object



14
15
16
# File 'lib/apdm/saxo/iptc.rb', line 14

def duplicate(original)
  FileUtils.cp(original, file)
end

#loggerObject



18
19
20
# File 'lib/apdm/saxo/iptc.rb', line 18

def logger
  APDM::Saxo.logger
end

#write(data = {}) ⇒ Object



22
23
24
25
26
# File 'lib/apdm/saxo/iptc.rb', line 22

def write(data = {})
  download if source
  
  (data)
end

#write_metadata(data = {}) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/apdm/saxo/iptc.rb', line 51

def (data = {})
  logger.info "Writing metadata to #{file}."
  write_value('ObjectName', name)
  data.each do |key, value|
    write_value(key.to_s.capitalize, value)
  end
end

#write_value(key, value) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/apdm/saxo/iptc.rb', line 59

def write_value(key, value)
  command = "exiv2 -M \"set Iptc.Application2.#{key} String #{value}\" #{file}"
  Open3.popen3(command) do |stdin, stdout, stderr|
    if stderr.gets
      logger.warn "Failed to write #{value} to #{key}: #{stderr.gets}"
    end
  end
end