Module: Grape::DSL::Routing
- Included in:
- API::Instance
- 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
- #cascade(value = nil) ⇒ 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.
- #given(conditional_option) ⇒ Object
- #lint! ⇒ Object
- #mount(mounts, opts = {}) ⇒ Object
- #mounted(&block) ⇒ Object
-
#namespace(space = nil, requirements: 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.
-
#route(methods, paths = ['/'], requirements: nil, anchor: true, **route_options) ⇒ Object
Defines a route that will be recognized by the Grape API.
-
#route_param(param, requirements: nil, type: nil) ⇒ 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, using: :path, cascade: true, parameter: 'apiver', strict: false, vendor: nil) { ... } ⇒ String?
Specify an API version.
-
#versions ⇒ Object
Array of defined versions.
Instance Attribute Details
#endpoints ⇒ Object (readonly)
Returns the value of attribute endpoints.
6 7 8 |
# File 'lib/grape/dsl/routing.rb', line 6 def endpoints @endpoints end |
Instance Method Details
#build_with(build_with) ⇒ Object
104 105 106 |
# File 'lib/grape/dsl/routing.rb', line 104 def build_with(build_with) inheritable_setting.build_params_with = build_with end |
#cascade(value = nil) ⇒ Object
18 19 20 21 22 |
# File 'lib/grape/dsl/routing.rb', line 18 def cascade(value = nil) return inheritable_setting.cascade_defined? ? inheritable_setting.cascade : true if value.nil? inheritable_setting.cascade = value end |
#do_not_document! ⇒ Object
122 123 124 |
# File 'lib/grape/dsl/routing.rb', line 122 def do_not_document! inheritable_setting.do_not_document! end |
#do_not_route_head! ⇒ Object
Do not route HEAD requests to GET requests automatically.
109 110 111 |
# File 'lib/grape/dsl/routing.rb', line 109 def do_not_route_head! inheritable_setting.do_not_route_head! end |
#do_not_route_options! ⇒ Object
Do not automatically route OPTIONS.
114 115 116 |
# File 'lib/grape/dsl/routing.rb', line 114 def inheritable_setting. end |
#given(conditional_option) ⇒ Object
8 9 10 11 12 |
# File 'lib/grape/dsl/routing.rb', line 8 def given(conditional_option, &) return unless conditional_option mounted(&) end |
#lint! ⇒ Object
118 119 120 |
# File 'lib/grape/dsl/routing.rb', line 118 def lint! inheritable_setting.lint! end |
#mount(mounts, opts = {}) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/grape/dsl/routing.rb', line 126 def mount(mounts, opts = {}) mount_opts = opts if opts[:refresh_already_mounted] Grape.deprecator.warn('`refresh_already_mounted` is not a `mount` option and will be ignored in a future release.') drop_endpoints_mounted_for(mounts) # Dropped before the recursion below re-enters with the same options, # so a Grape API does not warn once per mount and once per instance. mount_opts = opts.except(:refresh_already_mounted) end normalize_mounts(mounts).each_pair do |app, path| if app.respond_to?(:mount_instance) mount({ app.mount_instance(configuration: mount_opts[:with] || {}) => path }, mount_opts) next end in_setting = inheritable_setting # Past the mount_instance branch above, a Grape app here is an already # instantiated Grape::API::Instance (vs. a bare Rack app). if app.is_a?(Grape::Mountable) mount_path = Grape::Util::PathNormalizer.call(path) app.top_level_setting.add_mount_path(mount_path) app.inherit_settings(inheritable_setting) in_setting = app.top_level_setting app.change! change! end endpoints << Grape::Endpoint.new( in_setting, http_methods: :any, path:, app:, anchor: false, api: self ) end end |
#mounted(&block) ⇒ Object
14 15 16 |
# File 'lib/grape/dsl/routing.rb', line 14 def mounted(&block) evaluate_as_instance_with_configuration(block, lazy: true) end |
#namespace(space = nil, requirements: 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.
231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/grape/dsl/routing.rb', line 231 def namespace(space = nil, requirements: nil, **, &block) return inheritable_setting.namespace_path unless space || block validate_requirements!(requirements) within_namespace do nest(block) do inheritable_setting.add_namespace(Grape::Namespace.new(space, requirements:, **)) if space end end end |
#prefix(prefix = nil) ⇒ Object
Define a root URL prefix for your entire API.
88 89 90 91 92 |
# File 'lib/grape/dsl/routing.rb', line 88 def prefix(prefix = nil) return inheritable_setting.root_prefix if prefix.nil? inheritable_setting.root_prefix = prefix.to_s end |
#route(methods, paths = ['/'], requirements: nil, anchor: true, **route_options) ⇒ Object
Defines a route that will be recognized by the Grape API.
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/grape/dsl/routing.rb', line 183 def route(methods, paths = ['/'], requirements: nil, anchor: true, **, &) validate_requirements!(requirements) http_methods = methods == :any ? '*' : methods endpoint_description = inheritable_setting.route_description # +params+, +requirements+ and +anchor+ each travel as their own endpoint # input; the route-options bag keeps the description's other keys # (+success+, +tags+, …) plus any custom options. params = prepare_params(endpoint_description[:params]) = endpoint_description.except(:params) .deep_merge!() if .present? new_endpoint = Grape::Endpoint.new( inheritable_setting, http_methods:, path: paths, api: self, params:, requirements:, anchor:, route_options: , & ) endpoints << new_endpoint unless endpoints.include?(new_endpoint) inheritable_setting.route_end reset_validations! end |
#route_param(param, requirements: nil, type: nil) ⇒ Object
This method allows you to quickly define a parameter route segment in your API.
258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/grape/dsl/routing.rb', line 258 def route_param(param, requirements: nil, type: nil, **, &) # The param is named here, so the constraint is its own: nest whatever # it is, not just a Regexp. A Hash would name the param twice, or key a # capture this namespace does not introduce, and belongs on +namespace+. raise ArgumentError, "route_param :`#{param}` constrains :`#{param}`; pass the constraint itself, or a Hash of requirements to the enclosing namespace" if requirements.respond_to?(:to_hash) param_requirements = requirements ? { param.to_sym => requirements } : requirements Grape::Validations::ParamsScope.new(api: self) do requires param, type: type end if type namespace(":#{param}", requirements: param_requirements, **, &) end |
#routes ⇒ Object
An array of API routes.
249 250 251 |
# File 'lib/grape/dsl/routing.rb', line 249 def routes @routes ||= endpoints.map(&:routes).flatten end |
#scope(_name = nil, &block) ⇒ Object
Create a scope without affecting the URL.
make the code more readable.
98 99 100 101 102 |
# File 'lib/grape/dsl/routing.rb', line 98 def scope(_name = nil, &block) within_namespace do nest(block) end end |
#version(*args, using: :path, cascade: true, parameter: 'apiver', strict: false, vendor: nil) { ... } ⇒ String?
Specify an API version.
Called without arguments, returns the most recently declared version
(or nil). Called with one or more version strings, registers them
and stores a VersionOptions value object on the
inheritable settings; when given a block, the registration applies
within a nested namespace.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/grape/dsl/routing.rb', line 63 def version(*args, using: :path, cascade: true, parameter: 'apiver', strict: false, vendor: nil, &block) return @versions&.last if args.empty? raise Grape::Exceptions::MissingVendorOption.new if using == :header && vendor.nil? requested_versions = args.flatten.map(&:to_s) = VersionOptions.new(using:, cascade:, parameter:, strict:, vendor:) @versions = versions | requested_versions if block within_namespace do inheritable_setting.version = requested_versions inheritable_setting. = instance_eval(&block) end else inheritable_setting.version = requested_versions inheritable_setting. = end @versions&.last end |
#versions ⇒ Object
Returns array of defined versions.
274 275 276 |
# File 'lib/grape/dsl/routing.rb', line 274 def versions @versions ||= [] end |