Sha256: 52e6572e6b2eb2d51ebdbe1b73d416f774a76d01f76443c20f474764bd3633f7

Contents?: true

Size: 982 Bytes

Versions: 1

Compression:

Stored size: 982 Bytes

Contents

module Restorm
  module Middleware
    # This middleware expects the resource/collection data to be contained in the `data`
    # key of the JSON object
    class SecondLevelParseJSON < ParseJSON

      # Parse the response body
      #
      # @param [String] body The response body
      # @return [Mixed] the parsed response
      # @private
      def parse(body)
        json = parse_json(body)

        {
          :data => json[:data],
          :errors => json[:errors],
          :metadata => json[:metadata]
        }
      end

      # This method is triggered when the response has been received. It modifies
      # the value of `env[:body]`.
      #
      # @param [Hash] env The response environment
      # @private
      def on_complete(env)
        env[:body] = case env[:status]
                     when 204
                       parse('{}')
                     else
                       parse(env[:body])
                     end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
restorm-1.0.0 lib/restorm/middleware/second_level_parse_json.rb