Skip to content
Snippets Groups Projects
route_map.rb 1.39 KiB
Newer Older
  • Learn to ignore specific revisions
  • Douwe Maan's avatar
    Douwe Maan committed
    module Gitlab
      class RouteMap
        class FormatError < StandardError; end
    
        def initialize(data)
          begin
            entries = YAML.safe_load(data)
          rescue
    
            raise FormatError, 'Route map is not valid YAML'
    
    Douwe Maan's avatar
    Douwe Maan committed
          end
    
    
          raise FormatError, 'Route map is not an array' unless entries.is_a?(Array)
    
    Douwe Maan's avatar
    Douwe Maan committed
    
          @map = entries.map { |entry| parse_entry(entry) }
        end
    
        def public_path_for_source_path(path)
    
          mapping = @map.find { |mapping| mapping[:source] === path }
    
    Douwe Maan's avatar
    Douwe Maan committed
          return unless mapping
    
          path.sub(mapping[:source], mapping[:public])
        end
    
        private
    
        def parse_entry(entry)
    
          raise FormatError, 'Route map entry is not a hash' unless entry.is_a?(Hash)
          raise FormatError, 'Route map entry does not have a source key' unless entry.has_key?('source')
          raise FormatError, 'Route map entry does not have a public key' unless entry.has_key?('public')
    
    Douwe Maan's avatar
    Douwe Maan committed
    
    
          source_pattern = entry['source']
    
    Douwe Maan's avatar
    Douwe Maan committed
          public_path = entry['public']
    
    
          if source_pattern.start_with?('/') && source_pattern.end_with?('/')
            source_pattern = source_pattern[1...-1].gsub('\/', '/')
    
    Douwe Maan's avatar
    Douwe Maan committed
    
    
    Douwe Maan's avatar
    Douwe Maan committed
              source_pattern = /\A#{source_pattern}\z/
    
            rescue RegexpError => e
              raise FormatError, "Route map entry source is not a valid regular expression: #{e}"
            end
    
    Douwe Maan's avatar
    Douwe Maan committed
          end
    
          {
    
            source: source_pattern,
    
    Douwe Maan's avatar
    Douwe Maan committed
            public: public_path
          }
        end
      end
    end