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
-
.build_nested_query(value, prefix = nil) ⇒ Object
Taken from Rack 1.1.x to build nested query strings.
- .escape_uri(uri) ⇒ Object
- .normalize_extended_expression(regexp) ⇒ Object
-
.normalize_path(path) ⇒ Object
Normalizes URI path.
- .parse_regexp(regexp) ⇒ Object
-
.pop_trailing_nils!(ary) ⇒ Object
Removes trailing nils from array.
-
.regexp_anchored?(regexp) ⇒ Boolean
Determines whether the regexp must match the entire string.
- .unescape_uri(uri) ⇒ Object
Class Method Details
.build_nested_query(value, prefix = nil) ⇒ Object
Taken from Rack 1.1.x to build nested query strings
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rack/mount/utils.rb', line 74 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 |
.escape_uri(uri) ⇒ Object
57 58 59 |
# File 'lib/rack/mount/utils.rb', line 57 def escape_uri(uri) Parser.escape(uri.to_s, UNSAFE_PCHAR) end |
.normalize_extended_expression(regexp) ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'lib/rack/mount/utils.rb', line 104 def normalize_extended_expression(regexp) return regexp unless regexp. & 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("") # => "/"
25 26 27 28 29 30 31 |
# File 'lib/rack/mount/utils.rb', line 25 def normalize_path(path) path = "/#{path}" path.squeeze!('/') path.sub!(%r{/+\Z}, '') path = '/' if path == '' path end |
.parse_regexp(regexp) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/rack/mount/utils.rb', line 114 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_nils!(ary) ⇒ Object
Removes trailing nils from array.
pop_trailing_nils!([1, 2, 3]) # => [1, 2, 3]
pop_trailing_nils!([1, 2, 3, nil, nil]) # => [1, 2, 3]
pop_trailing_nils!([nil]) # => []
39 40 41 42 43 44 |
# File 'lib/rack/mount/utils.rb', line 39 def pop_trailing_nils!(ary) while ary.length > 0 && ary.last.nil? 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
99 100 101 |
# File 'lib/rack/mount/utils.rb', line 99 def regexp_anchored?(regexp) regexp.source =~ /\A(\\A|\^).*(\\Z|\$)\Z/m ? true : false end |
.unescape_uri(uri) ⇒ Object
63 64 65 |
# File 'lib/rack/mount/utils.rb', line 63 def unescape_uri(uri) Parser.unescape(uri).force_encoding('utf-8') end |