ruby-avm-library/lib/avm/creator.rb

124 lines
3.5 KiB
Ruby
Raw Normal View History

2011-03-07 21:38:20 +00:00
require 'avm/contact'
2011-03-09 15:14:31 +00:00
require 'nokogiri'
2011-03-07 21:38:20 +00:00
2011-03-07 17:39:47 +00:00
module AVM
2011-03-23 18:10:02 +00:00
# A container for Contacts (contributors to an image)
2011-03-07 17:39:47 +00:00
class Creator
2011-03-10 22:45:21 +00:00
attr_reader :contacts, :image
2011-03-07 21:38:20 +00:00
2011-03-10 22:45:21 +00:00
IPTC_CORE_FIELDS = [ :address, :city, :state, :zip, :country ]
PRIMARY_CONTACT_FIELDS = IPTC_CORE_FIELDS + [ :province, :postal_code ]
2011-03-11 18:27:17 +00:00
IPTC_MULTI_FIELD_MAP = [ [ :telephone, 'CiTelWork' ], [ :email, 'CiEmailWork' ] ]
IPTC_CORE_FIELD_ELEMENT_NAMES = %w{CiAdrExtadr CiAdrCity CiAdrRegion CiAdrPcode CiAdrCtry}
IPTC_CORE_FIELDS_AND_NAMES = IPTC_CORE_FIELDS.zip(IPTC_CORE_FIELD_ELEMENT_NAMES)
2011-03-09 15:14:31 +00:00
2011-03-11 18:27:17 +00:00
def initialize(image, given_contacts = [])
2011-03-07 17:39:47 +00:00
@options = {}
2011-03-11 18:27:17 +00:00
@contacts = given_contacts
2011-03-10 22:45:21 +00:00
@image = image
2011-03-07 17:39:47 +00:00
end
def merge!(hash)
@options.merge!(hash)
end
2011-03-11 18:27:17 +00:00
def length
contacts.length
end
def [](which)
contacts[which]
end
def to_a
contacts.sort.collect(&:to_h)
end
2011-03-07 17:39:47 +00:00
def method_missing(key, *opts)
2011-03-23 18:10:02 +00:00
if (key_to_s = key.to_s)[-1..-1] == '='
@options[key_to_s[0..-2].to_sym] = opts.first
2011-03-07 17:39:47 +00:00
else
2011-03-09 15:14:31 +00:00
if PRIMARY_CONTACT_FIELDS.include?(key)
primary_contact_field key
else
@options[key]
end
2011-03-07 17:39:47 +00:00
end
end
2011-03-07 21:38:20 +00:00
2011-03-10 22:45:21 +00:00
def add_to_document(document)
2011-03-11 18:27:17 +00:00
document.get_refs do |refs|
2011-03-10 22:45:21 +00:00
creator = refs[:dublin_core].add_child('<dc:creator><rdf:Seq></rdf:Seq></dc:creator>')
2011-03-09 15:14:31 +00:00
2011-03-10 22:45:21 +00:00
list = creator.at_xpath('.//rdf:Seq')
contact_info = refs[:iptc].add_child('<Iptc4xmpCore:CreatorContactInfo rdf:parseType="Resource" />').first
contacts.sort.each do |contact|
list.add_child(contact.to_creator_list_element)
end
if primary_contact
2011-03-11 18:27:17 +00:00
IPTC_MULTI_FIELD_MAP.each do |key, element_name|
2011-03-10 22:45:21 +00:00
contact_info.add_child "<Iptc4xmpCore:#{element_name}>#{contacts.sort.collect(&key).join(',')}</Iptc4xmpCore:#{element_name}>"
end
iptc_namespace = document.doc.root.namespace_scopes.find { |ns| ns.prefix == 'Iptc4xmpCore' }
2011-03-11 18:27:17 +00:00
IPTC_CORE_FIELDS_AND_NAMES.each do |key, element_name|
2011-03-10 22:45:21 +00:00
node = contact_info.document.create_element(element_name, primary_contact.send(key))
node.namespace = iptc_namespace
contact_info.add_child node
end
end
end
2011-03-09 15:14:31 +00:00
end
2011-03-11 18:27:17 +00:00
def from_xml(image, document)
contacts = []
document.get_refs do |refs|
refs[:dublin_core].search('.//dc:creator//rdf:li').each do |name|
contacts << { :name => name.text.strip }
2011-03-11 18:27:17 +00:00
end
IPTC_MULTI_FIELD_MAP.each do |key, element_name|
if node = refs[:iptc].at_xpath(".//Iptc4xmpCore:#{element_name}")
2011-03-11 18:27:17 +00:00
node.text.split(',').collect(&:strip).each_with_index do |value, index|
contacts[index][key] = value
end
end
end
IPTC_CORE_FIELDS_AND_NAMES.each do |key, element_name|
if node = refs[:iptc].at_xpath("//Iptc4xmpCore:#{element_name}")
2011-03-11 18:37:23 +00:00
contacts.each { |contact| contact[key] = node.text.strip }
2011-03-11 18:27:17 +00:00
end
end
end
2011-03-11 18:37:23 +00:00
if !(@contacts = contacts.collect { |contact| Contact.new(contact) }).empty?
@contacts.first.primary = true
end
2011-03-11 18:27:17 +00:00
end
2011-03-09 15:14:31 +00:00
def primary_contact
@contacts.find(&:primary) || @contacts.sort.first
end
def create_contact(info)
contact = Contact.new(info)
contacts << contact
contact
end
2011-03-07 21:38:20 +00:00
private
def primary_contact_field(field)
if contact = primary_contact
contact.send(field)
else
nil
end
end
2011-03-07 17:39:47 +00:00
end
end