@@ -4,6 +4,7 @@ module Grape
44 module DSL
55 module InsideRoute
66 include Declared
7+ include Entity
78
89 # Backward compatibility: alias exception class to previous location
910 MethodNotYetAvailable = Declared ::MethodNotYetAvailable
@@ -184,50 +185,6 @@ def stream(value = nil)
184185 end
185186 end
186187
187- # Allows you to make use of Grape Entities by setting
188- # the response body to the serializable hash of the
189- # entity provided in the `:with` option. This has the
190- # added benefit of automatically passing along environment
191- # and version information to the serialization, making it
192- # very easy to do conditional exposures. See Entity docs
193- # for more info.
194- #
195- # @example
196- #
197- # get '/users/:id' do
198- # present User.find(params[:id]),
199- # with: API::Entities::User,
200- # admin: current_user.admin?
201- # end
202- def present ( *args , **options )
203- key , object = if args . count == 2 && args . first . is_a? ( Symbol )
204- args
205- else
206- [ nil , args . first ]
207- end
208- entity_class = entity_class_for_obj ( object , options )
209-
210- root = options . delete ( :root )
211-
212- representation = if entity_class
213- entity_representation_for ( entity_class , object , options )
214- else
215- object
216- end
217-
218- representation = { root => representation } if root
219-
220- if key
221- representation = ( body || { } ) . merge ( key => representation )
222- elsif entity_class . present? && body
223- raise ArgumentError , "Representation of type #{ representation . class } cannot be merged." unless representation . respond_to? ( :merge )
224-
225- representation = body . merge ( representation )
226- end
227-
228- body representation
229- end
230-
231188 # Returns route information for the current request.
232189 #
233190 # @example
@@ -240,43 +197,6 @@ def route
240197 env [ Grape ::Env ::GRAPE_ROUTING_ARGS ] [ :route_info ]
241198 end
242199
243- # Attempt to locate the Entity class for a given object, if not given
244- # explicitly. This is done by looking for the presence of Klass::Entity,
245- # where Klass is the class of the `object` parameter, or one of its
246- # ancestors.
247- # @param object [Object] the object to locate the Entity class for
248- # @param options [Hash]
249- # @option options :with [Class] the explicit entity class to use
250- # @return [Class] the located Entity class, or nil if none is found
251- def entity_class_for_obj ( object , options )
252- entity_class = options . delete ( :with )
253- return entity_class if entity_class
254-
255- # entity class not explicitly defined, auto-detect from relation#klass or first object in the collection
256- object_class = if object . respond_to? ( :klass )
257- object . klass
258- else
259- object . respond_to? ( :first ) ? object . first . class : object . class
260- end
261-
262- representations = inheritable_setting . namespace_stackable_with_hash ( :representations )
263- if representations
264- potential = object_class . ancestors . detect { |potential | representations . key? ( potential ) }
265- entity_class = representations [ potential ] if potential
266- end
267-
268- entity_class = object_class . const_get ( :Entity ) if !entity_class && object_class . const_defined? ( :Entity ) && object_class . const_get ( :Entity ) . respond_to? ( :represent )
269- entity_class
270- end
271-
272- # @return the representation of the given object as done through
273- # the given entity_class.
274- def entity_representation_for ( entity_class , object , options )
275- embeds = { env : }
276- embeds [ :version ] = env [ Grape ::Env ::API_VERSION ] if env . key? ( Grape ::Env ::API_VERSION )
277- entity_class . represent ( object , **embeds , **options )
278- end
279-
280200 def http_version
281201 env . fetch ( 'HTTP_VERSION' ) { env [ Rack ::SERVER_PROTOCOL ] }
282202 end
0 commit comments