lib/grape/dsl/inside_route.rb
# 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.
#
# @example
#
#   get '/users/:id' do
#     present User.find(params[:id]),
#       with: API::Entities::User,
#       admin: current_user.admin?
#   end
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.
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 .
def present(*args)
        options = args.count > 1 ? args.extract_options! : {}
        key, object = if args.count == 2 && args.first.is_a?(Symbol)
                        args
                      else
                        [nil, args.first]
                      end
        entity_class = entity_class_for_obj(object, options)
        root = options.delete(:root)
        representation = if entity_class
                           embeds = { env: env }
                           embeds[:version] = env['api.version'] if env['api.version']
                           entity_class.represent(object, embeds.merge(options))
                         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
def present ( * args ) options = args . count > 1 ? args . extract_options! : { } key , object = if args . count == 2 && args . first . is_a? ( Symbol ) args else [ nil , args . first ] end entity_class = entity_class_for_obj ( object , options ) root = options . delete ( :root ) representation = if entity_class embeds = { env : env } embeds [ :version ] = env [ 'api.version' ] if env [ 'api.version' ] entity_class . represent ( object , embeds . merge ( options ) ) 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
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.
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 .
args
<not_specific>
example
get '/users/:id' do present User.find(params[:id]), with: API::Entities::User, admin: current_user.admin. end
get ' / users / : id ' do present User . find ( params [ : id ] ) with : API :: Entities :: User admin : current_user . admin . end