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
- #delete(paths = ['/'], options = {}, &block) ⇒ 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.
- #get(paths = ['/'], options = {}, &block) ⇒ Object
- #head(paths = ['/'], options = {}, &block) ⇒ Object
- #mount(mounts) ⇒ Object
- #namespace(space = nil, options = {}, &block) ⇒ Object (also: #group, #resource, #resources, #segment)
- #options(paths = ['/'], options = {}, &block) ⇒ Object
- #patch(paths = ['/'], options = {}, &block) ⇒ Object
- #post(paths = ['/'], options = {}, &block) ⇒ Object
-
#prefix(prefix = nil) ⇒ Object
Define a root URL prefix for your entire API.
- #put(paths = ['/'], options = {}, &block) ⇒ Object
-
#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
Instance Attribute Details
#endpoints ⇒ Object (readonly)
Returns the value of attribute endpoints.
9 10 11 |
# File 'lib/grape/dsl/routing.rb', line 9 def endpoints @endpoints end |
#route_set ⇒ Object (readonly)
Returns the value of attribute route_set.
9 10 11 |
# File 'lib/grape/dsl/routing.rb', line 9 def route_set @route_set end |
#routes ⇒ Object (readonly)
An array of API routes.
151 152 153 |
# File 'lib/grape/dsl/routing.rb', line 151 def routes @routes end |
Instance Method Details
#delete(paths = ['/'], options = {}, &block) ⇒ Object
119 120 121 |
# File 'lib/grape/dsl/routing.rb', line 119 def delete(paths = ['/'], = {}, &block) route('DELETE', paths, , &block) end |
#do_not_route_head! ⇒ Object
Do not route HEAD requests to GET requests automatically
52 53 54 |
# File 'lib/grape/dsl/routing.rb', line 52 def do_not_route_head! set(:do_not_route_head, true) end |
#do_not_route_options! ⇒ Object
Do not automatically route OPTIONS
57 58 59 |
# File 'lib/grape/dsl/routing.rb', line 57 def set(:do_not_route_options, true) end |
#get(paths = ['/'], options = {}, &block) ⇒ Object
103 104 105 |
# File 'lib/grape/dsl/routing.rb', line 103 def get(paths = ['/'], = {}, &block) route('GET', paths, , &block) end |
#head(paths = ['/'], options = {}, &block) ⇒ Object
115 116 117 |
# File 'lib/grape/dsl/routing.rb', line 115 def head(paths = ['/'], = {}, &block) route('HEAD', paths, , &block) end |
#mount(mounts) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/grape/dsl/routing.rb', line 61 def mount(mounts) mounts = { mounts => '/' } unless mounts.respond_to?(:each_pair) mounts.each_pair do |app, path| if app.respond_to?(:inherit_settings, true) app_settings = settings.clone mount_path = Rack::Mount::Utils.normalize_path([settings[:mount_path], path].compact.join("/")) app_settings.set :mount_path, mount_path app.inherit_settings(app_settings) end endpoints << Grape::Endpoint.new( settings.clone, method: :any, path: path, app: app ) end end |
#namespace(space = nil, options = {}, &block) ⇒ Object Also known as: group, resource, resources, segment
131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/grape/dsl/routing.rb', line 131 def namespace(space = nil, = {}, &block) if space || block_given? previous_namespace_description = @namespace_description @namespace_description = (@namespace_description || {}).deep_merge(@last_description || {}) @last_description = nil nest(block) do set(:namespace, Namespace.new(space, )) if space end @namespace_description = previous_namespace_description else Namespace.joined_space_path(settings) end end |
#options(paths = ['/'], options = {}, &block) ⇒ Object
123 124 125 |
# File 'lib/grape/dsl/routing.rb', line 123 def (paths = ['/'], = {}, &block) route('OPTIONS', paths, , &block) end |
#patch(paths = ['/'], options = {}, &block) ⇒ Object
127 128 129 |
# File 'lib/grape/dsl/routing.rb', line 127 def patch(paths = ['/'], = {}, &block) route('PATCH', paths, , &block) end |
#post(paths = ['/'], options = {}, &block) ⇒ Object
107 108 109 |
# File 'lib/grape/dsl/routing.rb', line 107 def post(paths = ['/'], = {}, &block) route('POST', paths, , &block) end |
#prefix(prefix = nil) ⇒ Object
Define a root URL prefix for your entire API.
47 48 49 |
# File 'lib/grape/dsl/routing.rb', line 47 def prefix(prefix = nil) prefix ? set(:root_prefix, prefix) : settings[:root_prefix] end |
#put(paths = ['/'], options = {}, &block) ⇒ Object
111 112 113 |
# File 'lib/grape/dsl/routing.rb', line 111 def put(paths = ['/'], = {}, &block) route('PUT', paths, , &block) end |
#route(methods, paths = ['/'], route_options = {}, &block) ⇒ Object
Defines a route that will be recognized by the Grape API.
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/grape/dsl/routing.rb', line 91 def route(methods, paths = ['/'], = {}, &block) = { method: methods, path: paths, route_options: (@namespace_description || {}).deep_merge(@last_description || {}).deep_merge( || {}) } endpoints << Grape::Endpoint.new(settings.clone, , &block) @last_description = nil reset_validations! end |
#route_param(param, options = {}, &block) ⇒ Object
Thie method allows you to quickly define a parameter route segment in your API.
160 161 162 163 164 |
# File 'lib/grape/dsl/routing.rb', line 160 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.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/grape/dsl/routing.rb', line 28 def version(*args, &block) if args.any? = args.pop if args.last.is_a? Hash ||= {} = { using: :path }.merge() raise Grape::Exceptions::MissingVendorOption.new if [:using] == :header && !.key?(:vendor) @versions = versions | args nest(block) do set(:version, args) set(:version_options, ) end end @versions.last unless @versions.nil? end |
#versions ⇒ Object
166 167 168 |
# File 'lib/grape/dsl/routing.rb', line 166 def versions @versions ||= [] end |