module FeedOutput # :nodoc: #TODO: move away from mixins for this functionality. #TODO: This is v005-specific. module EnvironmentNode # :nodoc: include LibXML #assumes a logger() #assumes retrieved_at() #TODO: Move these (now unused) methods to the publisher (e.g. a rails controller?) which will decide where it's going to host the env, # and which will set the feed property accordingly before serialising the env. # def public_feed_url # return "http://#{DEFAULT_HOST}/api/#{self.id}.xml" # end # def public_csv_url # return "http://#{DEFAULT_HOST}/api/#{self.id}.csv" # end # def public_json_url # return "http://#{DEFAULT_HOST}/api/#{self.id}.json" # end def create_eeml_document doc = XML::Document.new eeml = doc.root = XML::Node.new('eeml') XML::Namespace.new(eeml, nil, Eeml::EEML_HREF) XML::Namespace.new(eeml, 'xsi', Eeml::XSI_NAMESPACE) eeml['version'] = Eeml::EEML_VERSION eeml['xsi:schemaLocation'] = Eeml::EEML_SCHEMA_LOCATION return doc end # Create a csv representation of this environment by iterating through all datastreams and returning # their values. def to_csv return self.datastreams.map { |d| d.value }.join(",") end # Create an xml representation of this environment. The document returned should be valid EEML. # def to_eeml(options = { :full => true }) # # Returns the public feed URL for this environment. # logger.debug("*** Returning eeml representation of environment: #{self.identifier}") # doc = create_eeml_document # doc.root << to_xml_node(options) # return doc.to_s(:encoding => XML::Encoding::UTF_8) # end def to_xml_node_old(options = { :full => true }) logger.debug("*** Creating xml node for environment: #{self.identifier}") environment_node = XML::Node.new('environment') #TODO: this was retrieved_at in the db, but it's 'updated' in the xml. Clarify w sam... # ... env.retrieved_at doesn't make much sense to clients generating eeml using the gem. environment_node['updated'] = self.updated.strftime(XML_TIME_FORMAT_STRING) unless self.updated.nil? environment_node['id'] = self.identifier.to_s unless self.identifier.blank? #TODO: write all these strings out safely for xml environment_node['creator'] = self.creator.to_s unless self.creator.blank? #TODO: these all should really appear, even when absent? likely make conditional. unless self.title.blank? environment_node << title_node = XML::Node.new('title') title_node << self.title end unless self.feed_url.blank? environment_node << feed_node = XML::Node.new('feed') feed_node << self.feed_url end unless self.status.blank? environment_node << status_node = XML::Node.new('status') status_node << self.status end unless self.description.blank? environment_node << description_node = XML::Node.new('description') description_node << self.description end unless self.icon.blank? environment_node << icon_node = XML::Node.new('icon') icon_node << self.icon end unless self.website.blank? environment_node << website_node = XML::Node.new('website') website_node << self.website end unless self.email.blank? environment_node << email_node = XML::Node.new('email') email_node << self.email end unless self.location.nil? environment_node << location_node = XML::Node.new('location') location_node['domain'] = self.location.domain location_node['exposure'] = self.location.exposure unless self.location.exposure.blank? location_node['disposition'] = self.location.disposition unless self.location.disposition.blank? unless self.location.name.blank? location_node << location_name_node = XML::Node.new('name') location_name_node << self.location.name end location_node << lat_node = XML::Node.new('lat') lat_node << self.location.latitude location_node << lng_node = XML::Node.new('lon') lng_node << self.location.longitude unless self.location.elevation.blank? location_node << elevation_node = XML::Node.new('ele') elevation_node << self.location.elevation end end if options[:full] == true self.datastreams.each do |datastream| environment_node << datastream.to_xml_node unless datastream.nil? end end return environment_node end end module DataStreamNode # :nodoc: include LibXML # Create and return an XML::Node object representing this datastream. This method creates all # required child nodes of the datastream object, including the list of tag elements, any units element # and the value. def to_xml_node datastream_node = XML::Node.new('data') datastream_node['id'] = self.identifier.to_s self.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'] = self.min_value.to_s unless self.min_value.to_s.empty? value_node['maxValue'] = self.max_value.to_s unless self.max_value.to_s.empty? value_node << self.value.to_s unless self.unit_value.to_s.empty? && self.unit_type.to_s.empty? && self.unit_symbol.to_s.empty? datastream_node << unit_node = XML::Node.new('unit') unit_node['type'] = self.unit_type.to_s unless self.unit_type.to_s.empty? unit_node['symbol'] = self.unit_symbol.to_s unless self.unit_symbol.to_s.empty? unit_node << self.unit_value.to_s unless self.unit_value.to_s.empty? end return datastream_node end end end