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.
-
#route_set ⇒ Object
readonly
Returns the value of attribute route_set.
-
#routes ⇒ Object
readonly
An array of API routes.
Instance Method Summary collapse
-
#do_not_route_head! ⇒ Object
Do not route HEAD requests to GET requests automatically.
-
#do_not_route_options! ⇒ Object
Do not automatically route OPTIONS.
- #mount(mounts) ⇒ 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_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
Thie method allows you to quickly define a parameter route segment in your API.
-
#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 |
#route_set ⇒ Object (readonly)
Returns the value of attribute route_set.
10 11 12 |
# File 'lib/grape/dsl/routing.rb', line 10 def route_set @route_set end |
#routes ⇒ Object (readonly)
An array of API routes.
172 173 174 |
# File 'lib/grape/dsl/routing.rb', line 172 def routes @routes end |
Instance Method Details
#do_not_route_head! ⇒ Object
Do not route HEAD requests to GET requests automatically.
62 63 64 |
# File 'lib/grape/dsl/routing.rb', line 62 def do_not_route_head! namespace_inheritable(:do_not_route_head, true) end |
#do_not_route_options! ⇒ Object
Do not automatically route OPTIONS.
67 68 69 |
# File 'lib/grape/dsl/routing.rb', line 67 def namespace_inheritable(:do_not_route_options, true) end |
#mount(mounts) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/grape/dsl/routing.rb', line 71 def mount(mounts) mounts = { mounts => '/' } unless mounts.respond_to?(:each_pair) mounts.each_pair do |app, path| in_setting = inheritable_setting if app.respond_to?(:inheritable_setting, true) mount_path = Rack::Mount::Utils.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.regenerate_endpoints(in_setting) app.change! change! end endpoints << Grape::Endpoint.new( in_setting, method: :any, path: path, app: app, 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, or group, resource, segment, etc., will share their parent context as well as any configuration done in the namespace context.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/grape/dsl/routing.rb', line 149 def namespace(space = nil, = {}, &block) if space || block_given? within_namespace do previous_namespace_description = @namespace_description @namespace_description = (@namespace_description || {}).deep_merge(namespace_setting(:description) || {}) nest(block) do if space namespace_stackable(:namespace, Namespace.new(space, )) end end @namespace_description = previous_namespace_description end else Namespace.joined_space_path(namespace_stackable(:namespace)) end end |
#prefix(prefix = nil) ⇒ Object
Define a root URL prefix for your entire API.
57 58 59 |
# File 'lib/grape/dsl/routing.rb', line 57 def prefix(prefix = nil) namespace_inheritable(:root_prefix, prefix) end |
#reset_routes! ⇒ Object
Remove all defined routes.
177 178 179 |
# File 'lib/grape/dsl/routing.rb', line 177 def reset_routes! @routes = nil end |
#route(methods, paths = ['/'], route_options = {}, &block) ⇒ Object
Defines a route that will be recognized by the Grape API.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/grape/dsl/routing.rb', line 112 def route(methods, paths = ['/'], = {}, &block) = { method: methods, path: paths, for: self, route_options: ({ params: Grape::DSL::Configuration.stacked_hash_to_hash(namespace_stackable(: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
Thie method allows you to quickly define a parameter route segment in your API.
186 187 188 189 190 |
# File 'lib/grape/dsl/routing.rb', line 186 def route_param(param, = {}, &block) = .dup [:requirements] = { param.to_sym => [:requirements] } if [:requirements].is_a?(Regexp) namespace(":#{param}", , &block) 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 54 |
# File 'lib/grape/dsl/routing.rb', line 29 def version(*args, &block) if args.any? = args. = .reverse_merge(using: :path) fail Grape::Exceptions::MissingVendorOption.new if [:using] == :header && !.key?(:vendor) @versions = versions | args if block_given? within_namespace do namespace_inheritable(:version, args) namespace_inheritable(:version_options, ) instance_eval(&block) end else namespace_inheritable(:version, args) namespace_inheritable(:version_options, ) end # reset_validations! end @versions.last unless @versions.nil? end |
#versions ⇒ Object
Returns array of defined versions.
193 194 195 |
# File 'lib/grape/dsl/routing.rb', line 193 def versions @versions ||= [] end |