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

79 lines
1.5 KiB
Ruby
Raw Normal View History

2011-03-07 17:39:47 +00:00
require 'avm/creator'
2011-03-10 22:45:21 +00:00
require 'avm/xmp'
2011-03-07 17:39:47 +00:00
module AVM
class Image
attr_reader :creator
2011-03-11 18:59:34 +00:00
def initialize(options = {})
2011-03-10 22:45:21 +00:00
@creator = AVM::Creator.new(self)
2011-03-11 18:59:34 +00:00
@options = options
2011-03-07 17:39:47 +00:00
end
2011-03-09 15:14:31 +00:00
def to_xml
2011-03-10 22:45:21 +00:00
document = AVM::XMP.new
2011-03-09 15:14:31 +00:00
2011-03-10 22:45:21 +00:00
creator.add_to_document(document)
2011-03-09 15:14:31 +00:00
2011-03-14 16:35:29 +00:00
document.get_refs do |refs|
[ :title, :description ].each do |field|
refs[:dublin_core].add_child(%{<dc:#{field}>#{alt_li_tag(send(field))}</dc:#{field}>})
end
refs[:photoshop].add_child(%{<photoshop:Headline>#{headline}</photoshop:Headline>})
{
'Distance.Notes' => distance_notes,
'ReferenceURL' => reference_url,
'Credit' => credit,
'Date' => string_date,
'ID' => id,
}.each do |tag, value|
refs[:avm].add_child(%{<avm:#{tag}>#{value}</avm:#{tag}>}) if value
end
end
2011-03-10 22:45:21 +00:00
document.doc
2011-03-09 15:14:31 +00:00
end
2011-03-11 18:27:17 +00:00
2011-03-11 18:59:34 +00:00
def id
@options[:id]
end
def image_type
@options[:type]
end
def date
2011-03-14 16:35:29 +00:00
(Time.parse(@options[:date]) rescue nil)
end
def string_date
return nil if !date
date.strftime('%Y-%m-%d')
2011-03-11 18:59:34 +00:00
end
2011-03-11 19:04:50 +00:00
def distance
[ light_years, redshift ]
end
2011-03-11 18:27:17 +00:00
def self.from_xml(string)
document = AVM::XMP.from_string(string)
image = new
image.creator.from_xml(self, document)
image
end
2011-03-11 18:59:34 +00:00
def method_missing(method)
@options[method]
end
2011-03-14 16:35:29 +00:00
private
def alt_li_tag(text)
%{<rdf:Alt><rdf:li xml:lang="x-default">#{text}</rdf:li></rdf:Alt>}
end
2011-03-07 17:39:47 +00:00
end
end