module Eeml include Eeml::Constants # One of the component classes of Environment. Represents the location of the environment. Environments can only have a single location object. class Location attr_accessor :name, :latitude, :longitude, :elevation attr_accessor :domain, :exposure, :disposition def initialize(options = {}) @name = options[:name] @latitude = options[:latitude] @longitude = options[:longitude] @elevation = options[:elevation] @domain = options[:domain] @exposure = options[:exposure] @disposition = options[:disposition] end end # One of the component classes of Environment. Represents an individual datastream, and provides access to it's value # and other attributes. Environments can have zero or more datastreams. class DataStream attr_accessor :identifier, :value, :tags, :min_value, :max_value, :unit_symbol, :unit_type, :unit_value def initialize(options = {}) @identifier = options[:identifier] @value = options[:value] @max_value = options[:max_value] @min_value = options[:min_value] @tags = [] @unit_symbol = options[:unit_symbol] @unit_type = options[:unit_type] @unit_value = options[:unit_value] end def to_s "identifier: '#{@identifier}', value: '#{@value}', min_value: '#{@min_value}', max_value: '#{@max_value}', tags: '#{@tags.join(", ")}'" end end # This class represents the main point of entry to this library. In general we will be creating instances of this class, either # by manually specifying the parameters, or probably more commonly, by passing in either an EEML or JSON formatted string which # will be parsed to initialize the object. class Environment attr_accessor :identifier, :updated, :creator attr_accessor :title, :description, :feed_url, :website, :email, :icon, :status attr_accessor :location attr_accessor :datastreams # Create a new Environment object by passing parameters in a hash (just like creating an ActiveRecord object) def initialize(options = {}) @datastreams = [] @identifier = options[:identifier] @creator = options[:creator] @title = options[:title] @status = options[:status] @feed_url = options[:feed_url] @description = options[:description] @website = options[:website] @email = options[:email] @icon = options[:icon] end def add_datastream(datastream) #TODO: consider checking for unique identifier datastreams << datastream end def remove_last_datastream datastreams.pop end # Create multiple Environment objects from an EEML string containing multiple `environment` stanzas. def self.new_list_from_eeml(xml_str) parser = ParserRegistry.get_xml_parser_for(xml_str) return parser.make_environments_from_xml(xml_str) end # Create a new Environment object from an EEML string. def self.new_from_eeml(xml_str) parser = ParserRegistry.get_xml_parser_for(xml_str) return parser.make_environment_from_xml(xml_str) end # Create a new Environment object from a JSON string. def self.new_from_json(json_str) parser = ParserRegistry.get_json_parser_for(json_str) return parser.make_environment_from_json(json_str) end def update_datastreams_from_csv_values!(csv_values) csv_values.each_with_index do |new_val, index| datastream = datastreams[index] #if we don't have an existing datastream at same index, create new one if datastream.nil? self.datastreams << DataStream.new(:identifier => index, :value => new_val, :ordering => index) else #we had a match, so update the existing datastream datastream.value = new_val end end # if the csv specifies FEWER feeds than the env currently has. if csv_values.size < self.datastreams.size # remove the excess from the end of the env's list. num_extra = datastreams.size - csv_values.size 1.upto(num_extra) { remove_last_datastream } end end def to_eeml(version = Constants::EEML_VERSION) outputter = OutputRegistry.get_xml_output_for(version) outputter.to_eeml(self) end def to_json outputter = OutputRegistry.get_json_output_for outputter.to_json(self) end end end