Sha256: 02b418fb71551927eec03fd7ec744e45c4e895196d57fa24198dd000a5df4dcc

Contents?: true

Size: 919 Bytes

Versions: 7

Compression:

Stored size: 919 Bytes

Contents

module Her
  module Middleware
    # This middleware treat the received first-level JSON structure as the resource data.
    class FirstLevelParseJSON < ParseJSON
      # Parse the response body
      #
      # @param [String] body The response body
      # @return [Mixed] the parsed response
      def parse(body)
        json = parse_json(body)
        errors = json.delete(:errors) || {}
        metadata = json.delete(:metadata) || {}
        {
          :data => json,
          :errors => errors,
          :metadata => 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
      def on_complete(env)
        env[:body] = case env[:status]
        when 204
          parse('{}')
        else
          parse(env[:body])
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
her-0.6.3 lib/her/middleware/first_level_parse_json.rb
her-0.6.2 lib/her/middleware/first_level_parse_json.rb
her-0.6.1 lib/her/middleware/first_level_parse_json.rb
her-0.6 lib/her/middleware/first_level_parse_json.rb
her-0.5.5 lib/her/middleware/first_level_parse_json.rb
her-0.5.4 lib/her/middleware/first_level_parse_json.rb
her-0.5.3 lib/her/middleware/first_level_parse_json.rb