Class: Grape::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/grape/path.rb

Overview

Represents a path to an endpoint.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_path, namespace, settings) ⇒ Path

Returns a new instance of Path.



12
13
14
15
16
# File 'lib/grape/path.rb', line 12

def initialize(raw_path, namespace, settings)
  @raw_path = raw_path
  @namespace = namespace
  @settings = settings
end

Instance Attribute Details

#namespaceObject (readonly)

Returns the value of attribute namespace.



10
11
12
# File 'lib/grape/path.rb', line 10

def namespace
  @namespace
end

#raw_pathObject (readonly)

Returns the value of attribute raw_path.



10
11
12
# File 'lib/grape/path.rb', line 10

def raw_path
  @raw_path
end

#settingsObject (readonly)

Returns the value of attribute settings.



10
11
12
# File 'lib/grape/path.rb', line 10

def settings
  @settings
end

Class Method Details

.prepare(raw_path, namespace, settings) ⇒ Object



6
7
8
# File 'lib/grape/path.rb', line 6

def self.prepare(raw_path, namespace, settings)
  Path.new(raw_path, namespace, settings)
end

Instance Method Details

#mount_pathObject



18
19
20
# File 'lib/grape/path.rb', line 18

def mount_path
  settings[:mount_path]
end

#namespace?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/grape/path.rb', line 42

def namespace?
  namespace && namespace.to_s =~ /^\S/ && namespace != '/'
end

#pathObject



60
61
62
# File 'lib/grape/path.rb', line 60

def path
  Grape::Router.normalize_path(parts.join('/'))
end

#path?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/grape/path.rb', line 46

def path?
  raw_path && raw_path.to_s =~ /^\S/ && raw_path != '/'
end

#path_with_suffixObject



64
65
66
# File 'lib/grape/path.rb', line 64

def path_with_suffix
  "#{path}#{suffix}"
end

#root_prefixObject



22
23
24
# File 'lib/grape/path.rb', line 22

def root_prefix
  split_setting(:root_prefix)
end

#suffixObject



50
51
52
53
54
55
56
57
58
# File 'lib/grape/path.rb', line 50

def suffix
  if uses_specific_format?
    "(.#{settings[:format]})"
  elsif !uses_path_versioning? || (namespace? || path?)
    '(.:format)'
  else
    '(/.:format)'
  end
end

#to_sObject



68
69
70
# File 'lib/grape/path.rb', line 68

def to_s
  path_with_suffix
end

#uses_path_versioning?Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
# File 'lib/grape/path.rb', line 34

def uses_path_versioning?
  if settings.key?(:version) && settings[:version_options] && settings[:version_options].key?(:using)
    (settings[:version] && settings[:version_options][:using] == :path)
  else
    false
  end
end

#uses_specific_format?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
# File 'lib/grape/path.rb', line 26

def uses_specific_format?
  if settings.key?(:format) && settings.key?(:content_types)
    (settings[:format] && Array(settings[:content_types]).size == 1)
  else
    false
  end
end