Sha256: 5a76ea7f55e45546fd4c2c6a5b44b5b73012a4aa224bdc9f328bbee10869d3a7

Contents?: true

Size: 1.04 KB

Versions: 15

Compression:

Stored size: 1.04 KB

Contents

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

15 entries across 15 versions & 1 rubygems

Version Path
rancher-api-0.7.0 lib/rancher/api/middlewares/json_parser_middleware.rb
rancher-api-0.6.0 lib/rancher/api/middlewares/json_parser_middleware.rb
rancher-api-0.5.2 lib/rancher/api/middlewares/json_parser_middleware.rb
rancher-api-0.5.1 lib/rancher/api/middlewares/json_parser_middleware.rb
rancher-api-0.5.0 lib/rancher/api/middlewares/json_parser_middleware.rb
rancher-api-0.4.0 lib/rancher/api/middlewares/json_parser_middleware.rb
rancher-api-0.3.8 lib/rancher/api/middlewares/json_parser_middleware.rb
rancher-api-0.3.7 lib/rancher/api/middlewares/json_parser_middleware.rb
rancher-api-0.3.6 lib/rancher/api/middlewares/json_parser_middleware.rb
rancher-api-0.3.5 lib/rancher/api/middlewares/json_parser_middleware.rb
rancher-api-0.3.3 lib/rancher/api/middlewares/json_parser_middleware.rb
rancher-api-0.3.2 lib/rancher/api/middlewares/json_parser_middleware.rb
rancher-api-0.3.1 lib/rancher/api/middlewares/json_parser_middleware.rb
rancher-api-0.3.0 lib/rancher/api/middlewares/json_parser_middleware.rb
rancher-api-0.2.0 lib/rancher/api/middlewares/json_parser_middleware.rb