@@ -443,11 +443,89 @@ RSpec.configure do |config|
443443end
444444```
445445
446+ ## Reusable Responses with Entities
447+
448+ Entities are a simple and reusable means for converting Ruby objects to API responses.
449+ Entities can be used to conditionally include fields, nest other entities, and build
450+ ever larger responses, using inheritance.
451+
452+ ### Defining Entities
453+
454+ Entities inherit from Grape::Entity, and define a simple DSL. The ` #expose ` method can be called in
455+ a number of ways. When processing options passed to exposures two keys will always be defined, : version
456+ and : collection . The : version key is define as api.version. The : collection key is boolean, and defined
457+ as true if the object presented is an array.
458+
459+ * expose SYMBOLS
460+ * define a list of fields which will always be exposed
461+ * expose SYMBOLS, HASH
462+ * HASH keys include : if , : unless , : proc , : as , : using , : format_with , : documentation
463+ * : if and : unless accept hashes (passed during runtime) or procs (arguments are object and options)
464+ * expose SYMBOL, {: format_with => : formatter }
465+ * expose a value, formatting it first
466+ * ` :format_with ` can only be applied to one exposure at a time
467+ * expose SYMBOL, {: as => "alias"}
468+ * Expose a value, changing its hash key from SYMBOL to alias
469+ * ` :as ` can only be applied to one exposure at a time
470+ * expose SYMBOL BLOCK
471+ * block arguments are object and options
472+ * expose the value returned by the block
473+ * ` block ` can only be applied to one exposure at a time
474+
475+ ``` ruby
476+ module API
477+ module Entities
478+ class User < Grape ::Entity
479+ expose :first_name , :last_name
480+ expose :field , :documentation => {:type => " string" , :desc => " words go here" }
481+ expose :email , :if => {:type => :full }
482+ expose :user_type , user_id, :if => lambda {|user ,options | user.confirmed?}
483+ expose(:name ){|user ,options | [user.first_name, user.last_name].join(' ' )}
484+ expose :latest_status , :using => API ::Status , :as => :status
485+ end
486+ end
487+ end
488+
489+ module API
490+ module Entities
491+ class UserDetailed < API ::Entities ::User
492+ expose :account_id
493+ end
494+ end
495+ end
496+ ```
497+
498+ ### Using Entities
499+
500+ Once an entity is defined, it can be used within endpoints, by calling #present. The #present
501+ method accepts two arguments, the object to be presented and the options associated with it. The
502+ options hash must always include : with , which defines the entity to expose.
503+
504+ If the entity includes documentation it can be included in an endpoint's description.
505+
506+ ``` ruby
507+ module API
508+ class Users < Grape ::API
509+ version ' v1'
510+
511+ desc ' User index' , {
512+ :object_fields => API ::Entities ::User .documentation
513+ }
514+ get ' /users' do
515+ @users = User .all
516+ type = current_user.admin? ? :full : :default
517+ present @users , with: API ::Entities ::User , :type => type
518+ end
519+ end
520+ end
521+ ```
522+
446523## Describing and Inspecting an API
447524
448525Grape lets you add a description to an API along with any other optional
449526elements that can also be inspected at runtime.
450- This can be useful for generating documentation.
527+ This can be useful for generating documentation. If the response
528+ requires documentation, consider using an entity.
451529
452530``` ruby
453531class TwitterAPI < Grape ::API
0 commit comments