Rack::Mount::Utils

Private utility methods used throughout Rack::Mount.

Public Class Methods

debug(msg) click to toggle source
# File lib/rack/mount/utils.rb, line 25
def debug(msg)
  warn "Rack::Mount #{msg}" if $DEBUG
end
escape_uri(uri) click to toggle source
# File lib/rack/mount/utils.rb, line 71
def escape_uri(uri)
  Parser.escape(uri.to_s, UNSAFE_PCHAR)
end
normalize_extended_expression(regexp) click to toggle source
# File lib/rack/mount/utils.rb, line 118
def normalize_extended_expression(regexp)
  return regexp if (regexp.options & Regexp::EXTENDED) == 0
  source = regexp.source
  source.gsub!(/#.+$/, '')
  source.gsub!(/\s+/, '')
  source.gsub!(/\\\//, '/')
  Regexp.compile(source)
end
normalize_path(path) click to toggle source

Normalizes URI path.

Strips off trailing slash and ensures there is a leading slash.

normalize_path("/foo")  # => "/foo"
normalize_path("/foo/") # => "/foo"
normalize_path("foo")   # => "/foo"
normalize_path("")      # => "/"
# File lib/rack/mount/utils.rb, line 38
def normalize_path(path)
  path = "/#{path}"
  path.squeeze!('/')
  path.sub!(%{/+\Z}, '')
  path = '/' if path == ''
  path
end
parse_regexp(regexp) click to toggle source
# File lib/rack/mount/utils.rb, line 128
def parse_regexp(regexp)
  cache = @@_parse_regexp_cache ||= {}

  if expression = cache[regexp]
    return expression
  end

  unless regexp.is_a?(RegexpWithNamedGroups)
    regexp = RegexpWithNamedGroups.new(regexp)
  end

  expression = Regin.parse(regexp)

  unless Regin.regexp_supports_named_captures?
    tag_captures = Proc.new do |group|
      case group
      when Regin::Group
        # TODO: dup instead of mutating
        group.instance_variable_set('@name', regexp.names[group.index]) if group.index
        tag_captures.call(group.expression)
      when Regin::Expression
        group.each { |child| tag_captures.call(child) }
      end
    end
    tag_captures.call(expression)
  end

  cache[regexp] = expression.freeze
  expression
rescue Racc::ParseError, Regin::Parser::ScanError
  []
end
pop_trailing_blanks!(ary) click to toggle source

Removes trailing nils from array.

pop_trailing_blanks!([1, 2, 3])           # => [1, 2, 3]
pop_trailing_blanks!([1, 2, 3, nil, ""])  # => [1, 2, 3]
pop_trailing_blanks!([nil])               # => []
pop_trailing_blanks!([""])                # => []
# File lib/rack/mount/utils.rb, line 53
def pop_trailing_blanks!(ary)
  while ary.length > 0 && (ary.last.nil? || ary.last == '')
    ary.pop
  end
  ary
end
regexp_anchored?(regexp) click to toggle source

Determines whether the regexp must match the entire string.

regexp_anchored?(/^foo$/) # => true
regexp_anchored?(/foo/)   # => false
regexp_anchored?(/^foo/)  # => false
regexp_anchored?(/foo$/)  # => false
# File lib/rack/mount/utils.rb, line 113
def regexp_anchored?(regexp)
 regexp.source =~ /\A(\\A|\^).*(\\Z|\$)\Z/ ? true : false
end
silence_debug() click to toggle source
# File lib/rack/mount/utils.rb, line 17
def silence_debug
  old_debug, $DEBUG = $DEBUG, nil
  yield
ensure
  $DEBUG = old_debug
end
unescape_uri(uri) click to toggle source
# File lib/rack/mount/utils.rb, line 77
def unescape_uri(uri)
  Parser.unescape(uri).force_encoding('utf-8')
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.