Class: Rancher::Api::JsonParserMiddleware

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/rancher/api/middlewares/json_parser_middleware.rb

Instance Method Summary collapse

Instance Method Details

#on_complete(env) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rancher/api/middlewares/json_parser_middleware.rb', line 4

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