Module: Rack::Mount::Utils

Defined in:
lib/rack/mount/utils.rb

Overview

Private utility methods used throughout Rack::Mount. – This module is a trash can. Try to move these functions into more appropriate contexts. ++

Constant Summary collapse

RESERVED_PCHAR =
':@&=+$,;%'
SAFE_PCHAR =
"#{URI::REGEXP::PATTERN::UNRESERVED}#{RESERVED_PCHAR}"
UNSAFE_PCHAR =
Regexp.new("[^#{SAFE_PCHAR}]", false, 'N').freeze
Parser =
URI.const_defined?(:Parser) ? URI::Parser.new : URI

Class Method Summary collapse

Class Method Details

.build_nested_query(value, prefix = nil) ⇒ Object

Taken from Rack 1.1.x to build nested query strings



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rack/mount/utils.rb', line 88

def build_nested_query(value, prefix = nil) #:nodoc:
  case value
  when Array
    value.map { |v|
      build_nested_query(v, "#{prefix}[]")
    }.join("&")
  when Hash
    value.map { |k, v|
      build_nested_query(v, prefix ? "#{prefix}[#{Rack::Utils.escape(k)}]" : Rack::Utils.escape(k))
    }.join("&")
  when String
    raise ArgumentError, "value must be a Hash" if prefix.nil?
    "#{prefix}=#{Rack::Utils.escape(value)}"
  else
    prefix
  end
end

.debug(msg) ⇒ Object



25
26
27
# File 'lib/rack/mount/utils.rb', line 25

def debug(msg)
  warn "Rack::Mount #{msg}" if $DEBUG
end

.escape_uri(uri) ⇒ Object



71
72
73
# File 'lib/rack/mount/utils.rb', line 71

def escape_uri(uri)
  Parser.escape(uri.to_s, UNSAFE_PCHAR)
end

.normalize_extended_expression(regexp) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/rack/mount/utils.rb', line 118

def normalize_extended_expression(regexp)
  return regexp unless regexp.options & Regexp::EXTENDED != 0
  source = regexp.source
  source.gsub!(/#.+$/, '')
  source.gsub!(/\s+/, '')
  source.gsub!(/\\\//, '/')
  Regexp.compile(source)
end

.normalize_path(path) ⇒ Object

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("")      # => "/"


38
39
40
41
42
43
44
# File 'lib/rack/mount/utils.rb', line 38

def normalize_path(path)
  path = "/#{path}"
  path.squeeze!('/')
  path.sub!(%r{/+\Z}, '')
  path = '/' if path == ''
  path
end

.parse_regexp(regexp) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# 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) ⇒ Object

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!([""])                # => []


53
54
55
56
57
58
# 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) ⇒ Boolean

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

Returns:

  • (Boolean)


113
114
115
# File 'lib/rack/mount/utils.rb', line 113

def regexp_anchored?(regexp)
 regexp.source =~ /\A(\\A|\^).*(\\Z|\$)\Z/m ? true : false
end

.silence_debugObject



17
18
19
20
21
22
# 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) ⇒ Object



77
78
79
# File 'lib/rack/mount/utils.rb', line 77

def unescape_uri(uri)
  Parser.unescape(uri).force_encoding('utf-8')
end