class Grape::Path

Represents a path to an endpoint.

Attributes

namespace[R]
raw_path[R]
settings[R]

Public Class Methods

new(raw_path, namespace, settings) click to toggle source
# File lib/grape/path.rb, line 10
def initialize(raw_path, namespace, settings)
  @raw_path = raw_path
  @namespace = namespace
  @settings = settings
end
prepare(raw_path, namespace, settings) click to toggle source
# File lib/grape/path.rb, line 4
def self.prepare(raw_path, namespace, settings)
  Path.new(raw_path, namespace, settings).path_with_suffix
end

Public Instance Methods

mount_path() click to toggle source
# File lib/grape/path.rb, line 16
def mount_path
  settings[:mount_path]
end
namespace?() click to toggle source
# File lib/grape/path.rb, line 32
def namespace?
  namespace && namespace.to_s =~ /^\S/ && namespace != '/'
end
path() click to toggle source
# File lib/grape/path.rb, line 50
def path
  Rack::Mount::Utils.normalize_path(parts.join('/'))
end
path?() click to toggle source
# File lib/grape/path.rb, line 36
def path?
  raw_path && raw_path.to_s =~ /^\S/ && raw_path != '/'
end
path_with_suffix() click to toggle source
# File lib/grape/path.rb, line 54
def path_with_suffix
  "#{path}#{suffix}"
end
root_prefix() click to toggle source
# File lib/grape/path.rb, line 20
def root_prefix
  split_setting(:root_prefix)
end
suffix() click to toggle source
# File lib/grape/path.rb, line 40
def suffix
  if uses_specific_format?
    "(.#{settings[:format]})"
  elsif !uses_path_versioning? || (namespace? || path?)
    '(.:format)'
  else
    '(/.:format)'
  end
end
to_s() click to toggle source
# File lib/grape/path.rb, line 58
def to_s
  path_with_suffix
end
uses_path_versioning?() click to toggle source
# File lib/grape/path.rb, line 28
def uses_path_versioning?
  !!(settings[:version] && settings[:version_options][:using] == :path)
end
uses_specific_format?() click to toggle source
# File lib/grape/path.rb, line 24
def uses_specific_format?
  !!(settings[:format] && settings[:content_types].size == 1)
end

Private Instance Methods

parts() click to toggle source
# File lib/grape/path.rb, line 64
def parts
  parts = [mount_path, root_prefix].compact
  parts << ':version' if uses_path_versioning?
  parts << namespace.to_s
  parts << raw_path.to_s
  parts.flatten.reject { |part| part == '/' }
end
split_setting(key) click to toggle source
# File lib/grape/path.rb, line 72
def split_setting(key)
  return if settings[key].nil?
  settings[key].to_s.split('/')
end