Module: ActsAsApi::Base::InstanceMethods

Defined in:
lib/acts_as_api/base.rb

Instance Method Summary collapse

Instance Method Details

#as_api_response(api_template, options = {}) ⇒ Object

Creates the api response of the model and returns it as a Hash. Will raise an exception if the passed api template is not defined for the model


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/acts_as_api/base.rb', line 51

def as_api_response(api_template, options = {})
  api_attributes = self.class.api_accessible_attributes(api_template)

  if api_attributes.nil?
    raise ActsAsApi::TemplateNotFoundError,
          "acts_as_api template :#{api_template} was not found for model #{self.class}"
  end

  before_api_response(api_template)
  response_hash = around_api_response(api_template) do
    api_attributes.to_response_hash(self, api_attributes, options)
  end
  after_api_response(api_template)

  response_hash
end