Module: Grape::DSL::InsideRoute
- Extended by:
- ActiveSupport::Concern
- Includes:
- Settings
- Included in:
- Endpoint
- Defined in:
- lib/grape/dsl/inside_route.rb
Instance Attribute Summary
Attributes included from Settings
#inheritable_setting, #top_level_setting
Instance Method Summary collapse
-
#body(value = nil) ⇒ Object
Allows you to define the response body as something other than the return value.
-
#content_type(val = nil) ⇒ Object
Set response content-type.
-
#cookies ⇒ Object
Set or get a cookie.
-
#declared(params, options = {}, declared_params = nil) ⇒ Object
A filtering method that will return a hash consisting only of keys that have been declared by a
params
statement against the current/target endpoint or parent namespaces. - #entity_class_for_obj(object, options) ⇒ Object
-
#error!(message, status = nil, headers = nil) ⇒ Object
End the request and display an error to the end user with the specified message.
-
#header(key = nil, val = nil) ⇒ Object
Set an individual header or retrieve all headers that have been set.
-
#present(*args) ⇒ Object
Allows you to make use of Grape Entities by setting the response body to the serializable hash of the entity provided in the
:with
option. -
#redirect(url, options = {}) ⇒ Object
Redirect to a new url.
-
#route ⇒ Object
Returns route information for the current request.
-
#status(status = nil) ⇒ Object
Set or retrieve the HTTP status code.
-
#version ⇒ Object
The API version as specified in the URL.
Methods included from Settings
#api_class_setting, #get_or_set, #global_setting, #namespace_end, #namespace_inheritable, #namespace_inheritable_to_nil, #namespace_setting, #namespace_stackable, #namespace_start, #route_end, #route_setting, #unset, #unset_api_class_setting, #unset_global_setting, #unset_namespace_inheritable, #unset_namespace_setting, #unset_namespace_stackable, #unset_route_setting, #within_namespace
Instance Method Details
#body(value = nil) ⇒ Object
Allows you to define the response body as something other than the return value.
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/grape/dsl/inside_route.rb', line 151 def body(value = nil) if value @body = value elsif value == false @body = '' status 204 else @body end end |
#content_type(val = nil) ⇒ Object
Set response content-type
121 122 123 124 125 126 127 |
# File 'lib/grape/dsl/inside_route.rb', line 121 def content_type(val = nil) if val header('Content-Type', val) else header['Content-Type'] end end |
#cookies ⇒ Object
Set or get a cookie
137 138 139 |
# File 'lib/grape/dsl/inside_route.rb', line 137 def @cookies ||= Cookies.new end |
#declared(params, options = {}, declared_params = nil) ⇒ Object
A filtering method that will return a hash
consisting only of keys that have been declared by a
params
statement against the current/target endpoint or parent
namespaces
options. :include_parent_namespaces
defaults to true, hence must be set to false if
you want only to return params declared against the current/target endpoint
18 19 20 21 22 23 24 25 26 27 28 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 55 56 |
# File 'lib/grape/dsl/inside_route.rb', line 18 def declared(params, = {}, declared_params = nil) [:include_missing] = true unless .key?(:include_missing) [:include_parent_namespaces] = true unless .key?(:include_parent_namespaces) if declared_params.nil? declared_params = (![:include_parent_namespaces] ? route_setting(:declared_params) : (route_setting(:saved_declared_params) || [])).flatten(1) || [] end unless declared_params fail ArgumentError, 'Tried to filter for declared parameters but none exist.' end if params.is_a? Array params.map do |param| declared(param || {}, , declared_params) end else declared_params.inject(Hashie::Mash.new) do |hash, key| key = { key => nil } unless key.is_a? Hash key.each_pair do |parent, children| output_key = [:stringify] ? parent.to_s : parent.to_sym next unless [:include_missing] || children || params.key?(parent) if params.key?(parent) || [:include_missing] hash[output_key] = if children declared(params[parent] || {}, , Array(children)) else params[parent] end end end hash end end end |
#entity_class_for_obj(object, options) ⇒ Object
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/grape/dsl/inside_route.rb', line 218 def entity_class_for_obj(object, ) entity_class = .delete(:with) if entity_class.nil? # entity class not explicitely defined, auto-detect from relation#klass or first object in the collection object_class = if object.respond_to?(:klass) object.klass else object.respond_to?(:first) ? object.first.class : object.class end object_class.ancestors.each do |potential| entity_class ||= (Grape::DSL::Configuration.stacked_hash_to_hash(namespace_stackable(:representations)) || {})[potential] end entity_class ||= object_class.const_get(:Entity) if object_class.const_defined?(:Entity) && object_class.const_get(:Entity).respond_to?(:represent) end entity_class end |
#error!(message, status = nil, headers = nil) ⇒ Object
End the request and display an error to the end user with the specified message.
68 69 70 71 |
# File 'lib/grape/dsl/inside_route.rb', line 68 def error!(, status = nil, headers = nil) self.status(status || namespace_inheritable(:default_error_status)) throw :error, message: , status: self.status, headers: headers end |
#header(key = nil, val = nil) ⇒ Object
Set an individual header or retrieve all headers that have been set.
112 113 114 115 116 117 118 |
# File 'lib/grape/dsl/inside_route.rb', line 112 def header(key = nil, val = nil) if key val ? @header[key.to_s] = val : @header.delete(key.to_s) else @header end end |
#present(*args) ⇒ Object
Allows you to make use of Grape Entities by setting
the response body to the serializable hash of the
entity provided in the :with
option. This has the
added benefit of automatically passing along environment
and version information to the serialization, making it
very easy to do conditional exposures. See Entity docs
for more info.
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/grape/dsl/inside_route.rb', line 177 def present(*args) = args.count > 1 ? args. : {} key, object = if args.count == 2 && args.first.is_a?(Symbol) args else [nil, args.first] end entity_class = entity_class_for_obj(object, ) root = .delete(:root) representation = if entity_class = { env: env } [:version] = env['api.version'] if env['api.version'] entity_class.represent(object, .merge()) else object end representation = { root => representation } if root if key representation = (@body || {}).merge(key => representation) elsif entity_class.present? && representation.respond_to?('merge') representation = (@body || {}).merge(representation) end body representation end |
#redirect(url, options = {}) ⇒ Object
Redirect to a new url.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/grape/dsl/inside_route.rb', line 78 def redirect(url, = {}) = { permanent: false }.merge() if [:permanent] status 301 else if env['HTTP_VERSION'] == 'HTTP/1.1' && request.request_method.to_s.upcase != 'GET' status 303 else status 302 end end header 'Location', url body '' end |
#route ⇒ Object
Returns route information for the current request.
214 215 216 |
# File 'lib/grape/dsl/inside_route.rb', line 214 def route env['rack.routing_args'][:route_info] end |
#status(status = nil) ⇒ Object
Set or retrieve the HTTP status code.
96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/grape/dsl/inside_route.rb', line 96 def status(status = nil) if status @status = status else return @status if @status case request.request_method.to_s.upcase when 'POST' 201 else 200 end end end |
#version ⇒ Object
The API version as specified in the URL.
59 60 61 |
# File 'lib/grape/dsl/inside_route.rb', line 59 def version env['api.version'] end |