Sha256: 7fbe023180a9dfcf68cd2315028d3c2b915e0ea0b7f9c2b9a5697c06437960ec

Contents?: true

Size: 1.91 KB

Versions: 1

Compression:

Stored size: 1.91 KB

Contents

require 'eeml/csv_parser_v1'
require 'eeml/csv_parser_v2'
require 'eeml/libxml_eeml_parser_v005'
require 'eeml/libxml_eeml_parser_v051'
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 
        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 json["version"].to_i == Constants::EEML['0.5.0'][:version].to_i
        parser = JsonEnvironmentParserV005.new
      elsif json["version"] == Constants::JSON_API['1.0.0'][:version]
        parser = JsonEnvironmentParserV100.new
      elsif json["version"].to_f == Constants::JSON_API['0.6-alpha'][:version].to_f
        parser = JsonEnvironmentParserV006.new
      else
        raise # raise something so we jump to the rescue block below
      end
      return parser.make_environment_from_hash(json)
    rescue
      raise JSON::ParserError, "Invalid version specification. Permitted versions are #{Constants::EEML['0.5.0'][:version]}, #{Constants::JSON_API["0.6-alpha"][:version]} and #{Constants::JSON_API["1.0.0"][:version]}"
    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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
eeml-0.0.29 lib/eeml/environment_builder.rb