2017-01-29 16:31:53 -05:00
|
|
|
module Gitlab
|
|
|
|
class RouteMap
|
2017-03-01 06:00:37 -05:00
|
|
|
FormatError = Class.new(StandardError)
|
2017-01-29 16:31:53 -05:00
|
|
|
|
|
|
|
def initialize(data)
|
|
|
|
begin
|
|
|
|
entries = YAML.safe_load(data)
|
|
|
|
rescue
|
2017-02-07 15:19:12 -05:00
|
|
|
raise FormatError, 'Route map is not valid YAML'
|
2017-01-29 16:31:53 -05:00
|
|
|
end
|
|
|
|
|
2017-02-07 15:19:12 -05:00
|
|
|
raise FormatError, 'Route map is not an array' unless entries.is_a?(Array)
|
2017-01-29 16:31:53 -05:00
|
|
|
|
|
|
|
@map = entries.map { |entry| parse_entry(entry) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def public_path_for_source_path(path)
|
2017-02-07 15:19:12 -05:00
|
|
|
mapping = @map.find { |mapping| mapping[:source] === path }
|
2017-01-29 16:31:53 -05:00
|
|
|
return unless mapping
|
|
|
|
|
|
|
|
path.sub(mapping[:source], mapping[:public])
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def parse_entry(entry)
|
2017-02-07 15:19:12 -05:00
|
|
|
raise FormatError, 'Route map entry is not a hash' unless entry.is_a?(Hash)
|
2017-06-02 13:11:26 -04:00
|
|
|
raise FormatError, 'Route map entry does not have a source key' unless entry.key?('source')
|
|
|
|
raise FormatError, 'Route map entry does not have a public key' unless entry.key?('public')
|
2017-01-29 16:31:53 -05:00
|
|
|
|
2017-02-07 15:19:12 -05:00
|
|
|
source_pattern = entry['source']
|
2017-01-29 16:31:53 -05:00
|
|
|
public_path = entry['public']
|
|
|
|
|
2017-02-07 15:19:12 -05:00
|
|
|
if source_pattern.start_with?('/') && source_pattern.end_with?('/')
|
|
|
|
source_pattern = source_pattern[1...-1].gsub('\/', '/')
|
2017-01-29 16:31:53 -05:00
|
|
|
|
2017-02-07 15:19:12 -05:00
|
|
|
begin
|
2017-02-07 15:26:00 -05:00
|
|
|
source_pattern = /\A#{source_pattern}\z/
|
2017-02-07 15:19:12 -05:00
|
|
|
rescue RegexpError => e
|
|
|
|
raise FormatError, "Route map entry source is not a valid regular expression: #{e}"
|
|
|
|
end
|
2017-01-29 16:31:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
{
|
2017-02-07 15:19:12 -05:00
|
|
|
source: source_pattern,
|
2017-01-29 16:31:53 -05:00
|
|
|
public: public_path
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|