cirandas.net

ref: master

app/api/entity.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
module Api
  class Entity < Grape::Entity

    def initialize(object, options = {})
      object = nil if object.is_a? Exception
      super object, options
    end

    def self.represent(objects, options = {})
      if options[:has_exception]
        data = super objects, options.merge(is_inner_data: true)
        if objects.is_a? Exception
          data.merge ok: false, error: {
            type: objects.class.name,
            message: objects.message
          }
        else
          data = data.serializable_hash if data.is_a? Entity
          data.merge ok: true, error: { type: 'Success', message: '' }
        end
      else
        super objects, options
      end
    end

  end
end