Sha256: 2628638a3c7a6eeeebd574d9d8b0342d87c246d4f2f22f7c77eb6689ccc09714

Contents?: true

Size: 1.81 KB

Versions: 2

Compression:

Stored size: 1.81 KB

Contents

module ViewModelGateway
  class XsdFilesGateway
    attr_reader :simple_type, :assessment_type, :xsd_dir_path, :glob_path

    API_PATH = "api/schemas/xml/".freeze
    XSD_DEFAULT_PATH = "/api/schemas/xml/**/".freeze

    def initialize(domain_arguments)
      @simple_type = domain_arguments.simple_type
      @assessment_type = domain_arguments.assessment_type
      @xsd_dir_path = domain_arguments.xsd_dir_path
      @dir_path = Dir.pwd if domain_arguments.gem_path.nil? || domain_arguments.gem_path.empty?
    end

    def schema_version(file)
      api_path_start = file.index(API_PATH) + API_PATH.length
      schema_version = file[api_path_start..].split("/").first
      sap_defnied_in_rdsap_dir?(file) ? "#{schema_version}/SAP" : schema_version
    end

    def xsd_files
      files = if @xsd_dir_path.nil? || @xsd_dir_path.empty?
                case @assessment_type.downcase
                when "sap"
                  sap_xsd_files
                when "rdsap"
                  rdsap_xsd_files
                when "cepc"
                  cepc_xsd_files
                end
              else
                Dir.glob("#{@dir_path}#{@xsd_dir_path}")
              end

      raise ViewModelBoundary::XsdFilesNotFound, "No xsd files were found in #{@glob_path} directory" if files.nil? || files.empty?

      files
    end

  private

    def sap_defnied_in_rdsap_dir?(file)
      assessment_type == "SAP" && file.end_with?("SAP-Domains.xsd")
    end

    def sap_xsd_files
      @glob_path = "#{@dir_path + XSD_DEFAULT_PATH}*-Domains.xsd"
      Dir.glob(@glob_path)
    end

    def rdsap_xsd_files
      @glob_path = "#{@dir_path + XSD_DEFAULT_PATH}*-Domains.xsd"
      Dir.glob(@glob_path)
    end

    def cepc_xsd_files
      @glob_path = "#{@dir_path + XSD_DEFAULT_PATH}Reported-Data.xsd"
      Dir.glob(@glob_path)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
epb_view_models-1.0.12 lib/view_model_gateway/xsd_files_gateway.rb
epb_view_models-1.0.11 lib/view_model_gateway/xsd_files_gateway.rb