Module: Grape::DSL::Routing::ClassMethods
- Defined in:
- lib/grape/dsl/routing.rb
Instance Attribute Summary collapse
-
#endpoints ⇒ Object
readonly
Returns the value of attribute endpoints.
Instance Method Summary collapse
- #build_with(build_with) ⇒ Object
- #do_not_document! ⇒ Object
-
#do_not_route_head! ⇒ Object
Do not route HEAD requests to GET requests automatically.
-
#do_not_route_options! ⇒ Object
Do not automatically route OPTIONS.
- #lint! ⇒ Object
- #mount(mounts, *opts) ⇒ Object
-
#namespace(space = nil, options = {}, &block) ⇒ Object
(also: #group, #resource, #resources, #segment)
Declare a “namespace”, which prefixes all subordinate routes with its name.
-
#prefix(prefix = nil) ⇒ Object
Define a root URL prefix for your entire API.
- #reset_endpoints! ⇒ Object
-
#reset_routes! ⇒ Object
Remove all defined routes.
-
#route(methods, paths = ['/'], route_options = {}, &block) ⇒ Object
Defines a route that will be recognized by the Grape API.
-
#route_param(param, options = {}, &block) ⇒ Object
This method allows you to quickly define a parameter route segment in your API.
-
#routes ⇒ Object
An array of API routes.
-
#scope(_name = nil, &block) ⇒ Object
Create a scope without affecting the URL.
-
#version(*args, &block) ⇒ Object
Specify an API version.
-
#versions ⇒ Object
Array of defined versions.
Instance Attribute Details
#endpoints ⇒ Object (readonly)
Returns the value of attribute endpoints.
10 11 12 |
# File 'lib/grape/dsl/routing.rb', line 10 def endpoints @endpoints end |
Instance Method Details
#build_with(build_with) ⇒ Object
70 71 72 |
# File 'lib/grape/dsl/routing.rb', line 70 def build_with(build_with) namespace_inheritable(:build_params_with, build_with) end |
#do_not_document! ⇒ Object
88 89 90 |
# File 'lib/grape/dsl/routing.rb', line 88 def do_not_document! namespace_inheritable(:do_not_document, true) end |
#do_not_route_head! ⇒ Object
Do not route HEAD requests to GET requests automatically.
75 76 77 |
# File 'lib/grape/dsl/routing.rb', line 75 def do_not_route_head! namespace_inheritable(:do_not_route_head, true) end |
#do_not_route_options! ⇒ Object
Do not automatically route OPTIONS.
80 81 82 |
# File 'lib/grape/dsl/routing.rb', line 80 def namespace_inheritable(:do_not_route_options, true) end |
#lint! ⇒ Object
84 85 86 |
# File 'lib/grape/dsl/routing.rb', line 84 def lint! namespace_inheritable(:lint, true) end |
#mount(mounts, *opts) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 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 132 133 |
# File 'lib/grape/dsl/routing.rb', line 92 def mount(mounts, *opts) mounts = { mounts => '/' } unless mounts.respond_to?(:each_pair) mounts.each_pair do |app, path| if app.respond_to?(:mount_instance) opts_with = opts.any? ? opts.first[:with] : {} mount({ app.mount_instance(configuration: opts_with) => path }, *opts) next end in_setting = inheritable_setting if app.respond_to?(:inheritable_setting, true) mount_path = Grape::Router.normalize_path(path) app.top_level_setting.namespace_stackable[:mount_path] = mount_path app.inherit_settings(inheritable_setting) in_setting = app.top_level_setting app.change! change! end # When trying to mount multiple times the same endpoint, remove the previous ones # from the list of endpoints if refresh_already_mounted parameter is true refresh_already_mounted = opts.any? ? opts.first[:refresh_already_mounted] : false if refresh_already_mounted && !endpoints.empty? endpoints.delete_if do |endpoint| endpoint.[:app].to_s == app.to_s end end endpoints << Grape::Endpoint.new( in_setting, method: :any, path: path, app: app, route_options: { anchor: false }, forward_match: !app.respond_to?(:inheritable_setting), for: self ) end end |
#namespace(space = nil, options = {}, &block) ⇒ Object Also known as: group, resource, resources, segment
Declare a “namespace”, which prefixes all subordinate routes with its name. Any endpoints within a namespace, group, resource or segment, etc., will share their parent context as well as any configuration done in the namespace context.
185 186 187 188 189 190 191 192 193 |
# File 'lib/grape/dsl/routing.rb', line 185 def namespace(space = nil, = {}, &block) return Namespace.joined_space_path(namespace_stackable(:namespace)) unless space || block within_namespace do nest(block) do namespace_stackable(:namespace, Namespace.new(space, )) if space end end end |
#prefix(prefix = nil) ⇒ Object
Define a root URL prefix for your entire API.
56 57 58 |
# File 'lib/grape/dsl/routing.rb', line 56 def prefix(prefix = nil) namespace_inheritable(:root_prefix, prefix&.to_s) end |
#reset_endpoints! ⇒ Object
211 212 213 |
# File 'lib/grape/dsl/routing.rb', line 211 def reset_endpoints! @endpoints = [] end |
#reset_routes! ⇒ Object
Remove all defined routes.
206 207 208 209 |
# File 'lib/grape/dsl/routing.rb', line 206 def reset_routes! endpoints.each(&:reset_routes!) @routes = nil end |
#route(methods, paths = ['/'], route_options = {}, &block) ⇒ Object
Defines a route that will be recognized by the Grape API.
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/grape/dsl/routing.rb', line 147 def route(methods, paths = ['/'], = {}, &block) methods = '*' if methods == :any = { method: methods, path: paths, for: self, route_options: { params: namespace_stackable_with_hash(:params) || {} }.deep_merge(route_setting(:description) || {}).deep_merge( || {}) } new_endpoint = Grape::Endpoint.new(inheritable_setting, , &block) endpoints << new_endpoint unless endpoints.any? { |e| e.equals?(new_endpoint) } route_end reset_validations! end |
#route_param(param, options = {}, &block) ⇒ Object
This method allows you to quickly define a parameter route segment in your API.
220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/grape/dsl/routing.rb', line 220 def route_param(param, = {}, &block) = .dup [:requirements] = { param.to_sym => [:requirements] } if [:requirements].is_a?(Regexp) Grape::Validations::ParamsScope.new(api: self) do requires param, type: [:type] end if .key?(:type) namespace(":#{param}", , &block) end |
#routes ⇒ Object
An array of API routes.
201 202 203 |
# File 'lib/grape/dsl/routing.rb', line 201 def routes @routes ||= prepare_routes end |
#scope(_name = nil, &block) ⇒ Object
Create a scope without affecting the URL.
make the code more readable.
64 65 66 67 68 |
# File 'lib/grape/dsl/routing.rb', line 64 def scope(_name = nil, &block) within_namespace do nest(block) end end |
#version(*args, &block) ⇒ Object
Specify an API version.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/grape/dsl/routing.rb', line 29 def version(*args, &block) if args.any? = args. = .reverse_merge(using: :path) requested_versions = args.flatten.map(&:to_s) raise Grape::Exceptions::MissingVendorOption.new if [:using] == :header && !.key?(:vendor) @versions = versions | requested_versions if block within_namespace do namespace_inheritable(:version, requested_versions) namespace_inheritable(:version_options, ) instance_eval(&block) end else namespace_inheritable(:version, requested_versions) namespace_inheritable(:version_options, ) end end @versions.last if instance_variable_defined?(:@versions) && @versions end |
#versions ⇒ Object
Returns array of defined versions.
235 236 237 |
# File 'lib/grape/dsl/routing.rb', line 235 def versions @versions ||= [] end |