Module: Swagger::RackHelpers

Defined in:
lib/swagger/rack.rb

Instance Method Summary collapse

Instance Method Details

#request_spec(env: nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/swagger/rack.rb', line 5

def request_spec(env: nil)
  path = env['REQUEST_PATH']
  verb = env['REQUEST_METHOD'].downcase
  matching_paths = (@spec['paths'] || {}).map { |spec_path, spec|
    next unless spec[verb]
    matches = match_string(spec_path, path)
    next if matches.nil?
    {
      path: spec_path,
      captures: matches,
      spec: spec[verb]
    }
  }.compact

  case matching_paths.size
  when 0
    return nil
  when 1
    return matching_paths.first
  else
    spec_paths = matching_paths.map { |p| p[:path] }
    raise "Your API documentation is non-deterministic for the path: #{path} (#{spec_paths.join(', ')})"
  end
end