require 'eeml/csv_parser_v1' require 'eeml/csv_parser_v2' require 'eeml/libxml_eeml_parser_v005' require 'eeml/libxml_eeml_parser_v051' require 'eeml/libxml_eeml_parser_v052' require 'eeml/json_environment_parser_v005' require 'eeml/json_environment_parser_v006' require 'eeml/json_environment_parser_v100' module Eeml class EnvironmentBuilder # :nodoc: include LibXML def self.build_from_xml(xml_str) begin xml = XML::Parser.string(xml_str).parse if xml.root.namespaces.default.to_s == Constants::EEML['0.5.1'][:href] parser = LibXMLEemlParserV051.new elsif xml.root.namespaces.default.to_s == Constants::EEML['0.5.2'][:href] parser = LibXMLEemlParserV052.new else parser = LibXMLEemlParserV005.new end rescue => e parser = LibXMLEemlParserV005.new end return parser.make_environment_from_xml(xml_str) end def self.build_list_from_xml(xml_str) parser = LibXMLEemlParserV005.new return parser.make_environments_from_xml(xml_str) end def self.build_from_json(json_str) json = JSON.parse(json_str) if Constants::EEML['0.5.0'][:versions].include? json["version"].to_s parser = JsonEnvironmentParserV005.new elsif Constants::EEML['0.5.1'][:versions].include? json["version"].to_s parser = JsonEnvironmentParserV005.new elsif Constants::JSON_API['1.0.0'][:versions].include? json["version"].to_s parser = JsonEnvironmentParserV100.new elsif Constants::JSON_API['0.6-alpha'][:versions].include? json["version"].to_s parser = JsonEnvironmentParserV006.new else raise Eeml::Exceptions::BadVersion# raise something so we jump to the rescue block below end return parser.make_environment_from_hash(json) rescue Eeml::Exceptions::BadVersion, TypeError raise JSON::ParserError, "Invalid version specification. Permitted versions are #{Constants::EEML['0.5.0'][:versions].first}, #{Constants::JSON_API["0.6-alpha"][:versions].first} and #{Constants::JSON_API["1.0.0"][:versions].first}" end def self.build_from_csv(csv_str, version = :v1) if version == :v2 parser = CsvParserV2 else parser = CsvParserV1 end parser.make_environment_from_csv(csv_str) end end end