lib/quirky-api/response.rb
# Returns a JSON response for the API.
#
# @param response [Mixed Array|String|Hash|nil|Bool] The data to return.
# @param options [Hash] A hash of options to apss along with the response.
#                       These options will be passed into QuirkySerializer
#                       for processing.
#
# @example
#   # Will return an array of data.
#   respond_with Item.all
#
#   # Will return a single hash.
#   respond_with Item.last
#
#   # Will return boolean 'true'
#   respond_with true
#
#   # Will return a hash
#   respond_with({
#     my_name: 'Mike'
#   })
#
# All responses will contain a 'data' key, wrapping all information:
#
#     {
#        "data": {
#           "id": 1,
#           "name": "Mike",
#           "last": "Sea"
#         }
#     }
Returns a JSON response for the API.
Returns a JSON response for the API .
def respond_with(response, options = {}, &block)
      return if @performed_render
      options[:envelope] = QuirkyApi.envelope unless options.key?(:envelope)
      options[:params] = params
      options[:headers] = headers
      if block_given?
        options = response
        if options[:cache_key].present?
          cache_opts = {}
          cache_opts[:expires_in] = options[:expires_in] if options[:expires_in].present?
          options[:serialize] = false if options[:serialize].nil?
          cache_key = "#{options[:cache_key]}-api-endpoint"
          response = Rails.cache.fetch cache_key, cache_opts do
            data = if options[:serialize]
                     serialize(block.call)
                   else
                     block.call
                   end
            append_meta(data, options).to_json
          end
          renderable = build_json_response(response, options)
          render renderable
          return
        else
          response = yield
        end
      end
      options[:serialize] = true if options[:serialize].nil?
      return render(prepare_response(nil, options)) if response.nil?
      data = if response.respond_to?(:active_model_serializer) &&
                response.try(:active_model_serializer).present? &&
                options[:serialize] == true
               options[:current_user] = current_user if defined? current_user
               serializer = response.active_model_serializer
               if serializer <= ActiveModel::ArraySerializer
                 serializer = QuirkyArraySerializer
               end
               options[:serialized_data] = serializer.new(response, options)
               options[:serialized_data].as_json(root: false)
             else
               response
             end
      if response.respond_to?(:each) && response.is_a?(Hash)
        paginated_meta = {}
        response.each do |key, value|
          if value.respond_to?(:paginated_meta) && value.paginated_meta.present?
            paginated_meta[key] = value.paginated_meta
          end
        end
        options[:paginated_meta] = { paginated_meta: paginated_meta } unless paginated_meta.empty?
      end
      renderable = prepare_response(data, options)
      render renderable
    end
def respond_with ( response , options = { } , & block ) return if @performed_render options [ :envelope ] = QuirkyApi . envelope unless options . key? ( :envelope ) options [ :params ] = params options [ :headers ] = headers if block_given? options = response if options [ :cache_key ] . present? cache_opts = { } cache_opts [ :expires_in ] = options [ :expires_in ] if options [ :expires_in ] . present? options [ :serialize ] = false if options [ :serialize ] . nil? cache_key = "#{options[:cache_key]}-api-endpoint" response = Rails . cache . fetch cache_key , cache_opts do data = if options [ :serialize ] serialize ( block . call ) else block . call end append_meta ( data , options ) . to_json end renderable = build_json_response ( response , options ) render renderable return else response = yield end end options [ :serialize ] = true if options [ :serialize ] . nil? return render ( prepare_response ( nil , options ) ) if response . nil? data = if response . respond_to? ( :active_model_serializer ) && response . try ( :active_model_serializer ) . present? && options [ :serialize ] == true options [ :current_user ] = current_user if defined? current_user serializer = response . active_model_serializer if serializer <= ActiveModel :: ArraySerializer serializer = QuirkyArraySerializer end options [ :serialized_data ] = serializer . new ( response , options ) options [ :serialized_data ] . as_json ( root : false ) else response end if response . respond_to? ( :each ) && response . is_a? ( Hash ) paginated_meta = { } response . each do | key , value | if value . respond_to? ( :paginated_meta ) && value . paginated_meta . present? paginated_meta [ key ] = value . paginated_meta end end options [ :paginated_meta ] = { paginated_meta : paginated_meta } unless paginated_meta . empty? end renderable = prepare_response ( data , options ) render renderable end
Returns a JSON response for the API.
Returns a JSON response for the API .
# Fallback to default envelope unless we set one on the options. # Include params in all options sent forth to serializers. # Allow us to configure headers in response. # Rails seems to have an issue with caching large objects +.as_json+. # As a result, we need to actively transform the output +.to_json+ in # order to properly cache it and not completely screw up the output. # +expires_in+ is an optional setting for caches, so it's optional # here, too. # Use this option if you want to serialize your data automatically. # Make sure we serialize unless otherwise specified. # If our response variable is nil, just return a null value to save time. # If there's an active model serializer to speak of, use it. # Otherwise, just render what we've got. # Append paginated_meta to the response if any response objects has it
response
The data to return.
The data to return .
options
A hash of options to apss along with the response. These options will be passed into QuirkySerializer for processing.
A hash of options to apss along with the response . These options will be passed into QuirkySerializer for processing .
block
example
Will return an array of data. respond_with Item.all # Will return a single hash. respond_with Item.last # Will return boolean 'true' respond_with true # Will return a hash respond_with({ my_name: 'Mike' }) All responses will contain a 'data' key, wrapping all information: { "data": { "id": 1, "name": "Mike", "last": "Sea" } }
Will return an array of data . respond_with Item . all # Will return a single hash . respond_with Item . last # Will return boolean ' true ' respond_with true # Will return a hash respond_with ( { my_name : ' Mike ' } ) All responses will contain a ' data ' key wrapping all information : { " data " : { " id " : 1 " name " : " Mike " " last " : " Sea " } }