# encoding: utf-8 module OneApm module Support class RenameRulesEngine module Rename class Segment < Base OA_RENAME_SEM_FIRST_MARK = 'first_'.freeze OA_RENAME_SEM_LAST_MARK = 'last_'.freeze OA_RENAME_SEM_VALUE_SEPARATOR = ','.freeze OA_RENAME_SEM_REGEX = /\d+,*/ OA_RENAME_SEPARATOR = '/'.freeze def segment(request) return '' if !state || value.nil? separated_path = request.uri.path.split(OA_RENAME_SEPARATOR) separated_path.delete('') if value.start_with?(OA_RENAME_SEM_FIRST_MARK) to = (value.gsub(OA_RENAME_SEM_FIRST_MARK,'').to_i - 1) separated_path[0..to].join(OA_RENAME_SEPARATOR) elsif value.start_with?(OA_RENAME_SEM_LAST_MARK) from = (value.gsub(OA_RENAME_SEM_LAST_MARK,'').to_i) from = separated_path.size if from > separated_path.size from = -from separated_path[from..-1].join(OA_RENAME_SEPARATOR) elsif OA_RENAME_SEM_REGEX.match(value) value_at = value.split(OA_RENAME_SEM_VALUE_SEPARATOR) value_at.map{|at| separated_path.at(at.to_i-1)}.join(OA_RENAME_SEPARATOR) end end end end end end end