Sha256: 79882e63d87ee06b58833cd4781ea7368214324d924e761f9ffd0c6bdf3cfdc7

Contents?: true

Size: 1.09 KB

Versions: 7

Compression:

Stored size: 1.09 KB

Contents

module Tomograph
  class Path
    attr_reader :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

    def ==(other)
      other.instance_of?(self.class) && (other.path == 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-3.2.4 lib/tomograph/path.rb
tomograph-3.2.1 lib/tomograph/path.rb
tomograph-3.2.0 lib/tomograph/path.rb
tomograph-3.1.6 lib/tomograph/path.rb
tomograph-3.1.5 lib/tomograph/path.rb
tomograph-3.1.4 lib/tomograph/path.rb
tomograph-3.1.3 lib/tomograph/path.rb