Sha256: f4312f72ca30bee430cba6f27945a39923e60999418f512ea860f5e25ce6c260

Contents?: true

Size: 1002 Bytes

Versions: 7

Compression:

Stored size: 1002 Bytes

Contents

module Tomograph
  class Path
    def initialize(path)
      unless path && !path.empty?
        @path = ''
        return
      end
      @path = path
      @path = delete_till_the_end('{&')
      @path = delete_till_the_end('{?')
      @path = cut_off_query_params
      @path = remove_the_slash_at_the_end
    end

    def match(find_path)
      regexp =~ find_path
    end

    def regexp
      return @regexp if @regexp

      str = Regexp.escape(to_s)
      str = str.gsub(/\\{\w+\\}/, '[^&=\/]+')
      str = "\\A#{str}\\z"
      @regexp = Regexp.new(str)
    end

    def to_s
      @path
    end

    private

    def delete_till_the_end(beginning_with)
      path_index = @path.index(beginning_with)
      path_index ||= 0
      path_index -= 1
      @path.slice(0..path_index)
    end

    def remove_the_slash_at_the_end
      if @path[-1] == '/'
        @path[0..-2]
      else
        @path
      end
    end

    def cut_off_query_params
      @path.gsub(/\?.*\z/, '')
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
tomograph-2.2.0 lib/tomograph/path.rb
tomograph-2.1.0 lib/tomograph/path.rb
tomograph-2.0.1 lib/tomograph/path.rb
tomograph-2.0.0 lib/tomograph/path.rb
tomograph-1.2.0 lib/tomograph/path.rb
tomograph-1.1.0 lib/tomograph/path.rb
tomograph-1.0.0 lib/tomograph/path.rb