Sha256: 2263b4aafe1d5c0ac520aec57adae4024f146a18e86e9ae8ac1c40e2d13653a3

Contents?: true

Size: 925 Bytes

Versions: 8

Compression:

Stored size: 925 Bytes

Contents

module Her
  module Middleware
    # This middleware expects the resource/collection data to be contained in the `data`
    # key of the JSON object
    class JsonApiParser < 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[:meta] || {},
        }
      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, 304
          parse('{}')
        else
          parse(env[:body])
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
her-0.10.4 lib/her/middleware/json_api_parser.rb
her-0.10.3 lib/her/middleware/json_api_parser.rb
her-0.10.2 lib/her/middleware/json_api_parser.rb
her-1.0.1 lib/her/middleware/json_api_parser.rb
her-1.0.0 lib/her/middleware/json_api_parser.rb
her-0.10.1 lib/her/middleware/json_api_parser.rb
her-0.10.0 lib/her/middleware/json_api_parser.rb
her-0.9.0 lib/her/middleware/json_api_parser.rb