module Eeml class LibXMLEemlOutputV005 # :nodoc: include LibXML include Constants # main method. creates an EEML 5 document for the given environment. def to_eeml(environment) doc = XML::Document.new eeml = doc.root = XML::Node.new('eeml') XML::Namespace.new(eeml, nil, EEML5_HREF) XML::Namespace.new(eeml, 'xsi', XSI_NAMESPACE) eeml['version'] = EEML5_VERSION eeml['xsi:schemaLocation'] = EEML5_SCHEMA_LOCATION eeml << xml_node_for_environment(environment) return doc.to_s(:encoding => XML::Encoding::UTF_8) end def xml_node_for_environment(environment) environment_node = XML::Node.new('environment') #TODO: write all these strings out safely for xml environment_node['updated'] = environment.updated.utc.iso8601 unless environment.updated.nil? environment_node['id'] = environment.identifier.to_s unless environment.identifier.blank? environment_node['creator'] = environment.creator.to_s unless environment.creator.blank? unless environment.title.blank? environment_node << title_node = XML::Node.new('title') title_node << environment.title end unless environment.feed_url.blank? environment_node << feed_node = XML::Node.new('feed') feed_node << environment.feed_url end unless environment.status.blank? environment_node << status_node = XML::Node.new('status') status_node << environment.status end unless environment.description.blank? environment_node << description_node = XML::Node.new('description') description_node << environment.description end unless environment.icon.blank? environment_node << icon_node = XML::Node.new('icon') icon_node << environment.icon end unless environment.website.blank? environment_node << website_node = XML::Node.new('website') website_node << environment.website end unless environment.email.blank? environment_node << email_node = XML::Node.new('email') email_node << environment.email end unless environment.location.nil? environment_node << location_node = XML::Node.new('location') location_node['domain'] = environment.location.domain location_node['exposure'] = environment.location.exposure unless environment.location.exposure.blank? location_node['disposition'] = environment.location.disposition unless environment.location.disposition.blank? unless environment.location.name.blank? location_node << location_name_node = XML::Node.new('name') location_name_node << environment.location.name end location_node << lat_node = XML::Node.new('lat') lat_node << environment.location.latitude location_node << lng_node = XML::Node.new('lon') lng_node << environment.location.longitude unless environment.location.elevation.blank? location_node << elevation_node = XML::Node.new('ele') elevation_node << environment.location.elevation end end environment.datastreams.each do |datastream| environment_node << datastream_to_xml_node(datastream) end return environment_node end def datastream_to_xml_node(datastream) datastream_node = XML::Node.new('data') datastream_node['id'] = datastream.identifier.to_s datastream.tags.each do |tag| tag_node = XML::Node.new('tag') tag_node << tag datastream_node << tag_node end datastream_node << value_node = XML::Node.new('value') value_node['minValue'] = datastream.min_value.to_s unless datastream.min_value.to_s.empty? value_node['maxValue'] = datastream.max_value.to_s unless datastream.max_value.to_s.empty? value_node << datastream.value.to_s unless datastream.unit_value.to_s.empty? && datastream.unit_type.to_s.empty? && datastream.unit_symbol.to_s.empty? datastream_node << unit_node = XML::Node.new('unit') unit_node['type'] = datastream.unit_type.to_s unless datastream.unit_type.to_s.empty? unit_node['symbol'] = datastream.unit_symbol.to_s unless datastream.unit_symbol.to_s.empty? unit_node << datastream.unit_value.to_s unless datastream.unit_value.to_s.empty? end return datastream_node end end end