Class: GrapeSwagger::DocMethods::PathString

Inherits:
Object
  • Object
show all
Defined in:
lib/grape-swagger/doc_methods/path_string.rb

Class Method Summary collapse

Class Method Details

.build(route, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/grape-swagger/doc_methods/path_string.rb', line 7

def build(route, options = {})
  path = route.path.dup
  # always removing format
  path.sub!(/\(\.\w+?\)$/, '')
  path.sub!('(.:format)', '')

  # ... format path params
  path.gsub!(/:(\w+)/, '{\1}')

  # set item from path, this could be used for the definitions object
  path_name = path.gsub(%r{/{(.+?)}}, '').split('/').last
  item = path_name.present? ? path_name.singularize.underscore.camelize : 'Item'

  if route.version && options[:add_version]
    version = GrapeSwagger::DocMethods::Version.get(route)
    version = version.first while version.is_a?(Array)
    path.sub!('{version}', version.to_s)
  else
    path.sub!('/{version}', '')
  end

  path = "#{OptionalObject.build(:base_path, options)}#{path}" if options[:add_base_path]

  [item, path.start_with?('/') ? path : "/#{path}"]
end