Sha256: 1e4c8a36b4642771b20a17a752c9f175ae3154efa7f59979ad1f95ef03547fb2

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

module Rancher
  module Api
    class JsonParserMiddleware < Faraday::Response::Middleware
      def on_complete(env)
        body = env[:body]

        # Case when resource requested individually doesn't require special
        # handling
        #
        # {
        #   "id": "1ph1",
        #   "type": "machine",
        #   ...
        # }
        #

        # Case when resources are nested inside another 'data' key in response
        # require sepcial handling like the one below
        #
        # {
        #   "type": "collection",
        #   "resourceType": "machine",
        #   "data": [
        #     {
        #       "id": "1ph1",
        #       "type": "machine",
        #       ...
        #     }
        #   ]
        # }
        #

        # If requested resource has type collection - massage data a bit
        # if it's a single resource, just return and render response
        #
        return unless body[:data][:type] == 'collection'

        env[:body] = {
          data: body[:data][:data]
        }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rancher-api-beta-0.8.1 lib/rancher/api/middlewares/json_parser_middleware.rb
rancher-api-beta-0.8.0.pre.beta lib/rancher/api/middlewares/json_parser_middleware.rb