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

141 lines
3.7 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-15 14:55:09 +00:00
require 'avm/image_type'
require 'avm/image_quality'
2011-03-16 14:41:01 +00:00
require 'avm/observation'
2011-03-07 17:39:47 +00:00
module AVM
class Image
2011-03-15 14:55:09 +00:00
DUBLIN_CORE_FIELDS = [ :title, :description ]
2011-03-16 14:41:01 +00:00
AVM_SINGLE_FIELDS = [ 'Distance.Notes', 'Spectral.Notes', 'ReferenceURL', 'Credit', 'Date', 'ID', 'Type', 'Image.ProductQuality' ]
AVM_SINGLE_METHODS = [ :distance_notes, :spectral_notes, :reference_url, :credit, :date, :id, :type, :quality ]
AVM_SINGLES = AVM_SINGLE_FIELDS.zip(AVM_SINGLE_METHODS)
2011-03-15 14:55:09 +00:00
2011-03-15 20:56:27 +00:00
attr_reader :creator, :observations
2011-03-07 17:39:47 +00:00
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-15 20:56:27 +00:00
@observations = []
end
def create_observation(options)
observation = Observation.new(self, options)
@observations << observation
observation
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-15 20:56:27 +00:00
Observation.add_to_document(document, observations)
2011-03-09 15:14:31 +00:00
2011-03-14 16:35:29 +00:00
document.get_refs do |refs|
2011-03-15 14:55:09 +00:00
DUBLIN_CORE_FIELDS.each do |field|
2011-03-14 16:35:29 +00:00
refs[:dublin_core].add_child(%{<dc:#{field}>#{alt_li_tag(send(field))}</dc:#{field}>})
end
refs[:photoshop].add_child(%{<photoshop:Headline>#{headline}</photoshop:Headline>})
2011-03-16 14:41:01 +00:00
AVM_SINGLE_FIELDS.zip([distance_notes, spectral_notes, reference_url, credit, string_date, id, image_type, image_quality]).each do |tag, value|
2011-03-15 14:55:09 +00:00
refs[:avm].add_child(%{<avm:#{tag}>#{value.to_s}</avm:#{tag}>}) if value
end
distance_nodes = []
distance_nodes << rdf_li(light_years) if light_years
if redshift
distance_nodes << rdf_li('-') if distance_nodes.empty?
distance_nodes << rdf_li(redshift)
end
if !distance_nodes.empty?
refs[:avm].add_child(%{<avm:Distance><rdf:Seq>#{distance_nodes.join}</rdf:Seq></avm:Distance>})
2011-03-14 16:35:29 +00:00
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
2011-03-15 14:55:09 +00:00
(AVM::ImageType.const_get(@options[:type].to_sym).new rescue nil)
end
def image_quality
(AVM::ImageQuality.const_get(@options[:quality].to_sym).new rescue nil)
2011-03-11 18:59:34 +00:00
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)
2011-03-15 14:55:09 +00:00
options = {}
document.get_refs do |refs|
DUBLIN_CORE_FIELDS.each do |field|
if node = refs[:dublin_core].at_xpath(".//dc:#{field}//rdf:li[1]")
options[field] = node.text
end
end
2011-03-16 14:41:01 +00:00
AVM_SINGLES.each do |tag, field|
2011-03-15 14:55:09 +00:00
if node = refs[:avm].at_xpath("./avm:#{tag}")
options[field] = node.text
end
end
if node = refs[:photoshop].at_xpath('./photoshop:Headline')
options[:headline] = node.text
end
if node = refs[:avm].at_xpath('./avm:Distance')
2011-03-15 15:16:10 +00:00
list_values = node.search('.//rdf:li').collect { |li| li.text }
2011-03-15 14:55:09 +00:00
case list_values.length
when 1
options[:light_years] = list_values.first
when 2
options[:light_years] = (list_values.first == '-') ? nil : list_values.first
2011-03-15 15:16:10 +00:00
options[:redshift] = list_values.last
2011-03-15 14:55:09 +00:00
end
end
end
image = new(options)
2011-03-11 18:27:17 +00:00
image.creator.from_xml(self, document)
2011-03-16 14:22:59 +00:00
Observation.from_xml(image, document)
2011-03-11 18:27:17 +00:00
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-15 14:55:09 +00:00
def rdf_li(text)
%{<rdf:li>#{text}</rdf:li>}
end
2011-03-07 17:39:47 +00:00
end
end