Skip to content

Instantly share code, notes, and snippets.

@aishek
Last active August 29, 2015 14:13
Show Gist options
  • Save aishek/ece590998f0697eb95bd to your computer and use it in GitHub Desktop.
Save aishek/ece590998f0697eb95bd to your computer and use it in GitHub Desktop.
grape-entity example
# app/presenters/api/v1/base_entity.rb
module Api
module V1
module BaseEntity < Grape::Entity
format_with(:unix_timestamp) { |dt| dt.to_time.to_i }
end
end
end
# app/presenters/api/v1/status_detailed_entity.rb
module API
module V1
class StatusDetailedEntity < API::V1::StatusDetailed
expose :internal_id
end
end
end
# app/presenters/api/v1/status_entity.rb
module Api
module V1
class StatusEntity < Api::V1::BaseEntity
expose :user_name
expose :text, documentation: { type: "String", desc: "Status update text." }
expose :ip, if: { type: :full }
expose :user_type, :user_id, if: lambda { |status, options| status.user.public? }
expose :contact_info do
expose :phone
expose :address, using: API::V1::AddressEntity
end
expose :digest do |status, options|
Digest::MD5.hexdigest status.txt
end
expose :replies, using: API::V1::StatusEntity, as: :replies
expose :last_reply, using: API::V1::StatusDetailedEntity do |status, options|
status.replies.last
end
with_options(format_with: :unix_timestamp) do
expose :created_at
expose :updated_at
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment