lib/grape/endpoint.rb
def present(object, options = {})
      entity_class = options.delete(:with)

      object.class.ancestors.each do |potential|
        entity_class ||= (settings[:representations] || {})[potential]
      end

      entity_class ||= object.class.const_get(:Entity) if object.class.const_defined?(:Entity)

      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
      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. 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(object, options = {})
      entity_class = options.delete(:with)
      object.class.ancestors.each do |potential|
        entity_class ||= (settings[:representations] || {})[potential]
      end
      entity_class ||= object.class.const_get(:Entity) if object.class.const_defined?(:Entity)
      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
      body representation
    end
def present ( object , options = { } ) entity_class = options . delete ( :with ) object . class . ancestors . each do | potential | entity_class ||= ( settings [ :representations ] || { } ) [ potential ] end entity_class ||= object . class . const_get ( :Entity ) if object . class . const_defined? ( :Entity ) 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 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 .
object
options
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