Sha256: 2aba4ade593d4e15e4ba7899a5646df187402ee4243707a949137daa5c7721c3

Contents?: true

Size: 1.74 KB

Versions: 94

Compression:

Stored size: 1.74 KB

Contents

module Katello
  module Util
    class PathWithSubstitutions
      include Comparable

      attr_accessor :substitutions
      attr_accessor :path

      #path /content/rhel/server/$arch/$releasever/os
      #substitutions  {$arch => 'x86_64'}
      def initialize(path, substitutions)
        @substitutions = substitutions
        @path = path
        @resolved = []
      end

      def split_path
        @split ||= path.split('/')
      end

      def substitutions_needed
        # e.g. if content_url = "/content/dist/rhel/server/7/$releasever/$basearch/kickstart"
        #      return ['releasever', 'basearch']
        split_path.map { |word| word.start_with?('$') ? word[1..-1] : nil }.compact
      end

      def substitutable?
        path =~ /^(.*?)\$([^\/]*)/
      end

      def resolve_substitutions(cdn_resource)
        if @resolved.empty? && path =~ /^(.*?)\$([^\/]*)/
          base_path, var = Regexp.last_match[1], Regexp.last_match[2]
          cdn_resource.fetch_substitutions(base_path).compact.map do |value|
            new_substitutions = substitutions.merge(var => value)
            new_path = path.sub("$#{var}", value)
            @resolved << PathWithSubstitutions.new(new_path, new_substitutions)
          end
        end
        @resolved
      end

      def unused_substitutions
        substitutions.keys.reject do |key|
          path.include?("$#{key}") || split_path.include?(substitutions[key])
        end
      end

      def apply_substitutions
        substitutions.reduce(path) do |url, (key, value)|
          url.gsub("$#{key}", value)
        end
      end

      def <=>(other)
        key1 = path + substitutions.to_s
        key2 = other.path + other.substitutions.to_s
        key1 <=> key2
      end
    end
  end
end

Version data entries

94 entries across 94 versions & 1 rubygems

Version Path
katello-4.2.2 app/lib/katello/util/path_with_substitutions.rb
katello-4.2.1 app/lib/katello/util/path_with_substitutions.rb
katello-4.2.0.1 app/lib/katello/util/path_with_substitutions.rb
katello-4.1.4 app/lib/katello/util/path_with_substitutions.rb
katello-4.0.3 app/lib/katello/util/path_with_substitutions.rb
katello-4.2.0.1.rc3 app/lib/katello/util/path_with_substitutions.rb
katello-3.18.5 app/lib/katello/util/path_with_substitutions.rb
katello-4.2.0.1.rc2 app/lib/katello/util/path_with_substitutions.rb
katello-4.2.0.rc2 app/lib/katello/util/path_with_substitutions.rb
katello-4.1.3 app/lib/katello/util/path_with_substitutions.rb
katello-4.2.0.rc1 app/lib/katello/util/path_with_substitutions.rb
katello-4.0.2.1 app/lib/katello/util/path_with_substitutions.rb
katello-4.1.2.1 app/lib/katello/util/path_with_substitutions.rb
katello-4.0.2 app/lib/katello/util/path_with_substitutions.rb
katello-4.1.2 app/lib/katello/util/path_with_substitutions.rb
katello-3.18.4 app/lib/katello/util/path_with_substitutions.rb
katello-4.1.1 app/lib/katello/util/path_with_substitutions.rb
katello-4.1.0 app/lib/katello/util/path_with_substitutions.rb
katello-4.0.1.2 app/lib/katello/util/path_with_substitutions.rb
katello-3.18.3.1 app/lib/katello/util/path_with_substitutions.rb