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

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
89
90
91
92
93
94
# 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}[#{k}]" : k)
    }.join("&")
  when String
    raise ArgumentError, "value must be a Hash" if prefix.nil?
    "#{Rack::Utils.escape(prefix)}=#{Rack::Utils.escape(value)}"
  when NilClass
    Rack::Utils.escape(prefix)
  else
    if value.respond_to?(:to_param)
      build_nested_query(value.to_param.to_s, prefix)
    else
      Rack::Utils.escape(prefix)
    end
  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



97
98
99
100
101
102
103
104
# File 'lib/rack/mount/utils.rb', line 97

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


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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rack/mount/utils.rb', line 107

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

  expression = Reginald.parse(regexp)

  unless RegexpWithNamedGroups.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

  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

.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