Sha256: 69ddbf26307db4c70e7aaaf7b666ee673db7571a234abe8294d738dd95ee56de

Contents?: true

Size: 1.08 KB

Versions: 15

Compression:

Stored size: 1.08 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 and 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

15 entries across 15 versions & 1 rubygems

Version Path
tomograph-3.1.2 lib/tomograph/path.rb
tomograph-3.1.1 lib/tomograph/path.rb
tomograph-3.1.0 lib/tomograph/path.rb
tomograph-3.0.1 lib/tomograph/path.rb
tomograph-3.0.0 lib/tomograph/path.rb
tomograph-2.5.4 lib/tomograph/path.rb
tomograph-2.5.3 lib/tomograph/path.rb
tomograph-2.5.2 lib/tomograph/path.rb
tomograph-2.5.1 lib/tomograph/path.rb
tomograph-2.5.0 lib/tomograph/path.rb
tomograph-2.4.2 lib/tomograph/path.rb
tomograph-2.4.1 lib/tomograph/path.rb
tomograph-2.4.0 lib/tomograph/path.rb
tomograph-2.3.0 lib/tomograph/path.rb
tomograph-2.2.1 lib/tomograph/path.rb