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

81 lines
2.2 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
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-09 15:14:31 +00:00
2011-03-10 22:45:21 +00:00
def initialize(image)
2011-03-07 17:39:47 +00:00
@options = {}
2011-03-07 21:38:20 +00:00
@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
def method_missing(key, *opts)
if key.to_s[-1..-1] == '='
@options[key.to_s[0..-2].to_sym] = opts.first
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)
document.add_to_doc do |refs|
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
[ [ :telephone, 'CiTelWork' ], [ :email, 'CiEmailWork' ] ].each do |key, element_name|
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' }
IPTC_CORE_FIELDS.zip(%w{CiAdrExtadr CiAdrCity CiAdrRegion CiAdrPcode CiAdrCtry}).each do |key, element_name|
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
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