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
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
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rack/mount/utils.rb', line 72 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
55 56 57 |
# File 'lib/rack/mount/utils.rb', line 55 def escape_uri(uri) URI.escape(uri.to_s, UNSAFE_PCHAR) end |
.normalize_extended_expression(regexp) ⇒ Object
102 103 104 105 106 107 108 109 |
# File 'lib/rack/mount/utils.rb', line 102 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
112 113 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 |
# File 'lib/rack/mount/utils.rb', line 112 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 = Reginald.parse(regexp) unless Reginald.regexp_supports_named_captures? tag_captures = Proc.new do |group| group.each do |child| if child.is_a?(Reginald::Group) child.name = regexp.names[child.index] if child.index tag_captures.call(child) elsif child.is_a?(Reginald::Expression) tag_captures.call(child) end end end tag_captures.call(expression) end cache[regexp] = expression.freeze expression rescue Racc::ParseError, Reginald::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
97 98 99 |
# File 'lib/rack/mount/utils.rb', line 97 def regexp_anchored?(regexp) regexp.source =~ /\A(\\A|\^).*(\\Z|\$)\Z/m ? true : false end |
.unescape_uri(uri) ⇒ Object
61 62 63 |
# File 'lib/rack/mount/utils.rb', line 61 def unescape_uri(uri) URI.unescape(uri).force_encoding('utf-8') end |